Advertisement
Guest User

asd

a guest
May 25th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.38 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Introduz os dados num ficheiro
  4. introduzir()
  5. {
  6. clear
  7. nome=""
  8. morada=""
  9. telefone=""
  10. email=""
  11.  
  12. # open fd
  13. exec 3>&1
  14.  
  15. # Store data to $VALUES variable
  16. VALUES=$(dialog --ok-label "Guardar" \
  17.       --backtitle "Lista telefonica" \
  18.       --title "Criar novo contacto" \
  19.       --form "Dados:" \
  20. 15 50 0 \
  21.     "Nome:"     1 1 "$nome"      1 10 40 0 \
  22.     "Morada:"   2 1 "$morada"    2 10 40 0 \
  23.     "Telefone:" 3 1 "$telefone"  3 10 15 0 \
  24.     "Email:"    4 1 "$email"     4 10 40 0 \
  25. 2>&1 1>&3)
  26.  
  27.       a=1
  28.       b=4
  29.       while true;
  30.       do
  31.           valores=`echo "$VALUES" | tail -n $j | head -n 1`
  32.           case $i in
  33.                1)nome="$valores";;
  34.                2)morada="$valores";;
  35.                3)telefone="$valores";;
  36.                4)email="$valores";;
  37.           esac
  38.           i=`expr $a + 1`
  39.           j=`expr $b - 1`
  40.  
  41.           test $a -eq 10 && break
  42.       done
  43.  
  44.       #dialog --title "Oi" --msgbox "NmNome: $nome\nNmMorada: $morada\nNMTelefone: $telefone\nNMEMAIL: $email" 0 0
  45.      
  46.       mysql -u root -p -e "INSERT INTO agenda VALUES('id','$nome','$morada','$telefone','$email')" mysql_bash
  47. }
  48.  
  49.  
  50. # LISTAR DADOS
  51. listar()
  52. {
  53.     dialog --title 'Lista Telefonica' --textbox dados.dat 20 80
  54. }
  55.  
  56.  
  57. # PESQUISA
  58. pesquisa()
  59. {
  60.     # open fd
  61. exec 3>&1
  62.     palavra=$(dialog --ok-label "Ok" \
  63.       --backtitle "Lista telefonica" \
  64.       --title "Pesquisar" \
  65.       --form "Dados:" \
  66. 15 50 0 \
  67.     "Palavra:"     1 1  "$palavra"      1 10 40 0 \
  68. 2>&1 1>&3)
  69.     [ $? -ne 0 ] && break
  70.    
  71.    
  72.     # Testa pra ver se é procura exata ou parcial
  73.   if [ "$*" = "${*#*\*}" ];then
  74.     # Faz pesquisa exata do nome
  75.     mysql -u root -p -e\ "SELECT * FROM agenda WHERE nome = '$*'" mysql_bash
  76.  
  77.   # Procura por partes do nome
  78.   else
  79.     # ${*//\\*/%} = troca todos * por %, que é o curinga do LIKE
  80.     mysql -u root -p -e\ "SELECT * FROM agenda WHERE nome LIKE '${*//\\*/%}'" mysql_bash
  81.   fi
  82.  
  83.   # a procura retornou algum registro ?!
  84.   [ "$S" ] || { echo "Registro não encontrado";exit; }
  85.  
  86.  
  87.  
  88.    # colocar um TAB como IFS
  89.   IFS="$(echo -e '\t')"
  90.   # Apagamos a primeira linha, pois ela contém o nome dos campos
  91.   S=$(echo "$S" | sed '1d')
  92.  
  93.   echo "$S" | while read nome morada telefone email;
  94.   do
  95.  
  96.       resultado=$(dialog --ok-label "" \
  97.               --backtitle "Resultados da pesquisa" \
  98.               --title "" \
  99.               --form "Dados:" \
  100.         15 50 0 \
  101.             "Nome:"     1 1 "$nome"      1 10 40 0 \
  102.             "Morada:"   2 1 "$morada"    2 10 40 0 \
  103.             "Telefone:" 3 1 "$telefone"  3 10 15 0 \
  104.             "Email:"    4 1 "$email"     4 10 40 0 \
  105.         2>&1 1>&3)
  106.  
  107.   done
  108.    
  109. }
  110.  
  111.  
  112. # Mostra o menu na tela, com as ações disponíveis
  113. while : ; do
  114.     resposta=$(
  115.       dialog --stdout               \
  116.              --title 'Lista Telefonica'  \
  117.              --menu 'Introduza a opção:' \
  118.             0 0 0                   \
  119.             1 'Introduzir'          \
  120.             2 'Listar'              \
  121.             3 'Pesquisar'           \
  122.             4 'Alterar'             \
  123.             4 'Apagar'              \
  124.             0 'Sair'                )
  125.  
  126.     # Ela apertou CANCELAR ou ESC, então vamos sair...
  127.     [ $? -ne 0 ] && break
  128.  
  129.     # De acordo com a opção escolhida, dispara programas
  130.     case "$resposta" in
  131.          1) introduzir ;;
  132.          2) listar ;;
  133.          3) pesquisa ;;
  134.          4) alterar ;;
  135.          5) apagar ;;
  136.          0) break ;;
  137.     esac
  138.  
  139. done
  140.  
  141. # final msg
  142. echo 'Obrigado por utilizar a agenda e Adeus!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement