Sponsored Content
Top Forums UNIX for Dummies Questions & Answers getopts - command line arguments Post 302350711 by IceX on Friday 4th of September 2009 03:54:05 PM
Old 09-04-2009
getopts - command line arguments

Hi, I'm having problems with a script where I wanted every single option specified in the command line to have an argument taken with it, but for some reason only d works in the code I will be showing below.

For example if I did ./thisfile -a something
it would come up with "a chosen with " as it doesn't take something in as the argument, and the same happens with b and c too, but with d it actually says "d chosen with something". Can someone tell me why only d is taking in the argument value but not the other 3?

Here is the current test script:
Code:
#!/bin/sh
aflag=
bflag=
cflag=
dflag=
while getopts abcd: option
do
    case $option in
    a)    aflag=1
        aarg="$OPTARG"
    ;;
    b)    bflag=1
        barg="$OPTARG"
    ;;
    c)    cflag=1
        carg="$OPTARG"
    ;;
    d)    dflag=1
        darg="$OPTARG"
    ;;
    esac
done

if [ ! -z "$aflag" ]
then
    echo "a chosen with $aarg";
fi

if [ ! -z "$bflag" ]
then
    echo "b chosen with $barg";
fi

if [ ! -z "$cflag" ]
then
    echo "c chosen with $carg";
fi

if [ ! -z "$dflag" ]
then
    echo "d chosen with $darg";
fi


Last edited by IceX; 09-04-2009 at 05:02 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

arguments in command line

Hi all, How many arguments can we pass while testing a prgm at command line.. I encountered an issue while passing 10 arguments. For $10 its taking argument passed for $1 followed by 'zero'. can we pass more than 9 arguments /Is there any other way. Thanks, rrs (6 Replies)
Discussion started by: rrs
6 Replies

2. Shell Programming and Scripting

command line arguments

-------------------------------------------------------------------------------- I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence... (1 Reply)
Discussion started by: skooly5
1 Replies

3. Shell Programming and Scripting

getopts with non-option arguments?

Hello everyone, Is it possible to use getopts and also receive arguments without option flags? e.g. myscript arg1 arg2 -a arg3 -b arg4 If so, how do you stop getopts from exiting as soon as it detects the non-option arguments? (2 Replies)
Discussion started by: kdelok
2 Replies

4. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

5. Shell Programming and Scripting

Need help with parsing the arguments using getopts !!

Hi all, I am trying to use long arguments for my existing script. right now my script would work if given <script_name> -t <arg1> -b <arg2> -v <arg3>. The script code is shown below. while getopts t:v:b: OPT;do case "$OPT" in t) Todo=$OPTARG;; b) Batch=$OPTARG;; ... (3 Replies)
Discussion started by: SSSB
3 Replies

6. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

7. Shell Programming and Scripting

Intersperse arguments and options w/ getopts

Is it possible to get a script that uses getopts to accept options and arguments in any order? eg. -g -h 2 4 works like -g 2 -h 4. (1 Reply)
Discussion started by: lee.n.doan
1 Replies

8. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 Replies

9. Shell Programming and Scripting

using getopts to parse a command line

i have the following scenario want to run the following script with manadory and optional argumnets Manadory options are : filename="" port="" optional arguments type -t balances -b bal prices -p ./test filename port -t A -b bal my code i have that won't parse the options is... (1 Reply)
Discussion started by: nano2
1 Replies

10. Shell Programming and Scripting

Getopts how to handle missing '-' in command line args.

I'm using getopts to process command line args in a Bash script. The code looks like this: while getopts ":cfmvhs:t:" option; do case $option in c) operationMode="CHECK" ;; f) operationMode="FAST" ;; m) ... (6 Replies)
Discussion started by: gencon
6 Replies
getopts(1)						      General Commands Manual							getopts(1)

