I have two scripts, in which script1 takes pre-fixed arguments as below(I'm using case, shift function to shift arguments and assign them as required) This is how I call script1 normally:
script1 --env [env] --db [db] --table [table] --location [location]
This is how I iterate through arguments in script1:
while [ "$#" -gt 0 ]
do
case "$1" in
--env | --environment)
shift
envie="$1"
;;
--db)
shift
db="$1"
;;
--table)
shift
table="$1"
;;
--tableLocation)
shift
tableLoc="$1"
;;
esac
shift
done
My second script calls the above script along with the parameters(dynamic) and I have trouble doing it. I'm trying it this way now. I'd like to invoke script1 and would like to consider the status if a table has been created or not( I think a return code would help here).
env=env1
db=database1
table=table1
tablelocation=tablelocation1
#call script1
`source script1 --env $env --db $db --table $table1 --tableLocation $tablelocation`
I'm seeing "command not found" error for this line(there are no syntax errors) Please let me know if there is a way to call script1 with prefixed arguments.
You do not need back ticks.
env=env1
db=database1
table=table1
tablelocation=tablelocation1
#call script1
script1 --env "$env" --db "$db" --table "$table1" --tableLocation "$tablelocation"
You use back ticks only, if you want to save the result.
Make sure that script1 is executable ( chmod +x
) and the directory is your PATH
.
Your script only processes the first argument/value pair. You need to loop while there are still arguments left, eg something like:
while [[ $# -ge 1 ]]; do
case "$1" in
--env | --environment)
shift
envie="$1"
;;
--db)
shift
db="$1"
;;
--table)
shift
table="$1"
;;
--tableLocation)
shift
tableLoc="$1"
;;
*)
echo "invalid argument: $1"
exit 1
;;
esac
shift # shift out the consumed value
done
If you want to reuse your initial arguments, store them in an array before shifting them off:
initialArgs=( "$@" )
while [ "$#" -gt 0 ]; do
: ...processing here...
shift
done
source script1 "${initialArgs[@]}"
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.