[SOLVED] How-to supress output when exit code not 0 ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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'.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.