NAME
getopts - Parses utility options SYNOPSIS
getopts optstring name [arg...] STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: getopts: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
None OPERANDS
A string containing the option characters recognized by the utility invoking getopts. If a character is followed by a colon (:), the option is expected to have an argument, which should be supplied as a separate argument. Applications should specify an option character and its option-argument as separate arguments, but getopts interprets the characters following an option character requiring arguments as an argument whether or not this is done. An explicit null option-argument is not recognized if it is not supplied as a separate argument when getopts is invoked. The characters question-mark (?) and colon (:) must not be used as option characters by an application. The use of other option characters that are not alphanumeric produces unspecified results. If the option-argument is not supplied as a separate argument from the option character, the value in OPTARG is stripped of the option character and the hyphen (-). The first character in opt- string determines getopts behavior if an option character is not known or an option-argument is missing. The name of a shell variable that is set by the getopts utility to the option character that was found. The getopts utility by default parses positional parameters passed to the invoking shell procedure. If args are given, they are parsed instead of the positional parameters. DESCRIPTION
The getopts utility is used to retrieve flags and flag-arguments from a list of parameters. Each time it is invoked, the getopts utility places the value of the next flag in the shell variable specified by the name operand and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell is invoked, OPTIND is initialized to 1. When the flag requires a flag-argument, the getopts utility places it in the shell variable OPTARG. If no flag is found, or if the flag found does not have a flag-argument, OPTARG is unset. If a flag character not contained in the optstring operand is found where a flag character is expected, the shell variable specified by name is set to the question-mark (?) character. In this case, if the first character in optstring is a colon (:), the shell variable OPTARG is set to the flag character found, but no output is written; otherwise, the shell variable OPTARG is unset and a diagnostic message writ- ten. This condition is considered to be an error detected in the way arguments were presented to the invoking application, but is not an error in getopts processing. If a flag-argument is missing, then: If the first character of optstring is a colon, the shell variable specified by name is set to the colon (:) character and the shell variable OPTARG is set to the flag character found. Otherwise, the shell variable specified by name is set to the question-mark (?) character, the shell variable OPTARG is unset, and a diagnostic message is written to standard error. This condition is considered to be an error detected in the way arguments were presented to the invoking application, but is not an error in getopts processing; a diagnostic message is written as stated, but the exit status is zero. When the end of flags is encountered the getopts utility exits with: A return value greater than zero The shell variable OPTIND set to the index of the first non-flag-argument, where the first -- argument is considered to be a flag-argument if there are no other non-flag-argu- ments appearing before it, or the value $# + 1 if there are no non-flag-arguments The name variable set to the question-mark (?) character Any of the following identifies the end of flags: The special flag -- Finding an argument that does not begin with a - Encountering an error The shell variables OPTIND and OPTARG are local to the caller of getopts and are not exported by default. The shell variable specified by the name operand, OPTIND and OPTARG affect the current shell execution environment. If the application sets OPTIND to the value 1, a new set of parameters can be used: either the current positional parameters or new arg values. Any other attempt to invoke getopts multiple times in a single shell execution environment with parameters (positional parameters or flag operands) that are not the same in all invocations, or with an OPTIND value modified to be a value other than 1, produces unspeci- fied results. NOTES
If getopts is called in a subshell or separate utility execution environment, such as one of the following it will not affect the shell variables in the caller's environment: (getopts abc value "$@") nohup getopts ... Shell functions share OPTIND with the calling shell even though the positional parame- ters are changed. Functions that want to use getopts to parse their arguments will usually want to save the value of OPTIND on entry and restore it before returning. However, there are cases when a function will want to change OPTIND for the calling shell. RESTRICTIONS
The getopts command is implemented as a shell built-in command. It is only available to users of the Korn (ksh) and POSIX shells. A similar capability is available to Bourne and C shell users with the getopt command. EXIT STATUS
The following exit values are returned: An option, specified or unspecified by optstring, was found. Successful completion. The end of options was encountered or an error occurred. EXAMPLES
The following example script parses and displays its arguments: aflag= bflag= while getopts ab: name do case $name in a) aflag=1;; b) bflag=1 bval="$OPTARG";; ?) printf "Usage: %s: [-a] [-b value] args " $0 exit 2;; esac done if [ ! -z "$aflag" ]; then printf "Option -a specified " fi if [ ! -z "$bflag" ]; then printf 'Option -b "%s" specified ' "$bval" fi shift $(($OPTIND - 1)) printf "Remaining arguments are: %s " "$*" ENVIRONMENT VARIABLES
The following environment variables affect the execution of getopts: Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization variables contain an invalid setting, the utility behaves as if none of the variables had been defined. If set to a non-empty string value, overrides the values of all the other internationalization variables. Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multibyte characters in arguments and input files). Determines the locale used to affect the format and contents of diagnostic messages written to standard error. Determines the location of message catalogues for the processing of LC_MESSAGES. On exit, this variable will contain the value of a flag-argumentm if one was found, other- wise it is unset. This variable is used by the getopts utility as the index of the next argument to be processed. SEE ALSO
Commands: getopt(1), ksh(1), sh(1p) Routines: getopt(3) Standards: standards(5) getopts(1)
All times are GMT -4. The time now is 10:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy