Reputation: 95
Script should pass list of values to the argument and should validate if there is one argument is passed and no list. for example ./script --arg hi script should do the --arg command and add/delete hi
./script --arg "hi how are you " in this case no of arguments passed to arg how to give exception or through error if user enter above values to arg1.
function test() {
filename=$1
echo $filename
case "$2" in
a)
echo $3 >> $filename
echo "add "
# cat $filename
shift
shift
;;
exit
}
test $fileName $3 $4
Upvotes: 1
Views: 1835
Reputation: 1270
argsCount here will do the trick if you want to have a check on the number of the arguments passed. In the below example, I am passing 3 arguments and validation whether the arguments counts is equal to 3, if not it will exit from the script.
#!/usr/bin/env bash
set -ex
set -o pipefail
copyConfigFrom=$1
hostConfigFileName=$2
hostnameEmail=$3
argsCount="$#"
if [ "$argsCount" -ne 3 ]; then
echo "Usage: $0 copyConfigFrom hostConfigFileName hostnameEmail"
exit 1
fi
Upvotes: 1