Find the answer to your Linux question:
Results 1 to 3 of 3
Hi. Im wondering if there is a way to use flags when starting a script. Something like this. Code: Script -a -b 10 Right now Ive got something like this. ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    9

    BASH: Script Input Flags

    Hi. Im wondering if there is a way to use flags when starting a script.
    Something like this.

    Code:
    Script -a -b 10
    Right now Ive got something like this.

    Code:
    while getopts ab AA
       do case $AA in
           a) var="value"
           ;;
       esac
    done
    So with that code I can only use flags ( -a -b ). Can I do the -b 10 so it will store say varB=10?

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Getopts command in Linux Shell Script

    From the manual page,
    "optstring contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. Each time it is invoked, getopts places the next option in the shell variable variable_name, When an option requires an argument, getopts places that argument into the variable OPTARG. On errors getopts diagnostic messages are printed when illegal options or missing option arguments are encountered. If an illegal option is seen, getopts places ? into variable_name."

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    9
    So then the script would look like this.

    Code:
    while getopts ab: AA
       do case $AA in
           a) var="value"
           ;;
           b) varB=$optarg
       esac
    done
    when I run the scirpt like this

    Code:
    Script -a -b 10
    So $varB = 10?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...