LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > SUSE / openSUSE
User Name
Password
SUSE / openSUSE This Forum is for the discussion of Suse Linux.

Notices


Reply
  Search this Thread
Old 07-27-2009, 12:08 PM   #1
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Rep: Reputation: 15
Script out put to file


Here is my script,

if [-d /srv/srv/dir1/]; then

server_status=some command | grep "server may be down"

if ["server_status" = "server may be down"]; then
echo "server $(hostname) is down " >> /home/uname/filename
else
echo "server $(hostname) is OK " >> /home/uname/filename
fi

fi


if I dont have else condition in the script and when I run this script, I dont see any output file. Instead, I get some output right away on the screen. I checked and the 2nd line of the code generating that out put. I just want to have echo "server $(hostname) is down " in the out put file which I'm not getting.

when I run the above script with else condition, I get error to the screen that server is down, but i see server is ok in the file. Any ideas. Thanks
 
Old 07-27-2009, 12:16 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If you want the output of some command | grep "server may be down" stored in the variable server_status you have to use command substitution, that is:
Code:
server_status=$(some command | grep "server may be down")
but you can also embed the command in the if/then statement:
Code:
if some command | grep -q "server may be down"
  echo "server $(hostname) is down " >> /home/uname/filename
else
  echo "server $(hostname) is OK " >> /home/uname/filename
fi
 
Old 07-27-2009, 12:45 PM   #3
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Original Poster
Rep: Reputation: 15
I'm still getting the output to the screen, and getting server is OK in the file even server is down. I tried with adding command substitution and also with the embed command in if/then statement. But no luck. Thanks
 
Old 07-27-2009, 01:34 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What is the complete output of "some command", before piping it to grep? Please use CODE tags to preserve spacing.
 
Old 07-27-2009, 02:06 PM   #5
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks for your reply...Here is complete output before piping
Quote:
Unable to connect to NDS Server. NDS server may be down
Thanks
 
Old 07-27-2009, 02:17 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Maybe some command is writing to stderr rather than stdout. In that case this would do what you want
Code:
server_status=$(some command 2>&1 | grep "server may be down")
 
  


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
cat file over ssh and put it into log file sinister1 Programming 2 04-24-2009 12:54 AM
Bash script to put log files into single file and email DragonM15 Programming 13 11-08-2007 03:27 AM
.. how to put a script to '/etc/rc.d/rc.*? emil+ Slackware 11 02-24-2006 02:27 PM
unable to put file 4GB file INSIDE iso saravkrish Linux - Software 2 03-23-2005 03:41 PM
Bash put script jnusa Programming 3 09-07-2004 02:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > SUSE / openSUSE

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