LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH scripts check for arguments (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripts-check-for-arguments-793420/)

Drigo 03-05-2010 10:12 AM

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

troop 03-05-2010 10:21 AM

Code:

for argument in $@ ;do
  # case or if, for example:
  case $argument in
    "-1stargument" )
      do 1
    ;;
    "-2stargument" )
      do 2
    ;;
  esac
done


catkin 03-05-2010 11:34 AM

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


allanf 03-06-2010 12:23 AM

Quote:

Originally Posted by troop (Post 3887095)
Code:

for argument in $@ ;do

Code:

for argument in "$@"; do
Has arguments quoted.....

salasi 03-06-2010 08:37 AM

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.

catkin 03-06-2010 10:37 AM

Quote:

Originally Posted by salasi (Post 3888197)
If you want a tutorial, you could read here.

Thanks salasi -- that's a good tutorial; bookmarked :)

Drigo 03-07-2010 11:34 AM

Thanks! I will give it a try on Monday!


All times are GMT -5. The time now is 04:30 PM.