Home > Net >  Trouble with Errors on Unix sh with if statements
Trouble with Errors on Unix sh with if statements

Time:11-11

I am having a problem with the code below with if statements. It has three different arguments, I'm not sure if one or all of them are wrong.

#!/bin/sh


1=C
2=S
(1/2)=C  <---- This might be a problem

if [ $1 == "C*" ]
then
    echo "The city is:"

else [ $2 ==  "S*" ]
then
    echo "The state is:"

else [ $1 -a $2 == "B*" ]
then
    echo "The city and state is:"

else [ $1 -o $2 -ne "C*" "S*" "B*" ]
then
    echo "Not found!"
fi
    echo End of script

CodePudding user response:

Using numbers as variables is very unwise idea. Numbers are used to represent command line parameters.

Also this construction is AFAIK unknown to bash: (1/2)=C

Also this construction:

else [ $2 ==  "S*" ]
then

is wrong, should be some something like:

elif [ $2 ==  "S*" ]
then

Also this:

else [ $1 -o $2 -ne "C*" "S*" "B*" ]

is odd, you use -ne which is for compare numbers, for strings use !=

And also a lot of other errors/problems challenging constructions I have no time to mention.

  • Related