1

I have remove.sh which contains:

rm -v test.tmp

And I have install.sh which contains:

script remove.log -c './remove.sh'

What can I do so that whether or not test.tmp exists, I don't see any rm related messages on the screen but see removed 'test.tmp' or rm: cannot remove 'test.tmp': No such file or directory in remove.log which the script command produces?

ctype.h
  • 556
Codrguy
  • 121

2 Answers2

2

If your question is, "how do I suppress stderr output from a command", the answer will be something along the lines of:

command 2> /dev/null

This will redirect stderr (aka file descriptor 2) to /dev/null, which simply discards anything it receives.

However, the rm command supports the -f flag, which will prevent it from producing an error message if the file does not exist:

$ rm file_that_does_not_exist
rm: file_that_does_not_exist: No such file or directory
$ rm -f file_that_does_not_exist
$
larsks
  • 34,737
  • Thanks for your answer, but no. I don't want to suppress stderr. I just want to hide it from showing up on the terminal but want it logged in my log file. -f flag won't work either as again i need to see the error in the log file. – Codrguy Feb 17 '12 at 23:35
  • In that case, you want script -c remove.log 'your command' > /dev/null. The script command will capture stdout and stderr to your log file, and the redirection will suppress all terminal output from the script command. – larsks Feb 18 '12 at 00:06
1

script is overkill for this. Yes it works, but thats not what script was made for. Script is used for applications which access the TTY directly, and dont use STDOUT/STDERR.

You can easily accomplish this with basic shell redirection.

./remove.sh &> remove.log
phemmer
  • 71,831