Tuesday, January 1, 2013

Check minimum Command Line Parameters in Shell Script

The below code snippet shows how can check whether the minimum number for paramters are passed to the shell script or not.

-bash-3.2$ cat check_min_params
MINPARAMS=10
echo "All the command-line parameters are: "$*""
if [ $# -lt "$MINPARAMS" ]
then
  echo
  echo "This script needs at least $MINPARAMS command-line arguments!"
fi
echo
exit 0


OUTPUT :

-bash-3.2$ sh check_min_paramsAll the command-line parameters are:
This script needs at least 10 command-line arguments!
-bash-3.2$ sh check_min_params p1 p2 p3All the command-line parameters are: p1 p2 p3
This script needs at least 10 command-line arguments!
-bash-3.2$ sh check_min_params p1 p2 p3 p4 p5 p6 p7 p8 p9 p10All the command-line parameters are: p1 p2 p3 p4 p5 p6 p7 p8 p9 p10

$# --> Total number of parameter passed from shell script command line to the program.

No comments:

Post a Comment

Price List Query for Item

 SELECT qph.list_header_id,        qph.name,        qph.description,        qphh.start_date_active,        qphh.currency_code,        q...