bash - Passing multiple parameters to shell script and parsing them -
i trying run following program, need pass multiple options command executed. here example: giving inputs
/test.sh -s -n
script test.sh:
#! /bin/bash #set -x while [ $# -gt 0 ] case $1 in -s) service=$2 shift ;; -n) node=$3 command break ;; *) echo "invalid argument" break ;; esac done
using getopts
built-in command in bash:
#!/bin/bash service="default" node="default" while getopts 's:n:' opt; case $opt in s) service="$optarg" ;; n) node="$optarg" ;; *) exit 1 ;; esac done echo "service = '${service}'" echo "node = '${node}'"
testing it:
$ ./test.sh service = 'default' node = 'default' $ ./test.sh -s hello -n world service = 'hello' node = 'world' $ ./test.sh -n world -s hello service = 'hello' node = 'world' $ ./test.sh -e eh ./test.sh: illegal option -- e
Comments
Post a Comment