LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-01-2005, 09:29 AM   #1
Machiaveli
Member
 
Registered: Jan 2004
Posts: 47

Rep: Reputation: 15
stdout to file


I would like to redirect the output from stdout to a file, depending on the outcome of the exit status.

How would this be done?
I'm sure it's an easy question. I've read some of the documentation at tlpd.org concerning I/O redirection, but I don't know how to redirect the output in an if-statement.

Code:
#!/bin/bash

SITE=1.1.1.1
GOODLOG=/var/log/wlan/good.log
BADLOG=/var/log/wlan/bad.log

ping -c 2 $SITE > /dev/null
        if [ $? = 0 ]
        then
                # How do I >> stdout to $GOODLOG
        else
                echo `date +%F\ %r` Connection failed >> $BADLOG
fi
 
Old 09-01-2005, 09:34 AM   #2
ichi
Member
 
Registered: Mar 2005
Distribution: Gentoo
Posts: 50

Rep: Reputation: 15
Yup, it is easy, the redirection operator, >

For example, echo hi > /home/user/greeting would make the file /home/user/greeting contain 'hi'
 
Old 09-01-2005, 09:44 AM   #3
Machiaveli
Member
 
Registered: Jan 2004
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by ichi
Yup, it is easy, the redirection operator, >

For example, echo hi > /home/user/greeting would make the file /home/user/greeting contain 'hi'
Yes, I know > redirect, >> redirect and append.

But what would be the correct syntax/command to redirect and append the output from stdout to the @GOODLOG file, if the ping is successfull?
 
Old 09-01-2005, 09:49 AM   #4
ichi
Member
 
Registered: Mar 2005
Distribution: Gentoo
Posts: 50

Rep: Reputation: 15
Oh right, sorry.

I'm not too experienced with bash, but I think the only way to do it would be to redirect the output of ping to a temp file or a variable and then read it from that. I don't see any other way of doing it.
 
Old 09-01-2005, 11:41 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,290

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
i'm not sure why the if...then...else logic isn't working for me but this should put you on the right path:
Code:
#!/bin/bash

SITE=www.something.com
GOODLOG=/var/log/wlan/good.log
BADLOG=/var/log/wlan/bad.log
log=`cat schneidz.txt`
#ping -c 2 $SITE > /dev/null
echo $?
        if [ $? = 0 ]
        then
                echo $log >> $GOODLOG # How do I >> stdout to $GOODLOG
        else
                echo `date +%F\ %r` Connection failed >> $BADLOG
fi
ping -c 2 $SITE > /dev/null # you're sending stdout to /dev/null; you cant get it back. it has to be redirected in real time.

a better solution would be to save it (redirect it) and if '$?' is not 0 then delete it. then 'echo `date +%F\ %r` Connection failed >> $BADLOG'

you also lose the new lines, you will have to sed-awk "\012" (octal for line feed) back in.

anybody else got ideas?

Last edited by schneidz; 09-01-2005 at 12:00 PM.
 
Old 09-01-2005, 12:37 PM   #6
Machiaveli
Member
 
Registered: Jan 2004
Posts: 47

Original Poster
Rep: Reputation: 15
Nuthin' but a peanut :-)

Thanks!

Code:
#!/bin/bash


SITE=www.somesite.org
GLOG=/var/log/wlan/good.log       
BLOG=/var/log/wlan/bad.log         
TLOG=/var/log/wlan/temp.log  

ping -c 2 $SITE > $TLOG
        if [ $? = 0 ]
        then
                cat $TLOG >> $GLOG | cat /dev/null > $TLOG
        else
                echo `date +%F\ %r` Connection failed >> $BLOG | cat /dev/null > $TLOG
fi


# Voila!
 
Old 09-02-2005, 11:07 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,487

Rep: Reputation: 233Reputation: 233Reputation: 233
why not output errors to the stderr stream like normally?


I usually set up an alias, like:

Code:
alias LOG='echo'
alias ERROR='>&2 echo OOPS:'
(or usually I will use the shell built-in 'printf' instead).


then you can call
your_script > GOODLOG 2>BADLOG

or if it must be a file

Code:
alias LOG='> GOODLOG  echo'
alias ERROR='>&2 BADLOG echo OOPS:'
Then you can change it in ONE place if the name changes
and you can use the alias anywhere.

Also you don't get loads of Really Nasty echo blah-blah >> FILE lines everywhere.
yuk.

You can even make'em functions and add timestamps and such like.

just a thought.
 
Old 09-03-2005, 05:48 PM   #8
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
This line sucks!
Code:
cat $TLOG >> $GLOG | cat /dev/null > $TLOG
Why not
Code:
cat $TLOG >>$GLOG
>$TLOG
No point in spawning processes needlessly.
 
Old 09-04-2005, 09:53 AM   #9
Machiaveli
Member
 
Registered: Jan 2004
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by bigearsbilly
why not output errors to the stderr stream like normally?


I usually set up an alias, like:

Code:
alias LOG='echo'
alias ERROR='>&2 echo OOPS:'
(or usually I will use the shell built-in 'printf' instead).


then you can call
your_script > GOODLOG 2>BADLOG

or if it must be a file

Code:
alias LOG='> GOODLOG  echo'
alias ERROR='>&2 BADLOG echo OOPS:'
Then you can change it in ONE place if the name changes
and you can use the alias anywhere.

Also you don't get loads of Really Nasty echo blah-blah >> FILE lines everywhere.
yuk.

You can even make'em functions and add timestamps and such like.

just a thought.
Any and all thought are welcome, of course!
The reason I'm not even trying to write a more complex script with functions and a generally better code or logic is my lack of know-how and time. This is indeed my very first script and I wrote it after watching a LinuxCBT e-learning video on BASH scripting, so don't be too harsh on me :-)
It covered the concept of functions but not much about std/in/out/err or the redirection of them, for that matter.
I will however read some more about shell scripting and maybe I'll try to implement some of your suggestions!

Quote:
Originally posted by eddiebaby1023
This line sucks!
Code:
cat $TLOG >> $GLOG | cat /dev/null > $TLOG
Why not
Code:
cat $TLOG >>$GLOG
>$TLOG
No point in spawning processes needlessly.
Wow! Easy now..

It's my first script, ever. Needless to say there are room for improvments. The basic idea is to monitor a radiolan over a period of time and capture packets with mrtg. I decided to write this simple script to generate some ping packets and to check the response times, and/or the amount of failed connections. I'm sure there are programs and whatnot that I could use (to generate traffic) but I don't know how much time I have with this little project so hopefully it will suffice.

Sorry for any bad spelling, I'm not English or American
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Redirecting to file and stdout Quantum0726 Programming 3 11-14-2005 09:35 PM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 08:55 AM
Switch stdout between /dev/null and a file? gremlin2000 Linux - General 2 06-08-2004 08:12 AM
how to output stdout to a file SciYro Linux - Newbie 4 02-25-2004 06:08 PM
capturing STDOUT to file. tn1681 Programming 5 11-01-2002 12:51 PM

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

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