Linux – pass command with stdout to a bash function

bashlinuxscripting

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?

Best Answer

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.