last day (15 days later) » 

19:18
0
Q: bash comparison of integer always returns true?

r3wtI have the following simple bash script. for some reason it always returns true. #!/bin/bash service=hhvm status=($(ps -ef | grep -v grep | grep $service | wc -l)) if [[ $status -eq 0 ]]; then echo "starting $service" service hhvm start else echo "$service running" fi

Might want to put your pure coding questions onto Stack Overflow nextime.
just remove spaces around =. change foo = 3 --> foo=3 and run your script with bash /path/to/script
the real var is status=$(ps -ef | grep -v grep | grep $service | wc -l)
@ThomasW. unfortunately, i have been banned.
@r3wt i don't think downvotes get you banned unless you're a serial downvoter - take it up with their moderators, then. In either case, pure coding questions belong on Stack Overflow.
@ThomasW. no the first question i asked got a ton of down votes, so my account is more or less teetering on the edge. every down vote my account becomes banned. every couple hundred points it gets unbanned. its stupid.
19:18
remove outer parentheses from status=($(ps -ef | grep -v grep | grep $service | wc -l))
@KasiyA already tried it. out put of the call is 3 if service running, 0 if stopped, but always says that its running.
How do you run the script? with sh /path/to or bash /path/to? and what "out of call" is there? and what does mean grep -v grep there?
i run it with ./script.sh
@KasiyA grep -v grep will do the "--invert-match", exclude the grep command from the ps output.
@P.-H.Lin I know that, but according to what pattern? "grep" is patten there?
19:18
@KasiyA I guess so (this is a new finding to me), if you try ps aux| grep lightdm, the grep command itself will generate a process, creating a false-positive result.
@r3wt change status=($(ps -ef | grep -v grep | grep $service | wc -l)) to status=$(ps -ef | grep $service | wc -l). P.-H.Lin your guess is true.
KasiyA, again that's not the problem. the call returns 3, which it is supposed to. even after updating as you suggested, the script still doesn't work as expected.i appreciate your attempts, but no need to try any further. it is my problem i'll try to figure it out eventually.
@r3wt Which call? I'm stuck on it. the status=($(ps -ef | grep -v grep | grep $service | wc -l)) line is completely invalid if you want to save the integer number in it.
@KasiyA the line you just asked me to change. the output returns 3 if the service is running, 0 if not.
Run alone service=hhvm; ps -ef | grep -v grep | grep $service | wc -l in terminal and now what do you see? 3 , 4 ,0 ? what? please reply.
19:18
@KasiyA 1 is what i see.
And when service=hhvm; ps -ef | grep $service | wc -l
2 27 248 - after that command
Ok. last question: please post the results of service=hhvm; ps -ef | grep -v grep | grep $service | wc -l when hhvm service is running and when it's not running
@KasiyA 1 when running, 0 when not. please bear in mind, there may be more than 1 running at any given time, so long as atleast 1 is running the check should pass.
This is silly question but what is the output of if [[ 3 == 0 ]]; then echo "true" ; fi and if [[ 3 -eq 0 ]]; then echo "true" ; fi

last day (15 days later) »