简体   繁体   中英

how to pass list of values to shell argument in script

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM