Bash Script Parameters Number Check

31 03 2009

A code snippet for the parameters number checking in bash scripting

#!/bin/bash
 
ARGS=2
E_BADARGS=65
 
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` <arg1> <arg2>"
exit $E_BADARGS
fi
 
ARG1=$1
ARG2=$2
 
command $ARG1 $ARG2
 
exit 0

it checks if parameter numbers isĀ  equal to ARGS, executing the command if true and exiting from script with error code 65 (Wrong Parameter Numbers) otherwise.


Actions

Information

Leave a comment