bash - shell script: running cmd that was passed into a function -


i have bash script:

function run_cmd {     cmd="$@"     echo ">>> $cmd"     exec $cmd }  clusters_pkg="abc1, abc2" # run command run_cmd "package upload -c '$clusters_pkg'" 

however, when run command, usage error 'package' command. if run command copy+paste, works fine.

it doesn't seem me passing in $clusters_pkg variable spaces , quotes around it. how run "everything" passed run_cmd w/o shell clobbering things?

be sure read the link posted adrian frühwirth. answer short summary of advice found there.


if pass argument this:

run_cmd "package upload -c '$clusters_pkg'" 

then you'll have use eval in function single quotes treated quoting operators:

run_cmd () {     cmd="$@"     echo ">>> $cmd"     eval "$cmd" } 

if pass arguments this:

run_cmd package upload -c "$clusters_pkg" 

then can use array in function (much safer using eval):

run_cmd () {     cmd=( "$@" )     echo ">>> ${cmd[@]}"     "${cmd[@]}" } 

you don't need use exec; in fact, don't, causes current process replaced cmd: won't return shell called run_cmd after $cmd exits.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -