Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-15-2004, 09:31 AM   #1
Registered: Oct 2003
Location: TX
Distribution: RH 8.0
Posts: 97

Rep: Reputation: 15
Bash shell scripting question

This just deals with easy if statements, but I'm not too knowledgible about Bash's quirks.

I wrote a script that only needs one argument passed into it. However, I'm not sure how to output instructions about how to use the script if no argument is passed in.

Basically, my whole script consists of a lot of if's. But, problem number 2 arises if the proper if isn't found. I tried to put an else at the end, but that doesn't work because it is a standalone else outside of an if statment.

My solution is this, but how do I implement it. How would have an outside if that accepts any arguement but blank? Then have a second if to accept nothing.

Thanks for your time.
Old 07-15-2004, 09:47 AM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
How about this:

# Function to display usage. Everything between "cat << EOF" and "EOF" is
# displayed on the terminal.
function display_usage()
  cat << EOF
Basic Usage:
  $0 <argument>

<argument> is required, and causes action foo.

# Start of the main script. $# is a psecial variable that contains the number of
# arguments on the command line.
if [ $# -eq 0 ] ; then
  echo "Not enough arguments on command line. Displaying usage..."

  # This will preven the script from continuing execution since the
  # requirement of one or more arguments was not met.
  exit 1

# If the script reaches this point, there are 1 or more arguments on the command line.
Old 07-15-2004, 10:54 AM   #3
Registered: Oct 2003
Location: TX
Distribution: RH 8.0
Posts: 97

Original Poster
Rep: Reputation: 15
Yeah, that will work, thanks a lot!!!


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
shell (bash) scripting question(s) boxerboy Programming 5 11-08-2005 03:51 PM
Bash shell scripting question. pete1234 Programming 11 09-25-2005 02:59 AM
Bash shell scripting question. Itsu Linux - General 3 04-30-2005 03:52 AM
Help with I/O on bash shell scripting Dave6383 Programming 1 06-03-2004 05:24 PM
BASH Shell Scripting Question xianzai Programming 5 03-19-2004 07:50 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 AM.

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