I'm trying to make a script with the following usage:
bash script.sh [-a] databases ...
The script then populates the array $databases with the arguments from 'databases'
databases=("$@")
Now I'm trying to implement the option -a
which if set should declare att=1
:
while getopts "a" o; do
case "${o}" in
a)
att=1
;;
esac
done
Now I'm facing the problem that when executing the script -a
also gets put into the array.
I can't use read
for the array because I don't have the option to enter additional arguments after the script gets executed as the script will be automated with a RMM and that only lets you enter addition arguments when creating a job.
CodePudding user response:
You have to use shift
in case
block before you populate your variable:
while getopts "a" o; do
case $o in
a)
att=1
shift
;;
esac
done
databases=("$@")
declare -p databases att
Then run it like this:
bash script.sh -a DB1 DB2 DB3
Output:
declare -a databases=([0]="DB1" [1]="DB2" [2]="DB3")
declare -- att="1"