LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2010, 10:12 AM   #1
Drigo
Member
 
Registered: May 2009
Posts: 111

Rep: Reputation: 17
Unhappy BASH scripts check for arguments


I know that $1 ... $n will set the argument to the values but what happened if I have many options in one scripts lets suppose.

I can run:
myscript -1stargument -2ndargument -3rdargument

or myscript -1stargument

or myscript -1stargument -3rdargument
.
.
.


In my scripts I have

if [ $1 = "-1stargument" -o $2 = "-2ndargument"] #for every isntruction i need

fi


but is there any wayu to do:
if [ (any arguments or commands) = "(mydesired option ] ; then
(do this...)

fi

?

Thanks
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-05-2010, 10:21 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Code:
for argument in $@ ;do
  # case or if, for example:
  case $argument in
    "-1stargument" )
      do 1
    ;;
    "-2stargument" )
      do 2
    ;;
  esac
done
 
Old 03-05-2010, 11:34 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
getopts is designed for option parsing. It's one of those things that is better learned by example than by reading the documentation. Here's my getopts template.
EDIT: corrected ANSI string usage (in red).
Code:
    # Set defaults that may be overriden by command line parsing
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    <whatever>

    # Parse command line
    # ~~~~~~~~~~~~~~~~~~
    while getopts dhVo: opt 2>/dev/null
    do
        case $opt in
            d )
                debug='YES'
                ;;
            h )
                usage -v
                \exit 0
                ;;
            V )
                echo "$prgnam version $prg_ver"
                \exit 0
                ;;
            o )
                o_optarg="$OPTARG"
                ;;
            * )
                emsg="$emsg"$'\n'"<error message>"
        esac
    done

    # Test for extra arguments
    # ~~~~~~~~~~~~~~~~~~~~~~~~
    shift $(( $OPTIND-1 ))
    if [[ $* != '' ]]; then
        emsg="$emsg"$'\n'"<error message>"
    fi

    # Test for mandatory options not set
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if [[ ${o_optarg:-} = '' ]]; then
        <error message and exit>
    fi

    # Report any accumulated errors
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if [[ $emsg != '' ]]; then
        echo "$emsg" >&2
        usage
        \exit 1
    fi

Last edited by catkin; 03-07-2010 at 01:23 AM.
 
2 members found this post helpful.
Old 03-06-2010, 12:23 AM   #4
allanf
Member
 
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 98
Blog Entries: 1

Rep: Reputation: 19
Quote:
Originally Posted by troop View Post
Code:
for argument in $@ ;do
Code:
for argument in "$@"; do
Has arguments quoted.....
 
Old 03-06-2010, 08:37 AM   #5
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,061

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Thank you to catkin for that post; I had thought about doing something similar, but it was one of many things that I never got around to.

If you want a tutorial, you could read here.
 
1 members found this post helpful.
Old 03-06-2010, 10:37 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by salasi View Post
If you want a tutorial, you could read here.
Thanks salasi -- that's a good tutorial; bookmarked
 
Old 03-07-2010, 11:34 AM   #7
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Thanks! I will give it a try on Monday!
 
  


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
regarding shell scripts arguments neelendhar Linux - Newbie 5 05-18-2008 07:53 AM
bash script arguments R3N3G4D3 Programming 5 11-01-2007 10:16 AM
shell scripts wrappers and passing arguments nosbod Programming 2 09-21-2006 08:27 AM
Hiding arguments in shell scripts 3saul Linux - Software 2 01-09-2006 02:44 PM
bash arguments stuckinhell Programming 6 08-13-2004 05:10 AM


All times are GMT -5. The time now is 07:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration