Silence bash command output or change it to something custom

You may find that when running commands through bash or scripts that you want to silence or even change their default output to suit your needs.

The easiest way to silence the output of a command is to add > /dev/null

Assuming that mysql is running on the system in question (This will also work for any running process)

pgrep mysql
1046

To silence the output you would do this:

pgrep mysql > /dev/null

To change the output you could place it inside an if statement and call it from a script.

Save the following as mysqlrunning.sh and run it!

if pgrep mysql; /dev/nul #checks if process mysql is running and silences the output
then #If it is running do what ever is on the line underneath
echo MySQL is running normally #In this case display the following message
exit #Now Quit
else #If no process with the name mysql is running then do the line beneath
echo MySQL is not running AAARGGGHHHH! #Display this message!
fi #END
Print Friendly, PDF & Email

More Like This


Categories


Linux
  • Post a comment