roacha
roacha

Reputation: 603

Shell script hide console messages?

I am having a really hard time hiding console messages from my shells (ksh) script which I run in the background.

I have tried moving it to /dev/null but it doesn't seem to be working. Here is the line in the script:

pid=`/usr/local/bin/lsof | grep 16752 | grep LISTEN |awk '{print $2}'` > /dev/null 2>&1

Example after I have kicked off the script:

$ lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.
lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.
lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.
lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.
lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.
lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.
lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.
lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.
lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.
lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.

$ ls -lsof: WARNING: /home2/s499929/.lsof_ktazd2250 was updated.

Any ideas on what I am missing?

Upvotes: 1

Views: 1681

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263307

You're redirecting the output of the variable assignment. Since variable assignments don't produce any output, your redirection isn't doing anything.

The backticks capture the command's stdout; you need to redirect that command's stderr, which means the redirection needs to be between the backticks.

My first thought was to do this:

pid=`/usr/local/bin/lsof | grep 16752 | grep LISTEN | awk '{print $2}' 2>/dev/null`

but that only redirects the stderr of the awk command. You need to discard stderr of the entire pipeline:

pid=`(/usr/local/bin/lsof | grep 16752 | grep LISTEN | awk '{print $2}') 2>/dev/null`

But personally I prefer $(...) to backticks:

pid=$((/usr/local/bin/lsof | grep 16752 | grep LISTEN | awk '{print $2}') 2>/dev/null)

(Note that I'm only redirecting stderr, not stdout; you need stdout for the variable assignment.)

Upvotes: 0

Peder Klingenberg
Peder Klingenberg

Reputation: 41135

/usr/local/bin/lsof 2>/dev/null | grep 16752 | grep LISTEN |awk '{print $2}'

Upvotes: 3

Jonatan
Jonatan

Reputation: 2886

Maybe you need to redirect output of the actual lsof command and not awk?

Upvotes: 0

Related Questions