Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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? –  mgorven May 29 '12 at 6:10
add comment

1 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.

share|improve this answer
    
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? –  derelict May 29 '12 at 5:36
    
@derelict I don't understand the redirection problem, please edit your question and add more detail (preferably with an example). –  mgorven May 29 '12 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? –  derelict May 29 '12 at 6:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.