bash:getopts command help | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

bash:getopts command help

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-30-2012
bashily bashily is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 25 March 2013, 10:15 PM EDT
Posts: 48
Thanks: 14
Thanked 0 Times in 0 Posts
bash:getopts command help

How can I say one of the options is required? can I use an if statement?
let say:

Code:
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;;
  esac
done

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:

Code:
if [ $opt != "c" ] then
    echo "./script: Must select : -u or -c"
    exit 1
fi

Sponsored Links
    #2  
Old 03-30-2012
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 October 2014, 8:08 AM EDT
Location: NM
Posts: 10,237
Thanks: 281
Thanked 802 Times in 746 Posts

Code:
ctrue=N
utrue=N
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;;
  esac
done
# 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'
else
  echo 'required parameter entered'
fi

Made one or two other minor changes in the echo statements.
The Following User Says Thank You to jim mcnamara For This Useful Post:
bashily (03-31-2012)
Sponsored Links
    #3  
Old 03-31-2012
bashily bashily is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 25 March 2013, 10:15 PM EDT
Posts: 48
Thanks: 14
Thanked 0 Times in 0 Posts
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 09:27 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
using getopts to parse a command line nano2 Shell Programming and Scripting 1 04-26-2011 04:10 PM
Help with getopts command tonsat Shell Programming and Scripting 7 11-13-2010 10:56 AM
[bash] getopts not executing when called second time. ASGR Shell Programming and Scripting 2 02-18-2010 12:26 PM
getopts - command line arguments IceX UNIX for Dummies Questions & Answers 2 09-04-2009 04:03 PM
File handling, getopts command in unix bab123 Shell Programming and Scripting 2 07-01-2008 10:41 AM



All times are GMT -4. The time now is 09:05 AM.