0

ok.. this is the deal. i have a bash script like the following. the script is only to show you what i mean .. it may look strang... but is exactly what i need.:

#/bin/bash runcommand () { message="$1" shift echo "$message" $@ > /tmp/logfile if [ $? -gt 0 ]; then cat /tmp/logfile fi } runcommandwrapper () { wrapperoptions="$1" shift $* } rm -f /tmp/test ; rm -f /tmp/logfile runcommand "echo into file" echo "SUCCESS" > /tmp/test echo "-----------------" echo "test file:" echo "-----------------" cat /tmp/test echo "-----------------" echo echo "-----------------" echo "logfile file:" echo "-----------------" cat /tmp/logfile echo "-----------------" echo echo echo rm -f /tmp/test ; rm -f /tmp/logfile runcommand "echo into file" 'echo "SUCCESS" > /tmp/test' echo "-----------------" echo "test file:" echo "-----------------" cat /tmp/test echo "-----------------" echo echo "-----------------" echo "logfile file:" echo "-----------------" cat /tmp/logfile echo "-----------------" echo 

this works

runcommand "running command mount" mount 

this does not work

runcommand "running command fdisk" fdisk > /tmp/fdiskoutput 

in this case the text in quotation marks is not treated as a whole argument within the wrapper script. try it, you'll see what I mean. --> SOLVED

So Running the above script returns:

----------------- test file: ----------------- echo into file ----------------- ----------------- logfile file: ----------------- SUCCESS ----------------- echo into file ----------------- test file: ----------------- cat: /tmp/test: No such file or directory ----------------- ----------------- logfile file: ----------------- "SUCCESS" > /tmp/test ----------------- 

but the expected outcome is:

----------------- test file: ----------------- SUCCESS ----------------- ----------------- logfile file: ----------------- ----------------- echo into file ----------------- test file: ----------------- SUCCESS ----------------- ----------------- logfile file: ----------------- ----------------- 

how can i pass commands with redirection or pipe lining as a command to another function in bash ?

and help and tips would be very much appreciated! I have no idea how to get this working, or if this is possible at all?

1
  • Please describe how it "does not work". Is there an error message? What is the output and behaviour and how is it different from what you expect? Commented May 29, 2012 at 6:10

1 Answer 1

1

From the bash manpage:

 * Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. <snip> @ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... <snip> 

You therefore need to use "$@" (including the double quotes) instead of $* since you want to retain the original parameter quoting.

3
  • thanks... makes all sense... i'll give it a try.. as soon as possible! will this also solve my redirection (or piping) problem of commands sent to the runcommand function? Commented May 29, 2012 at 5:36
  • @derelict I don't understand the redirection problem, please edit your question and add more detail (preferably with an example). Commented May 29, 2012 at 5:43
  • see my second and/or third example of the function invocation. if i pass a command with stdout redirection ">" or piping "|" to the runcommand function, the function fails... hope this makes sense to you this way? Commented May 29, 2012 at 6:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.