Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing shell script which takes flags as input 3

Status
Not open for further replies.

bansalhimanshu

Programmer
Sep 27, 2004
36
0
0
US
I want to write shell script which takes some flags along with input parameters. What I mean is like ls which can take input as parameters filename/file directory alongwith flags like -l, -m, -a etc.

ls -latr *.txt

This way I want to write my script as

myscript -w myinput

Also do correct me if these characters with hyphen (-) are not called flags but something else.
 
man getopts

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
getopts is one way, but if it's only the one flag -w then:

Code:
...

wflag=no
if [ $1 = "-w" ]
then
 wflag=yes
 shift # drop -w from beginning of arg list
fi

inputfile=$1

if [ ${wflag} = 'yes' ]
then
  ...
  process input with -w flag
  ...
else
  ...
  process input without -w flag
  ...
fi

...


HTH,

p5wizard
 
Hi,

For the name, we call them usually switches

p5wizard, what about calling your script this way ( I know, you have worked hard 5 consecutive days!!! )

Code:
myscript -z myfile

$1 is not -w
so the file processed will be -z which is not afile.

the advantage of getopt ( suggested by PHV) is that it checks the validity of a switch.



 
Check out all of these, if you have them:[tt]
man -k getopt
getopt (1) - parse command options (enhanced)
getopts (1p) - parse utility options
getopt_long_only (3) - Parse command-line options
getopt_long (3) - Parse command-line options
getopts (1) [bashbuiltins] - bash built-in commands, see bash(1)
getopt (3p) - command option parsing
getopt (3) - Parse command-line options[/tt]

aau said:
For the name, we call them usually switches
... or arguments, or parameters, or flags, or options (hence the name [tt]getopts[/tt])

--
-- Ghodmode
 
And still I get a star? Must have given bansalhimanshu some ideas...

Yes, Ghodmode, you are right, I humbly agree... getopt(s) is probably the most advantageous command to check validity, it's just that I just hardly ever use it/them.


So here's a framework without getopt(s) for processing one optional flag -w and one parameter (inputfile)

Code:
#!/bin/ksh
# # #
# put comments here about usage
# script_name [ -w ] inputfile
# and exitcodes
#  1 - invalid flags
#  2 - can't open file
# #
# initially assume no wflag
WFLAG=no
# #
# error message if invalid options
USAGE="Usage: $0 [ -w ] inputfile"
# check number of parameters - must be one or two
if [ $# -eq 1 ]
then
 IFILE=$1
elif [ $# -eq 2 ]
then
 if [ "$1" = '-w' ]
 then
  WFLAG=yes
  IFILE=$2
 else
  echo ${USAGE}
  exit 1
 fi
else
 # 0 params or more than 2
 echo ${USAGE}
 exit 1
fi
# # 
# check validity of input file name (rudimentary check only)
if [ ! -r ${IFILE} ]
then
 echo "Can't open file ${IFILE}"
 exit 2
fi
# # 
# process file
if [ "${WFLAG}" = 'yes' ]
then
 ...
 process with -w flag
 ...
else
 ...
 process without -w flag
fi


same script with getopts (which is a ksh construct):

Code:
#!/bin/ksh
# # #
# put comments here about usage
# script_name [ -w ] inputfile
# and exitcodes
#  1 - invalid flags
#  2 - can't open file
# #
# initially assume no wflag
WFLAG=no
# #
# error message if invalid options
USAGE="Usage: $0 [ -w ] inputfile"
# #
# check validity of options specified
while getopts w OPT
do
 case ${OPT} in
  w) WFLAG=yes
     ;;
  ?) echo ${USAGE}
     exit 1
     ;;
 esac
done
# #
# shift past optional flag
shift $(($OPTIND -1))
# #
# should be one parameter left
if [ $# -ne 1 ]
then
 echo ${USAGE}
 exit 1
else
 IFILE=$1
fi
# # 
# check validity of input file name (rudimentary check only)
if [ ! -f ${IFILE} ]
then
 echo "Can't open file ${IFILE}"
 exit 2
fi
# # 
# process file
if [ "${WFLAG}" = 'yes' ]
then
 ...
 process with -w flag
 ...
else
 ...
 process without -w flag
 ...
fi

and with getopt (will run also in bourne shell):

Code:
#!/bin/bsh
# # #
# put comments here about usage
# script_name [ -w ] inputfile
# and exitcodes
#  1 - invalid flags
#  2 - can't open file
# #
# initially assume no wflag
WFLAG=no
# #
# error message if invalid options
USAGE="Usage: $0 [ -w ] inputfile"
# #
# check validity of options specified
# suppress default err msg from getopt
set -- `getopt w $* 2>/dev/null`
# #
# check if getopt found invalid flags
if [ $? -eq 0 ]
then
 #
 echo ${USAGE}
 exit 1
fi
while [ $1 != '--' ]
do
 case $1 in
  -w) WFLAG=yes
      shift
      ;;
 esac
done
# #
# shift past '--'
shift
# #
# should be one parameter left
if [ $# -ne 1 ]
then
 echo ${USAGE}
 exit 1
else
 IFILE=$1
fi
# # 
# check validity of input file name (rudimentary check only)
if [ ! -f ${IFILE} ]
then
 echo "Can't open file ${IFILE}"
 exit 2
fi
# # 
# process file
if [ "${WFLAG}" = 'yes' ]
then
 ...
 process with -w flag
 ...
else
 ...
 process without -w flag
 ...
fi


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top