I wrote a bash script which takes parameters by name and read its value in key value pairs. Below is an example of how I run my script.
sh test.sh param1='a' param2='b'
I then refer the input arguments like $param1 and $param2 inside my script. Below is the script I wrote to do this.
for item in $@; do
case $item in
(*=*) eval $item;;
esac
done
But when there is space in the value of the argument, it takes only the part before the space.
param3='select * from'
would only assign 'select' to param3 and not the whole string. How can I achieve this? Any help would be much appreciated.
EDIT :
After Inian's answer
for item in $@; do
case "$item" in
(*=*) eval "$item";;
esac
done
You can do this by quoting "$@"
and using declare
instead of eval
:
for item in "$@"; do
case $item in
(*=*) declare "$item" ;;
esac
done
However, this is now a bash script, so you have to run it with bash test.sh
and not sh test.sh
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.