LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-08-2008, 04:06 PM   #1
assyrian1
LQ Newbie
 
Registered: Jun 2005
Location: sydney
Distribution: Fedora Core 7
Posts: 17

Rep: Reputation: 0
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
 
Old 12-08-2008, 04:36 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,336

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
elif [ "$1" == "UDP" ]

should be:

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


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

should be:

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


----------------------
Steve Stites
 
Old 12-09-2008, 09:00 AM   #3
baig
Member
 
Registered: Nov 2008
Location: وادی ھنزہ
Distribution: Solaris 5.10, Debian Server 5.2, CentOS 5.6
Posts: 226
Blog Entries: 3

Rep: Reputation: 38
#!/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!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH if/elif variable problem GNUJoshua Programming 6 07-15-2008 07:01 AM
Problem in script bash with if nadavvin Linux - General 2 08-05-2006 01:51 PM
Bash if, elif, else not working Blackout_08 Programming 7 06-09-2006 10:28 AM
bash script problem fatbastard spice Linux - General 2 08-24-2005 02:15 AM
bash scripting question - elif ?? xscousr Programming 2 08-12-2003 09:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration