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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getopts and numeric parameters 1

Status
Not open for further replies.

Einstein47

Programmer
Nov 29, 2001
737
US
I am writing a script that will take a number of optional parameters including positive and negative numbers. I have used getopts before for parsing a parameter list, but it doesn't seem to be able to handle numbers (especially negative numbers) as parameters.

Like this:
script -5 (will display the last 5 lines of the log)
script 5 (will display all entries that have happened in the last 5 days)
script -5 -f FileName (will display the last 5 lines using the input file of FileName)

When I use getopts it doesn't like the -5 (Invalid parameter). I would like to make the script robust enough to handle this:
script -f filename -5
but I don't think that is possible with getopts.

I'm using AIX and ksh - if that helps.
Einstein47
("The only reason I would take up exercise would be so I could hear heavy breathing again.")
 

I can't find anything in the man pages for this. Have you tried using quotes either on the command line or in your script?

 
as an update - I have a work-around.

If you start your getopts parameter list with : (colon) Then the anything that doesn't match the parameter list will be read as a value for the item named ? - so I am able to effectively ignore what getopts "thinks" is a parameter. Just incase you're wondering, here is my whole script (the names have been changed ... yadda yadda)
Code:
cd /usr/HTTPServer/logs
inputFile=/usr/HTTPServer/logs/sslaccess_log
awkFile=/usr/HTTPServer/logs/who3.awk
parms=""

if [ $# -gt 0 ];then
  one=${1}
  if [ $one -gt 0 ];then
    strDay=$(date +%d)
    (( tmpDay = strDay - one + 1 ))
    if [ tmpDay -lt 10 ];then
      tmpDay="0$tmpDay"
    fi
    one="[$(date '+%Y.%m').${tmpDay}"
  fi
  displayLines="-v startVal=${one}"
fi

while getopts ":hsf:" Arg; do
  case $Arg in
    h) echo "Usage ${0##*/} [ # ][ -h ][ -s ][ -f filename ]"; exit 0;;
    s) graph="-v graph=1";;
    f) inputFile=$OPTARG ;;
    ?) ;;
  esac
done
shift $(( OPTIND - 1 ))

parms="$graph $displayLines"

if [ -f $inputFile ];then
  awk -f ${awkFile} ${parms} ${inputFile}
else
  echo "ERROR: $inputFile not found"
fi
Einstein47
("The only reason I would take up exercise would be so I could hear heavy breathing again.")
 
Hi Einstein:

It's nice to see you in the forums again. It's been awhile.

I don't have anything to add to your particular getopts problem other than to say I've abandomed using getopts. Since I like using variable=value, I'm using a ksh function called my_getopts given to me by a friend, Michael Wang:

function my_getopts {
#- version 3.141592, 2002-02-02, Michael Wang
typeset PATH= SEP= i
typeset -u _I
for i; do
_I=$i
eval ${_I%%?:))=*}= unset ${_I%%?:))=*}
case $_I in
*[!:]=*) eval $(IFS=$SEP; print ${_I%%=*}=\"${_I#*=}\") ;;
*:=*) eval $(IFS=$SEP; print ${_I%%:=*}=\"${i#*:=}\") ;;
esac
done
}

Here's a test program, my_prog.ss:

# my_prog.ss
# insert the my_getopts function or set FPATH to find the function.
my_getopts "$@" # process the command line options.

print "SID variable: $SID"
print "OWNER variable: $OWNER"
print "EMAIL variable: $EMAIL"
# end my_prog.ss

Executing my_prog.ss

my_prog.ss sid:=MySID owner=MySelf email:="a B"

yields:

SID variable: MySID
OWNER variable: MYSELF
EMAIL variable: a B

Michael upshifts all the variable names, but that's easily rectified if you wish.

Regards,

Ed
 
Interesting function - I can honestly say that it is over my head. I will keep it - if your friend Michael Wang doesn't mind - maybe someday I will understand it enough to use it.

For what I'm doing, my workaround will fill my needs. I have transitioned from SysAdmin to Java Programmer. So I don't have much reason to do shell scripting anymore.

I have missed the forums - it is good to know the feeling has been mutual. Einstein47
("The only reason I would take up exercise would be so I could hear heavy breathing again.")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top