LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 05-31-2010, 12:10 PM   #1
mastahyeti
LQ Newbie
 
Registered: May 2010
Posts: 15

Rep: Reputation: 0
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"
 
Old 05-31-2010, 12:58 PM   #2
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
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:~$
 
Old 05-31-2010, 12:59 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
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 &
 
Old 05-31-2010, 06:19 PM   #4
mastahyeti
LQ Newbie
 
Registered: May 2010
Posts: 15

Original Poster
Rep: Reputation: 0
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers/Passing arguments to functions question (C) smoothdogg00 Programming 7 03-17-2006 01:46 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 08:24 AM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 05:13 AM
[bash-scripting]functions + arguments hylke Programming 14 10-05-2004 01:48 AM
Issue with recursive script passing arguments gauge73 Linux - Newbie 0 01-06-2004 07:16 PM


All times are GMT -5. The time now is 11:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration