Visit The New, Modern Unix Linux Community

bash:getopts command help

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash:getopts command help
# 1  
bash:getopts command help

How can I say one of the options is required? can I use an if statement?
let say:
while getopts ":c:u:fp" opt; do
  case $opt in
    c)    echo "-c was triggered, Parameter: $OPTARG" >&2;;
    u)    echo "-u was triggered, Parameter: $OPTARG" >&2;;
    f)    echo "-u was triggered, Parameter: $OPTARG" >&2;;
    p)    echo "-u was triggered, Parameter: $OPTARG" >&2;;
    \?)    echo "Invalid option: -$OPTARG" >&2;exit 1;;
    *)    echo "Option -$OPTARG requires an argument." >&2;exit 1;;

Now I need to see if user requested one of the options -c or -u. it is necessary for the code.
if statement like the following wouldn't work:
if [ $opt != "c" ] then
    echo "./script: Must select : -u or -c"
    exit 1

# 2  
while getopts ":c:u:fp" opt; do
  case $opt in
    c)    ctrue=Y && echo "-c was triggered, Parameter: $OPTARG" >&2;;
    u)    utrue=Y && echo "-u was triggered, Parameter: $OPTARG" >&2;;
    f)    echo "-f was triggered, Parameter: $OPTARG" >&2;;
    p)    echo "-p was triggered, Parameter: $OPTARG" >&2;;
    \?)    echo "Invalid option: -$OPTARG" >&2;exit 1;;
    *)    echo "Option -$OPTARG requires an argument." >&2;exit 1;;
# if C was entered or U was entered (and both cannot be Y or N ) this cannot be true
if [ "$utrue" = "$ctrue" ]; then
  echo 'bad parameters'
  echo 'required parameter entered'

Made one or two other minor changes in the echo statements.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
I have this:
# Argument = -t test -r server -p password -v usage()
  cat << EOF
usage: $0 options
 This script run the test1 or test2 over a machine.
    -h        Show this message
    -t        Test type, can be ‘test1′ or ‘test2′
    -r        Server address
    -p        Server root password
    -v        Verbose
while getopts “ht:r:p:v” OPTION
      case $OPTION in
                  exit 1
 if [[ -z $TEST ]] || [[ -z $SERVER ]] || [[ -z $PASSWD ]]
      exit 1

when I run any other options it gives me the message:
./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..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #722
Difficulty: Medium
Konrad Zuse built the first digital freely programmable computer, the Z1.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How-to enforce check on getopts command

here is my script that expects the user to run it like ./ -f /tmp/log.txt more #!/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

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

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

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

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

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

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

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

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

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

Featured Tech Videos