problems when running a shell script, problems in the "if"

0

Hello, I went to run a shell script in the terminal but I get an error as if I could not accept the condition in the first one (I already gave him the permissions) the exercise says the following:

Build a Shell Script called lartc.sh that allows you to receive the following parameters and be validated:

lartc -i ethN -r class 1 percentage 1 ... class n percentage n

lartc -i ethN -cln

lartc -i ethN -voip bw

EthN: a number from 0 to 9 to place with the word eth ex. "Eth2"

Class: an 8-bit binary number ex. "01100101"

Percentage: a percentage number eg. "20%" (Between 0 - 100)

Bw: a decimal number (Positive or Negative)

examples:

lartc -i eth0 -r 01101010 39% 01001010 51% 01111010 20%

lartc -i eth6 -voip -2000

lartc -i eth3 -r 00000011 50% 11001100 69%

lartc -i eth9 -cln

and I will leave a screen shot of what I get in console

the $ 1 is lartc and it says lartc = lartc which is true but it does not execute the rest and I do not know why here is the code

#!/bin/bash
numero='^-?[0-9]+([.][0-9]+)?$'
cod='^[01]{8}$'
proc='0*(100|[0-9]{1,2})%$'
if [$1="lartc"] && [$2="-i"]; then 
   if [$3="eth0"] || [$3="eth1"] || [$3="eth2"] || [$3="eth3"] || [$3="eth4"] || [$3="eth5"] || [$3="eth6"] || [$3="eth7"] || [$3="eth8"] || [$3="eth9"]; then
  if [$4="-cln"]; then
 if [ -z $5 ]; then 
             echo correcto
else 
                                        echo incorrecto
fi
elif [$4="-voip"]; then
 if [$5=~ $numero]; then
                echo incorrecto
else
                    if [ -z $6 ]; then
                       echo correcto
 else 
                       echo incorrecto
fi
fi
elif [$4="-r"]; then
                      if [ $5 -le 0 ]; then
                         echo incorrecto
else 

 for ((num =6; num <=$5+5; num=num+1)) do
cont=0
cont=$num+1
if [${!num}= ~$cod] && [${!cont}= ~$proc]; then 
echo incorrecto
else 
echo correcto
fi
done
fi
else 
echo incorrecto
fi
else 
echo incorrecto
fi
else 
echo incorrecto!
fi
    
asked by Jose_Silva 27.10.2018 в 22:25
source

1 answer

1

There are several errors in the code. but I'll focus on the one that causes the error message:

./lartc.sh: línea 5: [=lartc]: orden no encontrada

The construction of type [ ... ] no is special syntax of the shell. It is actually a variant of the command test which has the peculiarity of obligatorily requiring that its last argument is ] . And like any other command, it is necessary that the name of the command and its arguments are separated by at least a blank space or tabulation.

That is, instead of [foo=bar] , the correct thing would be to write [ foo = bar ] or test foo = bar .

On the other hand, the error in the first command [ ... ] causes the first condition of the first block if to fail, so the echo command that is found to the bottom of the code is executed. That is why the rest of the code is not executed.

Your if block is really massive ( 6 levels of nesting! ), but this is a simplification of what happens:

if false; then
  # Aquí van el resto de if anidados
else
  echo incorrecto
fi

As for the other errors, I will only give general advice:

  • Quote the parameter expansions (example: "$var" instead of $var ) to prevent undesired effects from field splitting .
  • Combine syntax of Bash and POSIX . Decide for one.
  • Make the handling of arguments more robust. You could use getopts .
answered by 28.10.2018 в 02:42