LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-07-2015, 01:54 PM   #1
JoseKreif
Member
 
Registered: Jul 2015
Location: Iowa
Distribution: CentOS 6.6, RHEL Server release 5.5 (Tikanga)
Posts: 58

Rep: Reputation: 0
If Then Else using "grep"


Is there a way to determine True or False with a Grep Return?

I build a small menu program to kill and restart a VNC Server. This will save us Devs some time.

I added a command that goes
Code:
finger | grep "(:1)"
, this returns the VNC user. If the VNC Server is off for ":1", It shouldn't show up.

Is there a way to make it so its

Code:
if ( finger | grep "(:1)" ) Then
echo "Please Wait for VNC Server to close"
else
echo "VNC Server is down and ready to be restarted."

Last edited by JoseKreif; 07-07-2015 at 02:00 PM.
 
Old 07-07-2015, 02:34 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Yes.

grep(1) offers exit status which you can use in your script to determine if the outcome was successful versus not.

When using the grep exit status I always recommend checking for success because that is a zero, whereas "not found" is non-zero, but also error is a different non-zero value. Instead check for an affirmative found and then have an else case if you need to do something for the "not found & error" cases.

You check a command's exit status using $? within a script.

I'd enable "set -xv" within your script to ensure that you're looking at what you expect. Finger doesn't show that it provides exit status at all, which makes sense since it is a "report" type of command. But just make sure you don't get bit using a combination or a piped command and end up not looking at the exit status you intend to look at.
 
Old 07-07-2015, 02:51 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Just a side note: the ":1" isn't always true. It can be any number depending on the system... Each time a user logs in with concurrent users the GUI the number will be incremented (right now, I have a :2 display, my wife has the :0, :1 was mine, until I locked it up and manually killed it) and every ssh login that forwards windows can have other numbers (by default, starting with 10, but that is up to the configuration of sshd).
 
Old 07-07-2015, 04:43 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
SERVICE='firefox'
if /usr/bin/pgrep $SERVICE > /dev/null; then echo "$SERVICE is running..."; else echo "Write code here to here to make it run"; exit 1; fi
speweth:
firefox is running...

finger is not reliable as a service test. Better to test the {finger} port, (using nc, or telnet....)

Last edited by Habitual; 07-07-2015 at 04:45 PM.
 
Old 07-08-2015, 08:57 AM   #5
JoseKreif
Member
 
Registered: Jul 2015
Location: Iowa
Distribution: CentOS 6.6, RHEL Server release 5.5 (Tikanga)
Posts: 58

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jpollard View Post
Just a side note: the ":1" isn't always true. It can be any number depending on the system... Each time a user logs in with concurrent users the GUI the number will be incremented (right now, I have a :2 display, my wife has the :0, :1 was mine, until I locked it up and manually killed it) and every ssh login that forwards windows can have other numbers (by default, starting with 10, but that is up to the configuration of sshd).
In some causes yes, But I am from the IT Department, when we VNC into one of our locations, we are (:1), and that's the way we configured it. Now is I ssh, I would be (192.168.x.x), however (:1) will still show up on the finger list since that is the VNC server.
 
Old 07-08-2015, 09:21 AM   #6
JoseKreif
Member
 
Registered: Jul 2015
Location: Iowa
Distribution: CentOS 6.6, RHEL Server release 5.5 (Tikanga)
Posts: 58

Original Poster
Rep: Reputation: 0
All good now.

Code:
 
if finger | grep -q "(:1)"; then
  echo -e "\n VNC Server Is Not Fully Closed Yet, Try again in 5 seconds"
else
  echo -e "\n VNC Server has been killed. You may now start it back up"
fi ;;

http://oi58.tinypic.com/fmj2te.jpg

Not sure where my other reply went, but @jpollard, I know what you are saying, but I am from the IT Department (at Corporate) and the connection we use in VNC is "192.168.x.x:1". The VNC server confirguration is set to make a ":1" spot for us to log into. Only the IT department has that. So the Code I build kills and starts that ":1". It will remain ":1" for us all the time.

Now if we SSH into the server, and we will, because we need to in order to kill and restart it, we will be listed on finger as (192.168.x.x).

http://oi59.tinypic.com/2qmn23d.jpg
Attached Thumbnails
Click image for larger version

Name:	ffdp.PNG
Views:	9
Size:	16.3 KB
ID:	18905   Click image for larger version

Name:	ffdp 2.PNG
Views:	11
Size:	17.7 KB
ID:	18906  

Last edited by JoseKreif; 07-08-2015 at 10:15 AM.
 
  


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
[SOLVED] grep text from a line in between "start" and "end" word deepakdeore2004 Programming 7 08-07-2013 09:45 AM
Usage of "-f" option of grep in a script results in "Permission Denied" error zaayu87 Linux - Newbie 3 07-25-2013 12:45 PM
Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing adamlucansky Linux - General 8 09-25-2009 07:26 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:46 AM.

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