Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 22nd, 2005, 9:48 AM   #1
levk
Newbie
 
Join Date: Jul 2005
Posts: 7
Rep Power: 0 levk is on a distinguished road
capturing and dispatching on command line arguments in bash

I'm having problems with recognizing command line args in my script, whatever "if" I make, they all evaluate to true, what am I doing wrong? here's my test script:

#!/usr/bin/bash

if [ $1=blah ]; then
    echo no quotes
fi

if [ $1="blah" ]; then
    echo quotes
fi

if [ $1="foo" ]; then
    echo foo
fi

echo $1

And here's my output:

bash-2.05$ ./test.sh blah
no quotes
quotes
foo
blah
bash-2.05$

Thank you very much in advance
levk is offline   Reply With Quote
Old Jul 22nd, 2005, 10:09 AM   #2
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
if [ "$1" = "blah" ]; then
echo equals blah;
fi
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Old Jul 22nd, 2005, 10:26 AM   #3
levk
Newbie
 
Join Date: Jul 2005
Posts: 7
Rep Power: 0 levk is on a distinguished road
Thanks a lot, that worked; but now I have another problem: I need to execute the insides of the "if" if there was a particular argument or there were no arguments at all. Now it works for the particular arguments but givem me errors if I don't put any arguments; here's what my ifs look like:

if [ -z $1 -o "$1" = "unix" ]; then

And here's the error I'm getting:
bash-2.05$ ./logcollect.sh
./logcollect.sh: [: too many arguments
./logcollect.sh: [: too many arguments
./logcollect.sh: [: too many arguments
./logcollect.sh: [: too many arguments
./logcollect.sh: [: too many arguments
./logcollect.sh: [: too many arguments
./logcollect.sh: [: too many arguments
./logcollect.sh: [: too many arguments
./logcollect.sh: [: too many arguments
bash-2.05$

Thank you very much

Edit: it's one line for each "if" I have like that
levk is offline   Reply With Quote
Old Jul 22nd, 2005, 10:31 AM   #4
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
same fix
if [ -z "$1" -o "$1" = "unix" ]; then
   echo ok;
fi
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Old Jul 22nd, 2005, 10:35 AM   #5
levk
Newbie
 
Join Date: Jul 2005
Posts: 7
Rep Power: 0 levk is on a distinguished road
Doup! Thanks so much man
levk is offline   Reply With Quote
Old Jul 22nd, 2005, 11:53 AM   #6
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
You're welcome
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC