Unexpected operator in bash [closed]

0

I'm doing a bash script to automatically configure the network. When I finished it I put it in link and it did not give me any mistakes. However, when I run the scritp I get an error on line 58. The error is this:

  

configred.sh: 58: [: yes: unexpected operator

My code is this:

    #!/bin/bash
    echo "Bienvenido al configurador de red automático"
    echo "Selecciona la red en el la que estás 10.0.0.0 por ejemplo"
    read red
    # El parámetro -z comprueba si la variable está vacía
    if [ -z $red ]
        then
            echo "Debes introducir la dirección de red"
    else
            echo "Ahora introduce la dirección ip de la máquina"
            read ip
            if [ -z $ip ]
                then
                    echo "Debes introducir la máscara"
                    exit
            else
                echo "Introduce la máscara de red"
                read netmask
                if [ -z $netmask ]
                    then
                        echo "Debes introducir una mascara de red"
                        exit
                else
                    echo "Introduce la dirección del servidor DNS por ejemplo 8.8.8.8"
                    read dnsnameserver
                    if [ -z $dnsnameserver ]
                        then
                            echo "Debes introducir la dirección del servidor dns"
                            exit
                    else
                        echo "Introduce el dominio"
                        read search
                        if [ -z "$search" ]
                            then
                                echo "Debes introducir el dominio"
                                exit
                        else
                            echo "Introduce la puerta de enlace"
                            read puerta
                            if [ -z $puerta ]
                                then
                                    echo "Debes introducir una puerta de enlace"
                                    exit
                             else
                                echo "Hacemos copia de seguirdad (el fichero será interfaces2)"
                                    cp /etc/network/interfaces /etc/network/interfaces2
                                echo "auto enp0s3" >> /etc/network/interfaces
                                echo "iface enp0s3 inet static" >> /etc/network/interfaces
                                echo "address $ip" >> /etc/network/interfaces
                                echo "netmask $netmask" >> /etc/network/interfaces
                                echo "network $red" >> /etc/network/interfaces
                                echo "gateway $puerta" >> /etc/network/interfaces
                                echo "dns-nameservers $dnsnameserver" >> /etc/network/interfaces
                                echo "dns-search $search" >> /etc/network/interfaces
                                echo "Configuración acabada"
                                    echo "¿Quieres modificar ahora el fichero resolv.conf? si/no"
                                    read respuestaa
#la línea de abajo es la que falla
                                    if [ "$respuestaa" == "si" ]
                                        then
                                            echo "Procedemos a modificar el fichero /etc/resolv.conf"
                                            sed -i.back '1s/^/ $ip   $HOSTNAME.$dominio  $HOSTNAME\n/' /etc/resolv.conf
                                            echo "Se ha generado un fichero .bak de copia de seguridad"
                                            ls *.bak /etc
                                            echo "¿Quieres reiniciar la tajeta de red para aplicar los cambios? si o no"
                                            read respuesta
                                            if [ "$respuesta" == "si" ] 
                                                then
                                                    ifdown enp0s3
                                                    ifup enp0s3
                                                    echo "se ha reiniciado la tarjeta de red"
                                            elif [ "$respuestaa" == "no" ]
                                                then
                                                    echo "Hasta luego"
                                                    exit
                                            else
                                            echo "Introduce una respuesta válida"
                                            exit
                                            fi
                                    fi
                             fi
                        fi
                    fi
                fi
            fi
    fi
    
asked by ras212 07.05.2017 в 12:51
source

1 answer

1

I have restated the code more clearly, and it is already functional. The correct code is the following:

#!/bin/bash
echo "Bienvenido al configurador de red automático"
echo "Selecciona la red en el la que estás 10.0.0.0 por ejemplo"
    read red
echo "Ahora introduce la dirección ip de la máquina"
    read ip
echo "Introduce la puerta de enlace"
    read puerta
echo "Introduce la máscara de red"
    read netmask
echo "Introduce la dirección del servidor DNS por ejemplo 8.8.8.8"
    read dnsnameserver
echo "Introduce el dominio"
    read search
# El parámetro -z comprueba si la variable está vacía y -n si está completa
if [ -z $red ] || [ -z $ip ] || [ -z $netmask ] || [ -z $dnsnameserver ] || [ -z "$search" ] || [ -z $puerta ]
    then
        echo "Debes introducir todos los parámetros"
        exit
else
    echo "Hacemos copia de seguirdad (el fichero será interfaces2)"
            cp /etc/network/interfaces /etc/network/interfaces2
    echo "auto enp0s3" >> /etc/network/interfaces
    echo "iface enp0s3 inet static" >> /etc/network/interfaces
    echo "address $ip" >> /etc/network/interfaces
    echo "netmask $netmask" >> /etc/network/interfaces
    echo "network $red" >> /etc/network/interfaces
    echo "gateway $puerta" >> /etc/network/interfaces
    echo "dns-nameservers $dnsnameserver" >> /etc/network/interfaces
    echo "dns-search $search" >> /etc/network/interfaces
    echo "Configuración acabada"

    echo "¿Quieres modificar ahora el fichero resolv.conf? Pulsa una tecla"
        read respuestaa
            if [ -n "$respuestaa" ]
                then
                    echo "Procedemos a modificar el fichero /etc/resolv.conf"
                    sed -i.back '1s/^/ $ip   $HOSTNAME.$dominio  $HOSTNAME\n/' /etc/resolv.conf
                    echo "Se ha generado un fichero .bak de copia de seguridad"
                    ls *.bak /etc
                    echo "¿Quieres reiniciar la tajeta de red para aplicar los cambios? pulsa una tecla para confirmar"
                    read respuesta
                    if [ -n "$respuesta" ]
                        then
                            ifdown enp0s3
                            ifup enp0s3
                            echo "se ha reiniciado la tarjeta de red"
                    elif [ -z "$respuesta" ]
                        then
                            echo "Hasta luego"
                                exit
                    fi
                                   else
                                        echo "Introduce una respuesta válida"
                                        exit
            fi
fi
    
answered by 07.05.2017 / 16:27
source