LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   passing commands as arguments to functions in bash script (https://www.linuxquestions.org/questions/linux-general-1/passing-commands-as-arguments-to-functions-in-bash-script-811313/)

mastahyeti 05-31-2010 12:10 PM

passing commands as arguments to functions in bash script
 
Hello,
I wrote a simple bash script to let me treat any set of programs like a deamon. For example if I configure the script a certain way I can start/stop/get the status of apache, mysql and php all from one command. I am having a bit of a problem though. I am passing commands as strings to a function and then depending on the arguments to the script it might run one of these commands or another. Some of these commands need to be run in the background though, such as deluge-web. When I send "deluge-web &" to the function and it execute it deluge-web does not start in the background. I can't figure out why this is. I have tried escaping the & with ''s and with a \, but nothing seems to work. I know that this is some idiotic thing that I am overlooking, but I am a bit stumped. Here is the script configured to start/stop/get status of deluged and deluge-web. Thanks for your help.

Code:

#!/bin/bash

function checkanddosomething {
        if [ $6 == "stop" ]
                then
                        expression="$3"
                        todo="$5"
                else
                        if [ $6 == "start" ]
                                then
                                        expression="$2"
                                        todo="$4"
                                else
                                        expression="[[:alnum:]]"
                                        todo="true"
                        fi
        fi

        $1 | grep "$expression" > /tmp/testvar
        cat /tmp/testvar
        if [ -s /tmp/testvar ]
                then
                        true
                else
                        $todo
        fi
}

function ddtest {
        ps -e | grep deluged > /tmp/ddrunning
        if [ -s /tmp/ddrunning ]
        then
                echo "Deluged is running"
        else
                echo "Deluged is stopped"
        fi
}
function dwtest {
        ps -e | grep deluge-web > /tmp/dwrunning
        if [ -s /tmp/dwrunning ]
        then
                echo "Deluge-web is running"
        else
                echo "Deluge-web is stopped"
        fi
}


A1='ddtest'
A2="is running"
A3='stopped'
A4="deluged"
A5="killall deluged"
checkanddosomething "$A1" "$A2" "$A3" "$A4" "$A5" "$1"

M1='dwtest'
M2='is running'
M3='stopped'
M4="deluge-web &"
M5="killall deluge-web"
checkanddosomething "$M1" "$M2" "$M3" "$M4" "$M5" "$1"


Tischbein 05-31-2010 12:58 PM

Throwing in an eval works:

sage@mercury:~$ echo boo > 'a b'
sage@mercury:~$ x="gedit 'a b' &"
sage@mercury:~$ eval $x
[1] 7944 <-- launched in background
sage@mercury:~$

Robhogg 05-31-2010 12:59 PM

Have you tried your commands on the command line? This is what I got for a little test using find:

Code:

rob:~$ arg="find . -name a.out &"
rob:~$ $arg
find: paths must precede expression: &
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
rob:~$ find . -name a.out &
[1] 9886
rob:~$ ./scripts/C/CPL/Chapter_1/a.out
./scripts/C/a.out

[1]+  Done                    find . -name a.out
rob:~$ arg="find . -name a.out"
rob:~$ $arg &
[1] 9896
rob:~$ ./scripts/C/CPL/Chapter_1/a.out
./scripts/C/a.out

[1]+  Done                    $arg
rob:~$

This suggests that, when the ampersand is part of the statement assigned to the variable, it is being passed to find by the shell, rather than being interpreted separately.

Is there a need to pass the ampersand? Could you not add it to the line where the command contained in $todo is executed?:

Code:

$todo &

mastahyeti 05-31-2010 06:19 PM

Tischbein: Wonderful. This works great. Thanks!

Robhogg: You seem to be right about the way the ampersand works. I suppose that I don't really need the ampersand in the string. Thanks!


All times are GMT -5. The time now is 09:17 PM.