LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 10-21-2014, 06:31 AM   #1
sumncguy
Member
 
Registered: Jun 2013
Posts: 71

Rep: Reputation: Disabled
Bash Redirect stdout and sterr


This looks a little spaghetti-ish ... Just wonder if Im doing it right. The stdout and sterr redirect

Code:
ping -q -c2 -w2 $i >/dev/null 2>&1
 if [ $? -eq 0 ]
  then
   :
  else
   echo "No Ping"
 fi
Thanks in advance for the input.
 
Old 10-21-2014, 06:48 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,109

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
what is your goal? Is this script working as it was expected?
 
Old 10-21-2014, 07:06 AM   #3
sumncguy
Member
 
Registered: Jun 2013
Posts: 71

Original Poster
Rep: Reputation: Disabled
Hi pan...


The goal with respect to the redirect is to have nothing good or bad be sent to stdout (or actually the display).

The script is working as expected. But as I said, the redirect specifically ...

Code:
>/dev/null 2>&1
... looks a little sloppy and I was wondering if there was a better/cleaner way to do it.
 
Old 10-21-2014, 07:30 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,109

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
"Unfortunately" that is the correct syntax. There is no better way. /dev/null is the target, stdout is redirected into it, and stderr is redirected into stdout. (the file descriptor of stdout is 1, stderr is 2). /dev/null will eat up everything.
 
Old 10-21-2014, 08:06 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
While the redirections are difficult to avoid, you can simplify your script that way:

Code:
ping -q -c2 -w2 $i >/dev/null 2>&1 || echo "No Ping"
 
Old 10-21-2014, 10:01 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I believe the simplest direct all is:
Code:
command &>/dev/null
Also you can use 'if' directly to test the output of commands, so you could have done:
Code:
if ! ping -q -c2 -w2 $i &>/dev/null
then
  echo "No Ping"
fi
 
  


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
[SOLVED] Copy not redirect stdout and stderr in bash budgie26 Programming 11 05-07-2014 09:47 PM
redirect AND print stdout samel_tvom Programming 12 10-07-2012 06:37 PM
[SOLVED] Permanently redirect stdout picho Linux - Newbie 6 02-17-2012 06:46 PM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-04-2008 12:20 AM
Bash scripting (hide sterr messages) Quantum0726 Programming 2 06-07-2005 09:05 PM


All times are GMT -5. The time now is 07:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration