Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Introduz os dados num ficheiro
- introduzir()
- {
- clear
- nome=""
- morada=""
- telefone=""
- email=""
- # open fd
- exec 3>&1
- # Store data to $VALUES variable
- VALUES=$(dialog --ok-label "Guardar" \
- --backtitle "Lista telefonica" \
- --title "Criar novo contacto" \
- --form "Dados:" \
- 15 50 0 \
- "Nome:" 1 1 "$nome" 1 10 40 0 \
- "Morada:" 2 1 "$morada" 2 10 40 0 \
- "Telefone:" 3 1 "$telefone" 3 10 15 0 \
- "Email:" 4 1 "$email" 4 10 40 0 \
- 2>&1 1>&3)
- a=1
- b=4
- while true;
- do
- valores=`echo "$VALUES" | tail -n $j | head -n 1`
- case $i in
- 1)nome="$valores";;
- 2)morada="$valores";;
- 3)telefone="$valores";;
- 4)email="$valores";;
- esac
- i=`expr $a + 1`
- j=`expr $b - 1`
- test $a -eq 10 && break
- done
- #dialog --title "Oi" --msgbox "NmNome: $nome\nNmMorada: $morada\nNMTelefone: $telefone\nNMEMAIL: $email" 0 0
- mysql -u root -p -e "INSERT INTO agenda VALUES('id','$nome','$morada','$telefone','$email')" mysql_bash
- }
- # LISTAR DADOS
- listar()
- {
- dialog --title 'Lista Telefonica' --textbox dados.dat 20 80
- }
- # PESQUISA
- pesquisa()
- {
- # open fd
- exec 3>&1
- palavra=$(dialog --ok-label "Ok" \
- --backtitle "Lista telefonica" \
- --title "Pesquisar" \
- --form "Dados:" \
- 15 50 0 \
- "Palavra:" 1 1 "$palavra" 1 10 40 0 \
- 2>&1 1>&3)
- [ $? -ne 0 ] && break
- # Testa pra ver se é procura exata ou parcial
- if [ "$*" = "${*#*\*}" ];then
- # Faz pesquisa exata do nome
- mysql -u root -p -e\ "SELECT * FROM agenda WHERE nome = '$*'" mysql_bash
- # Procura por partes do nome
- else
- # ${*//\\*/%} = troca todos * por %, que é o curinga do LIKE
- mysql -u root -p -e\ "SELECT * FROM agenda WHERE nome LIKE '${*//\\*/%}'" mysql_bash
- fi
- # a procura retornou algum registro ?!
- [ "$S" ] || { echo "Registro não encontrado";exit; }
- # colocar um TAB como IFS
- IFS="$(echo -e '\t')"
- # Apagamos a primeira linha, pois ela contém o nome dos campos
- S=$(echo "$S" | sed '1d')
- echo "$S" | while read nome morada telefone email;
- do
- resultado=$(dialog --ok-label "" \
- --backtitle "Resultados da pesquisa" \
- --title "" \
- --form "Dados:" \
- 15 50 0 \
- "Nome:" 1 1 "$nome" 1 10 40 0 \
- "Morada:" 2 1 "$morada" 2 10 40 0 \
- "Telefone:" 3 1 "$telefone" 3 10 15 0 \
- "Email:" 4 1 "$email" 4 10 40 0 \
- 2>&1 1>&3)
- done
- }
- # Mostra o menu na tela, com as ações disponíveis
- while : ; do
- resposta=$(
- dialog --stdout \
- --title 'Lista Telefonica' \
- --menu 'Introduza a opção:' \
- 0 0 0 \
- 1 'Introduzir' \
- 2 'Listar' \
- 3 'Pesquisar' \
- 4 'Alterar' \
- 4 'Apagar' \
- 0 'Sair' )
- # Ela apertou CANCELAR ou ESC, então vamos sair...
- [ $? -ne 0 ] && break
- # De acordo com a opção escolhida, dispara programas
- case "$resposta" in
- 1) introduzir ;;
- 2) listar ;;
- 3) pesquisa ;;
- 4) alterar ;;
- 5) apagar ;;
- 0) break ;;
- esac
- done
- # final msg
- echo 'Obrigado por utilizar a agenda e Adeus!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement