LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 05-11-2014, 04:51 PM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 93

Rep: Reputation: 0
Unhappy Shell Script Check for 2 lines Return?


Hi Guys

New to shell scripting..

I am trying to run a script which basically runs a command.. and if that command returns two lines then it does nothing.. but if it returns one line then it runs another command..

In other words, basically the script runs a command if it sees only one line return, if it sees more than one, do nothing.. (normally it will return only two lines)

I could capture the return status of the command but then it will always be true.. so that method won't work here..

How do I capture the fact that it has returned two lines and not one.. and so how do I modify this script below (I know it is wrong in itself in terms of rules)..

Code:
#!/bin/bash

ps -ef | grep script

if [ "$?" != "0" ]
run command
 
Old 05-11-2014, 04:58 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Code:
if [ $(ps -ef | grep script | wc -l) = 2 ]; then
    # Run command
fi
 
1 members found this post helpful.
Old 05-11-2014, 05:01 PM   #3
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rknichols View Post
Code:
if [ $(ps -ef | grep script | wc -l) = 2 ]; then
    # Run command
fi
DOH... didn't think of wc -l.. dumb of me .. so simple..

Thanks, will test it out
 
Old 05-11-2014, 05:06 PM   #4
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rknichols View Post
Code:
if [ $(ps -ef | grep script | wc -l) = 2 ]; then
    # Run command
fi
I am getting this error

Quote:
./check.sh: line 3: [: missing `]'
Code:
#!/bin/bash

if [ $(ps -ef | grep script | wc -l) = 1]; then
echo "sudo /etc/init.d/script restart"
fi
 
Old 05-11-2014, 05:08 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
That closing "]" needs to be a separate "word", preceded by a space.
 
Old 05-11-2014, 05:35 PM   #6
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rknichols View Post
That closing "]" needs to be a separate "word", preceded by a space.
Thanks .. was probably trying to do it in a hurry to fix an issue
 
Old 05-11-2014, 06:33 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
"a = b" is not a valid numeric comparison in bash. "a -eq b" is.

jlinkels
 
Old 05-11-2014, 07:08 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Are you checking for 2 lines because the ps returns the grep itself in addition to the script? Use pgrep, it's made to solve this exact problem:
Code:
if pgrep -f script ; then
  run command...
fi
 
1 members found this post helpful.
Old 05-11-2014, 08:22 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by jlinkels View Post
"a = b" is not a valid numeric comparison in bash. "a -eq b" is.

jlinkels
This is a place where testing two strings for equality is perfectly valid and is all that is needed.
 
Old 05-11-2014, 09:59 PM   #10
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Thanks guys

So for some reason the equality is not working for me..

If I do

if pgrep -f script ; then
fixcommand

How do i make it do the then statement when pgrep does not return something.. because since the script is running it should not do anything

Something like
 
Old 05-11-2014, 10:15 PM   #11
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
is this a good idea??

Code:
if [ -z `pgrep service` ] ; then
restart service
 
Old 05-12-2014, 03:56 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You seem to be over-complicating things a bit. The if is testing for success, so if pgrep returns something it will execute your new command. As you actually want the opposite of this you will
need to negate the outcome supplied to if.

Also, as pgrep outputs to the console and you are not interested in the output (only if there is any), I would suggest redirecting the output to /dev/null
 
Old 05-12-2014, 10:34 AM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Oh, I didn't notice you were checking if the script wasn't running. Like grail said, you need to negate the condition:
Code:
if ! pgrep -f script ; then
...
 
  


Reply



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
Using ping's return in a shell script brgsousa Linux - Software 3 05-28-2008 02:19 PM
Shell Script to check filesize - Amount Of lines baddah Programming 1 08-20-2007 07:58 AM
Return code of a shell script zachos.v Linux - Software 3 10-10-2006 09:51 AM
Return value to shell in script mwade Linux - Newbie 11 08-18-2006 09:49 AM
return value from shell script to c code? khucinx Programming 1 05-13-2004 03:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:43 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration