Sponsored Content
Full Discussion: bash:getopts command help
Top Forums Shell Programming and Scripting bash:getopts command help Post 302616299 by bashily on Saturday 31st of March 2012 11:43:01 AM
Old 03-31-2012
I have this:
Code:
#!/bin/bash
# Argument = -t test -r server -p password -v usage()
{
  cat << EOF
usage: $0 options
 This script run the test1 or test2 over a machine.
 OPTIONS:
    -h        Show this message
    -t        Test type, can be ‘test1′ or ‘test2′
    -r        Server address
    -p        Server root password
    -v        Verbose
EOF
}
 TEST=
SERVER=
PASSWD=
VERBOSE=
while getopts “ht:r:p:v” OPTION
do
      case $OPTION in
            h)
                  usage
                  exit 1
                  ;;
            t)
                  TEST=$OPTARG
                  ;;
            r)
                  SERVER=$OPTARG
                  ;;
            p)
                  PASSWD=$OPTARG
                  ;;
            v)
                  VERBOSE=1
                  ;;
            ?)
                  usage
                  exit
                  ;;
     esac
done
 if [[ -z $TEST ]] || [[ -z $SERVER ]] || [[ -z $PASSWD ]]
then
      usage
      exit 1
fi

when I run any other options it gives me the message:
Quote:
./myscript: illegal option -- r
This is controlled by the command not my script and I don't have such a message. How can I get rid of that error?

---------- Post updated 03-31-12 at 11:43 AM ---------- Previous update was 03-30-12 at 08:56 PM ----------

haha! I found the problem. it was wrong quotation!

Last edited by bashily; 03-31-2012 at 10:27 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File handling, getopts command in unix

I need to create a shell script having the menu with few options such as 1. Listing 2. Change permissions 3. Modify Contents 4. Delete Files 5. Exit 1. For 1. Listing: Display a special listing of files showing their date of modification and access time (side by side) along with their... (2 Replies)
Discussion started by: bab123
2 Replies

2. UNIX for Dummies Questions & Answers

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... (2 Replies)
Discussion started by: IceX
2 Replies

3. Shell Programming and Scripting

[bash] getopts not executing when called second time.

Hi, Unexpectedly, the function below doesn't seem to work when called a second time. The output shows that when the function is called the first time, it works as expected but when it is called a second time, the loop that processes the options passed to the function, with the while loop,... (2 Replies)
Discussion started by: ASGR
2 Replies

4. Shell Programming and Scripting

Help with getopts command

Hello All, I have shell script as below in a.ksh. #! /usr/bin/ksh while getopts a: b: ab:f: VAR do case $VAR in a) A=${OPTARG} echo $A;; b) B=${OPTARG} echo $B;; ab) AB=${OPTARG} echo $AB ;; f) F=${OPTARG} echo $F ;; esac done When I execute sh a.ksh -a 1 -b 2 -ab 3 -f 4 as below... (7 Replies)
Discussion started by: tonsat
7 Replies

5. 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

6. 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

7. Shell Programming and Scripting

[BASH] Using getopts

Heyas Just recently there was a thread about parsing arguments, where i read the first time about getopts. This said, i'd like to 'provide' a list function that can be 'trigered' using an 'option'(?). The regarding code snippets are: while getopts... (7 Replies)
Discussion started by: sea
7 Replies

8. Shell Programming and Scripting

[BASH] getopts, OPTARG is passed but empty

EDIT: -- SOLVED -- Heyas, Getting used to optargs, but by far not understanding it. So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals. Passing: browser -t test -d sect $HOME Where -t should change the title, and -d... (0 Replies)
Discussion started by: sea
0 Replies

9. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

10. Shell Programming and Scripting

How-to enforce check on getopts command

here is my script that expects the user to run it like ./best.sh -f /tmp/log.txt more best.sh #!/bin/bash while getopts ":f:" opt; do case $opt in f) file_in="$OPTARG" ;; \?) echo "Invalid option -$OPTARG" >&2 ;; esac done uname -a SunOS mymac 5.11 11.2 sun4v... (15 Replies)
Discussion started by: mohtashims
15 Replies
LIBBASH(7)							  libbash Manual							LIBBASH(7)

NAME
libbash -- A bash shared libraries package. DESCRIPTION
libbash is a package that enables bash dynamic-like shared libraries. Actually its a tool for managing bash scripts whose functions you may want to load and use in scripts of your own. It contains a 'dynamic loader' for the shared libraries ( ldbash(1)), a configuration tool (ldbashconfig(8)), and some libraries. Using ldbash(1) you are able to load loadable bash libraries, such as getopts(1) and hashstash(1). A bash shared library that can be loaded using ldbash(1) must answer 4 requirments: 1. It must be installed in $LIBBASH_PREFIX/lib/bash (default is /usr/lib/bash). 2. It must contain a line that begins with '#EXPORT='. That line will contain (after the '=') a list of functions that the library exports. I.e. all the function that will be usable after loading that library will be listed in that line. 3. It must contain a line that begins with '#REQUIRE='. That line will contain (after the '=') a list of bash libraries that are required for our library. I.e. every bash library that is in use in our bash library must be listed there. 4. The library must be listed (For more information, see ldbashconfig(8)). Basic guidelines for writing library of your own: 1. Be aware, that your library will be actually sourced. So, basically, it should contain (i.e define) only functions. 2. Try to declare all variables intended for internal use as local. 3. Global variables and functions that are intended for internal use (i.e are not defined in '#EXPORT=') should begin with: __<library_name>_ For example, internal function myfoosort of hashstash library should be named as __hashstash_myfoosort This helps to avoid conflicts in global name space when using libraries that come from different vendors. 4. See html manual for full version of this guide. AUTHORS
Hai Zaar <haizaar@haizaar.com> Gil Ran <ril@ran4.net> SEE ALSO
ldbash(1), ldbashconfig(8), getopts(1), hashstash(1) colors(1) messages(1) urlcoding(1) locks(1) Linux Epoch Linux
All times are GMT -4. The time now is 06:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy