Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-20-2006, 12:51 PM
|
#1
|
Member
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92
Rep:
|
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators
I'm trying to write a Bash script using some shorthand Bash operators.
I want a short script to test for a commandline argument.
- If $1 is not present, then print a Usage statement and exit from the function.
- If there is a value, then continue with the rest of the function.
Code:
function testIt {
# If no arguments, print usage statement & exit from function
[[ -n "$1" ]] || ( echo "Usage: testIt foo"; exit 0 ; )
# Else, continue and print the arguments
echo "FOO=$1"
}
Here's the problem. If I run the command with no arguments, the 'echo "FOO=$1"' is still printed.
Code:
$ testIt
Usage: testIt foo
FOO=
I know this is simple, but I just can't wrap my head around it this morning. It's been a long week.
Any help is appreciated.
-= Stefan
|
|
|
01-20-2006, 01:19 PM
|
#2
|
Member
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 492
|
Try this:
Code:
#!/bin/sh
if test "$1" == "" ; then
echo $'\a'Supply 1 parameter, e.g:
echo $0 BAR
exit
fi
echo FOO=$1
|
|
|
01-20-2006, 02:27 PM
|
#3
|
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141
Rep: 
|
I think the reason you were still getting the "FOO=" output is that ( echo "Usage: testIt foo"; exit 0 ; ) runs in a subshell because of the brackets. It exits from the subshell at your exit statement and continues with the script. dogpatch's example executes in one level...
|
|
|
01-20-2006, 08:20 PM
|
#4
|
Member
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92
Original Poster
Rep:
|
Thanks Dogpatch. The 'if ; then; fi' certainly works.
I was just trying to figure out how to do this with the shorthand operators like [[ TEST ]] || and && .
Since I write these sorts of scripts often, I was hoping to find a one-liner to do this. Plus, it'd slick.
It *should* work at some level, right?
Hrm, I'll have to work on this more.
|
|
|
01-20-2006, 08:55 PM
|
#5
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795
|
Here is one way to do it:
Code:
..
[[ -n "$1" ]] || { echo "Usage: testIt foo"; exit 0 ; }
..
|
|
|
01-21-2006, 12:07 PM
|
#6
|
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928
|
[[ -n "$1" ]] && echo "FOO=$1" || echo "Usage: testIt foo"
|
|
|
01-21-2006, 01:21 PM
|
#7
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795
|
Quote:
Originally Posted by gnashley
[[ -n "$1" ]] && echo "FOO=$1" || echo "Usage: testIt foo"
|
You are missing the exit requirement.
|
|
|
01-24-2006, 03:56 PM
|
#8
|
Member
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92
Original Poster
Rep:
|
Quote:
Originally Posted by jlliagre
Here is one way to do it:
Code:
..
[[ -n "$1" ]] || { echo "Usage: testIt foo"; exit 0 ; }
..
|
This statement will exit from the current shell. And actually, this is probably the preferred behavior within a shell script-- the script spawns a subshell (e.g. #!/bin/bash ), and the script & subshell will exit if it encounters an error.
However, if I put this in function within my current interactive shell, my current shell will exit.
I'm not saying that your solution is wrong  It is just an interesting thing to notice.
|
|
|
01-25-2006, 01:12 AM
|
#9
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795
|
Quote:
Originally Posted by stefanlasiewski
This statement will exit from the current shell. And actually, this is probably the preferred behavior within a shell script-- the script spawns a subshell (e.g. #!/bin/bash ), and the script & subshell will exit if it encounters an error.
However, if I put this in function within my current interactive shell, my current shell will exit.
I'm not saying that your solution is wrong  It is just an interesting thing to notice.
|
If you want only the function to be exited, here's a way:
Code:
..
[[ -n "$1" ]] || { echo "Usage: testIt foo"; return ; }
..
|
|
|
02-07-2006, 05:20 PM
|
#10
|
Member
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92
Original Poster
Rep:
|
Thank you jlliagre. Both of your solutions work.
Thanks!
|
|
|
All times are GMT -5. The time now is 06:04 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|