LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash script if elif problem (https://www.linuxquestions.org/questions/linux-software-2/bash-script-if-elif-problem-689173/)

assyrian1 12-08-2008 04:06 PM

Bash script if elif problem
 
Hi,
I have the following code for a bash script
Code:

#
#!/bin/bash

FILE="-f"
UDP="-u"
V4L="-v"

if [ "$1" == "$FILE" ]
then
echo "-f"
elif [ "$1" == "UDP" ]
then
echo "-u"
elif [ "$1" == "V4L" ]
then
echo "-v"
else
echo "no matches with parameter $1"
fi

when i send -f to the script is displays -f as expected however any other arguments i sent e.g. -u or -v, it goes straight to the else branch e.g
./script.sh -u
results in
no matches with parameter -u

Am I doing something wrong here?

I have also tried putting the first if and then on the same line with a semi-colon after the brace i.e.
if [ "$1" == "$FILE" ]; then
echo "-f"
and leaving the rest unchanged but this still produces the same problem

Thanks

jailbait 12-08-2008 04:36 PM

elif [ "$1" == "UDP" ]

should be:

elif [ "$1" == "$UDP" ]


elif [ "$1" == "V4L" ]

should be:

elif [ "$1" == "$V4L" ]


----------------------
Steve Stites

baig 12-09-2008 09:00 AM

#!/bin/bash

FILE="-f"
UDP="-u"
V4L="-v"

if [ "$1" == "$FILE" ]
then
echo "-f"
elif [ "$1" == "UDP" ] -->$UPD
then
echo "-u"
elif [ "$1" == "V4L" ] -->$V4L
then
echo "-v"
else
echo "no matches with parameter $1"
fi


run your script in debug mode..


sh -x your_script_name.sh

and see what you want to compare and what it is actually comparing..

Cheers!!


All times are GMT -5. The time now is 03:05 AM.