Hiding internal work outputs by shell script

1

I make a shell script that can update a tools. I want that when i run the

root@host:# sh script.sh

then the output will be

Its installing
Its done

But now my script look like

root@host:# sh script.sh
Its installing
Showing internat script working outputs
Its done

As i know stty -echo command hide stdin in terminal and stty echo enable stdin show in terminal. But how can i hide the stander output by shell script ?

Mastan

Posted 2015-05-10T12:42:27.107

Reputation: 250

Unwanted output can be redirected to /dev/null, or to a file in case you need it for subsequent diagnostics. – AFH – 2015-05-10T13:27:07.530

http://www.tldp.org/guides.html -> Two guides about bash-programming. These will answer all your questions. – Hannu – 2015-05-10T13:45:16.483

@AFH. I try to redirect /dev/null but i failed. – Mastan – 2015-05-10T13:58:34.517

check if you have at first line something like: #!/bin/bash -x and if yes remove -x – Romeo Ninov – 2015-05-10T14:00:59.327

@Mastan - I think you need to give a little more information than "tried and failed": what did you try, and what was the nature of the failure? – AFH – 2015-05-10T19:53:03.450

@AFH. In my script there is a line. mv /root/Desktop/lynis/plugins/* /etc/lynis/plugins/ . For that there is an output shows on terminal. I try the output redirect into /dev/null file but it doesn't work. I wrote the line mv /root/Desktop/lynis/plugins/* /etc/lynis/plugins/ | &>/dev/null. – Mastan – 2015-05-11T18:05:42.700

1You don't use the pipe symbol with redirection: your command should be mv /root/Desktop/lynis/plugins/* /etc/lynis/plugins/ &>/dev/null. The pipe symbol sends the output to another program's input; redirection sends the output to a file: you can't do both (although that the program to which output is piped can have its own output redirected, as in prog1 | prog2 &> file3). – AFH – 2015-05-12T00:31:24.677

Answers

1

Redirect both stdout and stderr to /dev/null:

> /dev/null 2>&1

OR in bash:

&> /dev/null

You can do it for all programs spawned by your script by using exec with the redirection at the beginning of your script.

exec > /dev/null 2>&1

Unless the programs you invoke in your script access the terminal directly (rare), this should cover you.

One note on redirections: The order matters. It needs to be > /dev/null 2>&1, NOT 2>&1 >/dev/null. I used to think the latter would work because > looks like an arrow which makes me think of pointers (as does the word "redirect"), and if I point stderr to stdout and then point stdout to /dev/null, then both should be pointing to /dev/null. That is not the case, though. File descriptors are not pointers and it is more helpful to think of > as sort of an assignment to a file descriptor rather than pointing. (Sort of, because technically, a fd is just a number and you need system functions like dup2 to open a different file into the same file descriptor; but I think assignment is a good high-level abstraction).

PSkocik

Posted 2015-05-10T12:42:27.107

Reputation: 1 182