LinuxQuestions.org
Visit Jeremy's Blog.
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 10-21-2011, 12:07 PM   #1
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Rep: Reputation: 78
How-to supress output when exit code not 0 ?


rhel 5.7 in bash

i have a script taht has this, and i need to supress stdout for all $? codes.

passwd -S $1 >/dev/null

works fine when $?=0 but does not bit buck when $? != 0

is this because error message comes from pam?
 
Old 10-21-2011, 12:12 PM   #2
r.osmanov
LQ Newbie
 
Registered: Feb 2011
Distribution: openSUSE, Ubuntu, Gentoo
Posts: 17

Rep: Reputation: 3
Quote:
Originally Posted by Linux_Kidd View Post
rhel 5.7 in bash
passwd -S $1 >/dev/null
Code:
passwd -S $1 > /dev/null 2>&1
?
 
Old 10-21-2011, 12:44 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
At least in bash you could also do:
Code:
passwd -S $1 &> /dev/null
Which will redirect stdout and stderr to /dev/null.
 
Old 10-21-2011, 12:54 PM   #4
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by crts View Post
At least in bash you could also do:
Code:
passwd -S $1 &> /dev/null
Which will redirect stdout and stderr to /dev/null.
hmmm, yeah, was missing the &

almost there, one last issue. i need to get the stdout or stderr into a variable while at same time supressing screen output

VALID=`passwd -S $1`

i need a variable string to work with while at the same time i need to supress "passwd: unknown user name" , etc. can this be done in a single line?

thnx.

Last edited by Linux_Kidd; 10-21-2011 at 01:06 PM.
 
Old 10-21-2011, 01:50 PM   #5
r.osmanov
LQ Newbie
 
Registered: Feb 2011
Distribution: openSUSE, Ubuntu, Gentoo
Posts: 17

Rep: Reputation: 3
Quote:
Originally Posted by Linux_Kidd View Post
hmmm, yeah, was missing the &

almost there, one last issue. i need to get the stdout or stderr into a variable while at same time supressing screen output

VALID=`passwd -S $1`

i need a variable string to work with while at the same time i need to supress "passwd: unknown user name" , etc. can this be done in a single line?

thnx.
Do you want suppress stderr(descriptor no. 2) and get stdout(descriptor no. 1) into a variable?
What's about
Code:
VALID=`passwd -S $1 2>/dev/null`
?
 
Old 10-21-2011, 06:17 PM   #6
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by r.osmanov View Post
Do you want suppress stderr(descriptor no. 2) and get stdout(descriptor no. 1) into a variable?
What's about
Code:
VALID=`passwd -S $1 2>/dev/null`
?
i would any output into the variable and suppress anything to the screen, etc.
thnx
 
Old 10-22-2011, 04:26 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Linux_Kidd View Post
i would any output into the variable and suppress anything to the screen, etc.
thnx
In this case you can do:
Code:
variable=$(passwd -S users 2>&1)
This redirects stderr to stdout and then assigns the output to variable. However, if you want to check if the command succeeded it is easier to check the exit code with
$?
rather than parsing the returned message in 'variable'.

Last edited by crts; 10-22-2011 at 04:28 AM.
 
Old 10-22-2011, 07:50 AM   #8
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by crts View Post
In this case you can do:
Code:
variable=$(passwd -S users 2>&1)
This redirects stderr to stdout and then assigns the output to variable. However, if you want to check if the command succeeded it is easier to check the exit code with
$?
rather than parsing the returned message in 'variable'.
i'll give this try. and yes, i will be checking $?, and i also need the string for other parts of script, etc. thanks.
 
Old 10-22-2011, 09:32 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Also, note that output in stderr and a non-zero exit code are completely unrelated.
 
1 members found this post helpful.
Old 10-24-2011, 07:41 AM   #10
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by crts View Post
In this case you can do:
Code:
variable=$(passwd -S users 2>&1)
This redirects stderr to stdout and then assigns the output to variable. However, if you want to check if the command succeeded it is easier to check the exit code with
$?
rather than parsing the returned message in 'variable'.
here's the issue, "variable=$(passwd -S $1 2>&1)" always returns $?=0. i was looking for a short one-liner that could fill the variable while at the same time be able to grab $? of the passwd command. i'm no bash expert, but i assume in this case there is a $? for "$(passwd -S $1 2>&1)" and then that gets overwritten when the shell pipes the data into the variable..? doing what i need in two lines is ok, i was just looking for a one-liner, etc.
 
Old 10-24-2011, 08:02 AM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Linux_Kidd View Post
here's the issue, "variable=$(passwd -S $1 2>&1)" always returns $?=0. i was looking for a short one-liner that could fill the variable while at the same time be able to grab $? of the passwd command. i'm no bash expert, but i assume in this case there is a $? for "$(passwd -S $1 2>&1)" and then that gets overwritten when the shell pipes the data into the variable..? doing what i need in two lines is ok, i was just looking for a one-liner, etc.
No, it does not - at least not in my bash (GNU bash, version 4.1.5).
Code:
$ passwd -S no-user 2>&1
passwd: user 'no-user' does not exist
$ variable=$(passwd -S no-user 2>&1)
$ echo $?
1
The result is also not piped into the variable. It is assigned. Post the exact code that you are using. Also keep in mind, that $? will hold the exit status of the last executed command. If you have a command after the assignment then $? will hold that commands exit status.
 
Old 10-24-2011, 08:29 AM   #12
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
crts,
indeed you are correct, not sure exactly why i thought my exit codes were always zero (was testing it about 10 times) and thats why i posted "doesnt work for me". but in fact it does work as desired.

thnx, i'll mark this one solved.

Last edited by Linux_Kidd; 10-24-2011 at 08:30 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
Exit Code wasamzy Linux - Server 1 11-26-2010 03:11 AM
how to supress terminal output? RedOctober45 Linux - Software 2 01-11-2008 08:59 AM
Supress kernel output AP81 Linux - General 5 02-19-2007 02:16 AM
Exit Code: 7 dbzw SUSE / openSUSE 0 12-21-2004 01:06 PM
kppp exit output 16 Greek Acrobat Linux - Networking 2 09-04-2004 02:11 PM

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

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