LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-20-2006, 12:51 PM   #1
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Rep: Reputation: 16
Question 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
 
Old 01-20-2006, 01:19 PM   #2
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
Try this:
Code:
#!/bin/sh
if test "$1" == "" ; then
	echo $'\a'Supply 1 parameter, e.g:
	echo $0 BAR
	exit
fi
echo FOO=$1
 
Old 01-20-2006, 02:27 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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...
 
Old 01-20-2006, 08:20 PM   #4
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Original Poster
Rep: Reputation: 16
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.
 
Old 01-20-2006, 08:55 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Here is one way to do it:
Code:
..
[[ -n "$1" ]] || { echo "Usage: testIt foo"; exit 0 ; }
..
 
Old 01-21-2006, 12:07 PM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
[[ -n "$1" ]] && echo "FOO=$1" || echo "Usage: testIt foo"
 
Old 01-21-2006, 01:21 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by gnashley
[[ -n "$1" ]] && echo "FOO=$1" || echo "Usage: testIt foo"
You are missing the exit requirement.
 
Old 01-24-2006, 03:56 PM   #8
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Original Poster
Rep: Reputation: 16
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.
 
Old 01-25-2006, 01:12 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ; }
..
 
Old 02-07-2006, 05:20 PM   #10
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Original Poster
Rep: Reputation: 16
Thumbs up

Thank you jlliagre. Both of your solutions work.

Thanks!
 
  


Reply



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 statement os2 Programming 2 03-20-2005 10:13 PM
? on logical operators in BASH eroica Linux - Software 10 02-01-2005 01:12 PM
bash script test file operators... bulliver Programming 3 10-17-2003 12:06 PM
bash if statement question xscousr Programming 3 09-02-2003 11:58 AM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:28 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