Function not called when no arguments is passed

Hi Guys,

I am trying to pass arguments to the script i am wrinting.
When no argument is passed or wrong argument is passed, the script needs to output the way it needs to be called and exit.

Currently, when no arguments is passed, it is not getting exited but goes on assuming those variables to be blank.

The below is my code.

# Help message
#********************************************************************
usage() {
  print "Usage: $0 -i INP_FILE -o RPUT_FILE"
  print "FTPing file to Mainframe"
  print "  -i INP_FILE    - file to be ftped"
  print "  -p RPUT_FILE   - Remote File Name"
}
 
while getopts i:o: c
do
  case $c in
    i) INP_FILE=$OPTARG
       ;;
    o) RPUT_FILE=$OPTARG
       ;;
    ?) usage
       exit
       ;;
  esac
done
shift `expr $OPTIND - 1`

Can somebody point out what i am doing wrong..

Cheers!!!!

You could check the number of arguments in the beginning of your script.
You appear to be using some variant of the Korn Shell (or the Z Shell),
so this should work:

#!/bin/ksh
# Help message
#********************************************************************
usage() {
  print "Usage: $0 -i INP_FILE -o RPUT_FILE"
  print "FTPing file to Mainframe"
  print "  -i INP_FILE    - file to be ftped"
  print "  -p RPUT_FILE   - Remote File Name"
}
 
(( $# )) || {
  usage
  exit
  }

while getopts :i:o: c; do
  case $c in
    i) INP_FILE=$OPTARG
       ;;
    o) RPUT_FILE=$OPTARG
       ;;
    ?) usage
       exit
       ;;
  esac
done
shift $(( OPTIND - 1 )) 

P.S. I suppose you may need to add the -p option to the getopts string..
P.P.S. I added the initial colon in order to avoid the unknown option error.

Try this..

#!/bin/ksh
# Help message
#********************************************************************
usage() {
  print "Usage: $0 -i INP_FILE -o RPUIT_FILE"
  print "FTPing file to Mainframe"
  print "  -i INP_FILE    - file to be ftped"
  print "  -o RPUT_FILE   - Remote File Name"
  exit 1
  }
while getopts i:o: c
do
  case $c in
    i) INP_FILE=$OPTARG
       first=$c
       ;;
    o) RPUT_FILE=$OPTARG
       second=$c
       ;;
    ?) usage
       exit
       ;;
  esac
done
shift `expr $OPTIND - 2`
 
 if [ -z "$INP_FILE" ] && [ -z "$RPUT_FILE" ] ; then
  echo "Cause : first and second parameter are missing!!\n " ; usage
 elif [ -z "$RPUT_FILE" ] ; then
  echo "Cause : second parameter is missing!!\n " ; usage
 elif [ -z "$INP_FILE" ] ; then
  echo "Cause : first parameter is missing!!\n " ; usage
 fi
 
if [ "$first" != "i" ] &&  [ "$second" != "o" ]; then
  echo "Cause : first and second parameter are wrong!! \n" ; usage
 elif [ "$second" != "o" ] ; then
   echo "Cause : second parameter is wrong!! \n" ; usage
 elif [ "$first" != "i" ] ; then
  echo "Cause : first parameter is wrong!! \n" ; usage
 fi
 
if [ $# -ne 1 ] ; then echo "Cause : Arguments are exceed !! \n" ; usage ; fi

regards
ygemici

1 Like

Thanks Guys,,,

I have used the IF condition to check there are 4 arguments passed before the getopts,, as well as checking $OPTIND - 2 did it all....

I got what i needed.. Thanks Once again.. :slight_smile: