LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-23-2006, 11:06 PM   #1
ralvez
Member
 
Registered: Oct 2003
Location: Canada
Distribution: ArchLinux && Slackware 10.1
Posts: 298

Rep: Reputation: 30
Question Shell script --cannot assign variable--


I'm trying to write a simple script that monitors if a program is already running and if it is it does not initiate another instance but if it is not then it runs.
Here is the code:

<code>
#!/bin/bash

# If program is running $RUNNING_INSTANCE = 1

RUNNING_INSTANCE= ps -A | grep -c 'xbindkeys';


function run(){
xbindkeys;
}


if [ "$RUNNING_INSTANCE" -eq 0 ]; then
echo "Running: $RUNNING_INSTANCE"
run;
else
echo "Running: $RUNNING_INSTANCE";
fi
</code>

Now ... this part works s -A | grep -c 'xbindkeys' but RUNNING_INSTANCE does not take the value. It does not matter what I do I cannot assign the value to it.
Does anyone have an idea where I'm going wrong? I'm not a bash programmer but it occurs to me that that should work.

Ideas ??

Thanks.

Rick
 
Old 02-24-2006, 12:24 AM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
I haven't tried your script (or even read all of it, sorry) - but if it's a copy/paste of your actual script there's a problem with:
Code:
RUNNING_INSTANCE= ps -A | grep -c 'xbindkeys';
Does the following work?
Code:
RUNNING_INSTANCE=`ps -A | grep -c 'xbindkeys';`
 
Old 02-24-2006, 03:23 AM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 77
BASH scripts do not need lines to be terminated with semicolons.
Only lines with two separate commands need them. It's not Perl.
 
Old 02-24-2006, 06:20 AM   #4
ralvez
Member
 
Registered: Oct 2003
Location: Canada
Distribution: ArchLinux && Slackware 10.1
Posts: 298

Original Poster
Rep: Reputation: 30
gilead,
It does not work even with that. I tryed using the `` quotation (I so them in a book) but no luck I get the following error:
<code>
[ralvez@dragonfly ~]$ ./keyboard
./keyboard: line 3: 2: command not found <-- this shows (the number 2) that now I have two instances of the program
./keyboard: line 10: [: : integer expression expected <-- this line tells me that the assignment did not happen
Not Running: <-- and this tells me the the script just "run thrugh"
</code>

Any other ideas?
 
Old 02-24-2006, 06:56 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,452
Blog Entries: 54

Rep: Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895
I tryed using the `` quotation
I always use "$()". Makes for better reading. For me, that is.
About the script. I prefer "pgrep" over any ps|grep combo. It allows more fine-grained control over what you're looking for and you save one command. I also prefer using arrays because they make this type of checking so much easier. Also make good use of variables and exit codes. It will save you typing in the end.
About "debugging", if you don't see what a script is doing you want to run it as "sh -x script" or do "set -x" on the cmdline first. If you *know* there's an error you can "set -e" and have it stop right after the error occurs.

Code:
#!/bin/bash
BIN=/path/to/xbindkeys
function run() { $BIN; }
PIDS=($(pgrep -d " "  -f "$BIN"))
if [ "${#PIDS[@]}" = "0" ]; then
 echo "No running ${BIN}s"
 run &
else
 echo "PIDs: ${PIDS[@]}"
fi
exit 0
or, simpler if you don't need to report PIDs:

Code:
#!/bin/bash
BIN=/path/to/xbindkeys
pgrep -d " "  -f "$BIN" 2>&1>/dev/null  &&  \
echo "$BIN is running." || { echo "No running ${BIN}s"; $BIN; }
exit 0

Last edited by unSpawn; 02-24-2006 at 06:58 AM.
 
Old 02-24-2006, 12:59 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
Quote:
Originally Posted by unSpawn
About the script. I prefer "pgrep" over any ps|grep combo
Thanks for the tip - I hadn't considered that.
Quote:
Originally Posted by bulliver
BASH scripts do not need lines to be terminated with semicolons.
Only lines with two separate commands need them. It's not Perl.
Hi Bulliver,
I'm assuming that you meant to post this in http://www.linuxquestions.org/questi...d.php?t=418795. Yes I realise the code he posted was bash syntax (so was the code I gave him). But the error he posted back was perl and it was one I'd had a while back. (see man perldiag and search for bareword). Here is the entry:
Code:
Bareword "%s" not allowed while "strict subs" in use
           (F) With "strict subs" in use, a bareword is only allowed as a subroutine identifier, in curly
           brackets or to the left of the "=>" symbol.  Perhaps you need to predeclare a subroutine?
Since I can't find that error anywhere in bash man pages or in google searches, it therefore looked like the code had been put into a perl script (perhaps as a wrapper for debugging? beats me) - hence the comment about perl.
 
Old 02-24-2006, 04:56 PM   #7
ralvez
Member
 
Registered: Oct 2003
Location: Canada
Distribution: ArchLinux && Slackware 10.1
Posts: 298

Original Poster
Rep: Reputation: 30
unSpawn et all,
Thank you very much!!
I figured it out. I've been programming in about 9 different languages over the years but I've never ( I do not know why ) did much with bash.
I assumed that variables would have global scope by default and I guess that was the reason things did not work.
Here is the working code:
<code>

#!/bin/bash

function chkInstance(){
INST=`ps -A | grep -c xbindkeys`
if [ "$INST" == 0 ]; then
xbindkeys
else
:
fi
return $INST;
}

chkInstance

</code>

The logic is simple: if $INST gets a 0 then there are no instances of xbindkeys if it gets 1 then it is running. I considered getting the PID, like you unSpawn but then figured it would be easier if I used the number of running instances so I would get an small integer (0/1).

Once again thanks.

Rick
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script and Dynamic variable xanthium Programming 11 07-12-2011 06:05 AM
Shell Script Random Variable Daniel Programming 14 12-03-2007 05:00 AM
expanding variable in shell script dipenchaudhary Programming 8 02-08-2006 05:05 PM
Help with shell script, can't show variable stormrider_may Programming 5 01-27-2006 05:22 PM
how to assign password for a user in shell script mtest Programming 10 10-29-2003 06:52 AM


All times are GMT -5. The time now is 12:33 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