3

I already revised my code, but i coudn´t find anything wrong, when i try to execute with ./client.sh it outputs the following error:

./client.sh: line 111: syntax error near unexpected token `done'

Here is my script:

while :
do
        clear
        menu_inicial=$(dialog --stdout --title 'Menu inicial' --menu 'Escolha uma opção:' 0 0 0 
        Cadastrar 'Cria um novo usuário'                            
        Entrar 'Fazer login com sua conta'                          
        Sair 'Encerra o SEPA')

        # Caso o usuário selecione Cancelar, a execução do script será encerrada
        if [ $? = 1 ]; then
            if [ -f resposta_servidor ] && [ -f dados_digitados ]; then
                rm resposta_servidor dados_digitados
                clear
                exit
            else
                clear
                exit
            fi
        fi

        # THE PROBLEM IS ON THIS CASE
        case "$menu_inicial" in

            "Entrar") ##### LOGIN #####
                while :
                do
                    clear
                    # Dialog para digitar o nome de usuário
                    login_usuario=$(dialog --title 'Entrar' --stdout --inputbox 'Digite seu nome de usuário:' 0 0)

                    # Caso o usuário selecione Cancelar, a execução do script será encerrada
                    if [ $? = 1 ]; then
                        break; # Retorna ao menu inicial
                    # Caso o nome de usuário nao for digitado, o usuário será alertado
                    elif [ "$login_usuario" = '' ]; then
                        dialog --stdout --msgbox 'Digite o nome de usuário!' 0 0
                        break;
                    fi

                    # Dialog para digitar a senha
                    login_senha=$(dialog --title 'Entrar' --stdout --passwordbox 'Digite sua senha:' 0 0)

                    # Caso o usuário selecione Cancelar, a execução do script será encerrada
                    if [ $? = 1 ]; then
                        break; # Retorna ao menu inicial
                    # Caso a senha não for digitada, o usuário será alertado
                    elif [ "$login_senha" = '' ]; then
                        dialog --stdout --msgbox 'Digite a senha!' 0 0
                        break;
                    fi

                    echo $login_usuario","$login_senha",login" > /dev/tcp/127.0.0.1/5678; # Envia os dados digitados pelo usuário para o servidor
                    nc -l -p 8765 > resposta_servidor # Abre a porta 8765 no cliente para receber a resposta do servidor

                    # Caso o nome de usuário de a senha estejam corretos, o login é feito
                    if grep -qw "LOGIN_SUCESSO" resposta_servidor
                    then
                        dialog --stdout --msgbox 'Conectado com sucesso!' 0 0
                        break;
                    # Caso o nome de usuário ou a senha estejam incorretos, uma mensagem de erro é mostrada
                    elif grep -qw "LOGIN_ERRO" resposta_servidor
                    then
                        dialog --stdout --msgbox 'Senha incorreta, tente novamente!' 0 0
                    fi
                done ##### END LOGIN #####
            ;;

            "Cadastrar") ##### REGISTER #####
                while :
                do
                    clear
                    # Dialog para digitar o nome de usuário
                    cadastrar_usuario=$(dialog --title 'Cadastrar' --stdout --inputbox 'Digite seu nome de usuário:' 0 0)

                    # Caso o usuário selecione Cancelar, a execução do script será encerrada
                    if [ $? = 1 ]; then
                        break; # Retorna ao menu inicial
                    # Caso o nome de usuário nao for digitado, o usuário será alertado
                    elif [ "$cadastrar_usuario" = '' ]; then
                        dialog --stdout --msgbox 'Digite o nome de usuário!' 0 0
                        break;
                    fi

                    # Dialog para digitar a senha
                    cadastrar_senha=$(dialog --title 'Cadastrar' --stdout --passwordbox 'Digite sua senha:' 0 0)

                    # Caso o usuário selecione Cancelar, a execução do script será encerrada
                    if [ $? = 1 ]; then
                        break; # Retorna ao menu inicial
                    # Caso a senha não for digitada, o usuário será alertado
                    elif [ "$cadastrar_senha" = '' ]; then
                        dialog --stdout --msgbox 'Digite a senha!' 0 0
                        break;
                    fi

                    echo $cadastrar_usuario","$cadastrar_senha",cadastro" > /dev/tcp/127.0.0.1/5678; # Envia os dados digitados pelo usuário para o servidor
                    nc -l -p 8765 > resposta_servidor # Abre a porta 8765 no cliente para receber a resposta do servidor

                    # Caso o nome de usuário de a senha estejam definidos, o cadastro é feito
                    if grep -qw "CADASTRO_SUCESSO" resposta_servidor
                    then
                        dialog --stdout --msgbox 'Cadastrado com sucesso!' 0 0
                        break;
                    # Caso ocorra algum erro durante o cadastro, uma mensagem de erro é mostrada
                    elif grep -qw "CADASTRO_ERRO" resposta_servidor
                    then
                        dialog --stdout --msgbox 'Erro no cadastro, tente novamente!' 0 0
                    fi
                done ##### END REGISTER #####
            ;;
        esac
done
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 1
    Try adding set -x to the top of your script and see what is being sent to the shell before it errors. – jesse_b Feb 04 '18 at 18:20
  • I can't find any issues either but I suggest using uniform styling. Some of your if/then statements have then on the same line, some don't, etc. Here is this code more uniformly formatted and properly tabbed if it helps anyone view it: https://gist.github.com/jessebutryn/917ad06d18df8ff9b44c3d7b6a07d032 – jesse_b Feb 04 '18 at 18:26
  • Does it work if you add a hashbang to the top of the script? #!/bin/bash – jesse_b Feb 04 '18 at 18:47
  • 5
    The way to debug this sort of thing (and any other program) is to make a copy and then start deleting lines until you get the minimum piece of code that reproduces the error. Once you have found this minimal example, edit your question and show us only that. It is not very easy to debug 112 lines of code. . . – terdon Feb 04 '18 at 18:51
  • Here's a general tip on writing long, complicated case statements in sh (or any language, really): Write separate functions that implement each case, and then have a simple case statement that calls the appropriate function. Each function can be debugged independently, and the case statement is much easier to read. in fact the whole script is much easier to read. – cas Feb 05 '18 at 03:58

1 Answers1

2

Apart from a missing #!/bin/bash on the first line, the only thing I can spot in this script that would be worth fixing is the command substitution with dialog at the top:

clear
menu_inicial=$(dialog --stdout --title 'Menu inicial' --menu 'Escolha uma opção:' 0 0 0 
Cadastrar 'Cria um novo usuário'
Entrar 'Fazer login com sua conta'
Sair 'Encerra o SEPA')

This should probably be

clear
menu_inicial=$(dialog --stdout --title 'Menu inicial' --menu 'Escolha uma opção:' 0 0 0 \
Cadastrar 'Cria um novo usuário' \
Entrar 'Fazer login com sua conta' \
Sair 'Encerra o SEPA')

Note the escaped newlines. Without doing this, the command substitution will involve calling dialog, then Cadastrar, Entrar and Sair as if they were utilities.

If I add these to your script, it seems to work for me using bash 4.4 on OpenBSD.

Neither I nor ShellCheck can spot a syntax error that would trigger the error that you are seeing.

The only thing ShellCheck finds that I missed at my first glance at the code is that

echo $cadastrar_usuario","$cadastrar_senha",cadastro"

would probably be better written as

printf '%s,%s,cadastro\n' "$cadastrar_usuario" "$cadastrar_senha"

And similarly in one other place in the code.


If the script file has been edited on a Windows machine, it could be that it's a DOS text file. In that case it has an extra carriage return at the end of each line which could very well cause the interpreter to complain about syntax errors. Turning your script into a DOS text file and running it with bash does provoke some syntax errors, however, these are not exactly the same as the ones you are reporting.

You may convert the file to a Unix text file using the dos2unix utility. See also the questions How to test whether a file uses CRLF or LF without modifying it? and Remove ^M character from log files

Regarding the missing #!-line, see the question Which shell interpreter runs a script with no shebang?

Kusalananda
  • 333,661