Sponsored Content
Full Discussion: getopts question!!!
Top Forums Shell Programming and Scripting getopts question!!! Post 302112179 by andy2000 on Monday 26th of March 2007 10:34:46 AM
Old 03-26-2007
getopts question!!!

is there a better way to check if all args are set???

while getopts h:p:u: opt
do
case "$opt" in
h) host="$OPTARG";;
p) port="$OPTARG";;
u) user="$OPTARG";;
\?)
echo >&2 \
"usage: $0 -h host -p port -u user"
exit 1;;
esac
done

shift `expr $OPTIND - 1`

if test -z "$host"
then
echo >&2 \ "host missing !!!\nusage: $0 -h host -p port -u user"
exit 1
fi

if test -z "$port"
then
echo >&2 \ port missing\n "usage: $0 -h host -p port -u user"
exit 1
fi

if test -z "$user"
then
echo >&2 \ "user missing!!\nusage: $0 -h host -p port -u user"
exit 1
fi

Last edited by andy2000; 03-26-2007 at 11:41 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

help in getopts

hey need help with getopts again. i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument e.g srcipt_name -f 3 i am able to use getopts to do this but i am having problems two accept more than two agruments e.g.... (1 Reply)
Discussion started by: problems
1 Replies

2. Shell Programming and Scripting

question about getopts

hello there I am back with more questions (sorry it is been quite a while since I had done scripting). I had tried the search function to search for the threads that might have an answer to my question, but I could not find it, so I had decided to post it. I had created the scripts below in ksh... (2 Replies)
Discussion started by: ahtat99
2 Replies

3. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

4. Shell Programming and Scripting

getopts question

could anyone please tell me what this will automatically set my variable all=True.... #!/bin/sh all=FALSE while getopts a: option do case "option" in a) all=TRUE;; /?) echo "...... " exit 1;; esac done if then echo "true" else echo "false" (2 Replies)
Discussion started by: k2k
2 Replies

5. Shell Programming and Scripting

getopts question

I am trying to set up prompts when you don't enter the right information or dont enter the information at all, when executing a script. Below is the question that i am asking and i am not sure how to set up the if statements to make sure that the user enters the name, cpu's, memory and ip. I was... (3 Replies)
Discussion started by: rookieuxixsa
3 Replies

6. Shell Programming and Scripting

Question about getopts

I am trying to use the finction getopts in script. From what I understand this can be an example for using: while getopt "p:rvt:" do case p) echo "$OPTARG" ;; r) echo... (5 Replies)
Discussion started by: programAngel
5 Replies

7. Shell Programming and Scripting

? used in getopts

Suppose I have a code below . while getopts a: opt do case $opt in a) app_name="$OPTARG";; *) echo "$opt is an invalid option"; exit 1;; ?) echo "The value of $OPTARG is an invalid option"; exit 1;; esac done Could anyone please tell me in which case my... (1 Reply)
Discussion started by: maitree
1 Replies

8. Shell Programming and Scripting

Getopts help

Hi All, I am writing a script to pass the getopts argument to the function which I have. But it as soon as I execute the script, the argument is taking it as blank. I tried using multiple way to check but its not working. Can someone please let me know what wrong in this code. function1()... (4 Replies)
Discussion started by: sidh_arth85
4 Replies

9. Shell Programming and Scripting

Using getopts

Hi. Can somebody please show me an example of how to use getopts to assign a variable if it's been passed into the script but to set a default if no value has been passed in? And also how to handle a param with multiple values ... so a sub parse (can I use a function for this?)? Here's my code... (1 Reply)
Discussion started by: user052009
1 Replies

10. UNIX for Beginners Questions & Answers

Question about getopts optional argument [args...]

There are many places where I can see the syntax description for optargs, which, usually boils down to this: getopts OPTSTRING VARNAME where: OPTSTRING tells getopts which options to expect and where to expect arguments VARNAME tells getopts which shell-variable to use for option reporting... (2 Replies)
Discussion started by: sharkura
2 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 09:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy