Friday, July 24. 2009
Filter stderr output in bash
Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
Well, email client filters are much easier to use for this purpose. ;> Of course you could defend Bash solution as a bandwidth-saving one. Anyway - thanks for this recipe.
Thanks for the example above. I have a specific question regarding your solution. I am trying to suppress the BAD PASSWORD error from the passwd command. I used your script as a model and did the following:
sudo passwd existingUser 2> >(grep -v "BAD PASSWORD" >&2) > /dev/null and it almost works. The problem that I am having is that the prompts for the password and verification now comes up after I the required password needs to be entered in. The BAD PASSWORD output is suppressed though. I'm relatively new to shell scripts and was wondering how I can get the order right.
sudo passwd existingUser 2> >(grep -v "BAD PASSWORD" >&2) > /dev/null and it almost works. The problem that I am having is that the prompts for the password and verification now comes up after I the required password needs to be entered in. The BAD PASSWORD output is suppressed though. I'm relatively new to shell scripts and was wondering how I can get the order right.
First, you probably shouldn't be suppressing that error. chuckle
Use the "--line-buffered" option for grep to display each line one at a time unless it is filtered. By default grep will only output results after the input from the piped command has finished.
-darren
Use the "--line-buffered" option for grep to display each line one at a time unless it is filtered. By default grep will only output results after the input from the piped command has finished.
-darren
Darren, Thanks for getting back to me. I tried using the --line-buffered option, but got the same result (i.e. sudo passwd existingUser 2> >(grep -v --line-buffered "BAD PASSWORD" >&2) > /dev/null). I think that the solution is related to what you said about grep outputting results after the command completes. (I appreciate you not getting on me too much about suppressing the passwd error. The password checking is actually being done through a GUI interface.) Anyway, being a shell scripting newbie, if you can think of any way around this problem, I'd appreciate your feedback. I guess that I could emulate the passwd functionality and re-direct that input into the actual passwd command, but I'm not quite sure how to do that either.
I'm just thinking about it more - passwd is opening a second tty, which is the correct design for prompting for a password. Since the grep is only on the current tty, I'm pretty certain you cannot filter the output.
If you still want to do this, use the "--stdin" option to passwd to pipe in the password. Do not put the password on the command line, but pipe it from the output of another command that prompts and verifies the password. If you do some simple verification for password strength, you won't get the "BAD PASSWORD" errors you're trying to filter when you pipe it into passwd.
If you still want to do this, use the "--stdin" option to passwd to pipe in the password. Do not put the password on the command line, but pipe it from the output of another command that prompts and verifies the password. If you do some simple verification for password strength, you won't get the "BAD PASSWORD" errors you're trying to filter when you pipe it into passwd.
Utilizing the --stdin option was what the doctor ordered. Thanks so much for your help!
Wow, that's really clever. But seriously, who even reads cron emails?
Well, I guess only me. chuckle
I actually read through root/cron mail for roughly one hundred systems every morning for work, so I use filter-syslog, custom newsgroups and slrn with a judicious scorefile/killfile to only see the interesting stuff that I really need to take care of.
I actually read through root/cron mail for roughly one hundred systems every morning for work, so I use filter-syslog, custom newsgroups and slrn with a judicious scorefile/killfile to only see the interesting stuff that I really need to take care of.
Navigation
Calendar
|
|
February '12 | |||||
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | ||||
Quicksearch
Categories
Blog Administration
Powered by serendipity, Design by Garvin Hicking. Smile, you're on the candid credit line!