Bash에서 명령 출력을 숨기는 방법
최종 사용자를 위해 bash 스크립트를 더 우아하게 만들고 싶습니다. bash가 명령을 실행할 때 출력을 숨기는 방법 예를 들어 bash가 실행될 때
yum install nano
다음은 bash를 실행 한 사용자에게 표시됩니다.
Loaded plugins: fastestmirror
base | 3.7 kB 00:00
base/primary_db | 4.4 MB 00:03
extras | 3.4 kB 00:00
extras/primary_db | 18 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 3.8 MB 00:02
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nano.x86_64 0:2.0.9-7.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
nano x86_64 2.0.9-7.el6 base 436 k
Transaction Summary
================================================================================
Install 1 Package(s)
Total download size: 436 k
Installed size: 1.5 M
Downloading Packages:
nano-2.0.9-7.el6.x86_64.rpm | 436 kB 00:00
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
Package: centos-release-6-4.el6.centos.10.x86_64 (@anaconda-CentOS-201303020151.x86_64/6.4)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : nano-2.0.9-7.el6.x86_64 1/1
Verifying : nano-2.0.9-7.el6.x86_64 1/1
Installed:
nano.x86_64 0:2.0.9-7.el6
Complete!
이제 사용자에게 이것을 숨기고 대신 표시하고 싶습니다.
Installing nano ......
이 작업을 어떻게 수행 할 수 있습니까? 스크립트를보다 사용자 친화적으로 만드는 데 확실히 도움이 될 것입니다. 오류가 발생하면 사용자에게 표시되어야합니다.
편집 명령 집합이 실행되는 동안 동일한 메시지를 표시하는 방법을 알고 싶습니다.
이것을 사용하십시오.
{
/your/first/command
/your/second/command
} &> /dev/null
설명
명령에서 출력을 제거하려면 두 가지 옵션이 있습니다.
더 이상 입력을받지 않도록 출력 설명자 파일을 닫습니다. 다음과 같이 보입니다.
your_command "Is anybody listening?" >&-
Usually, output goes either to file descriptor 1 (stdout) or 2 (stderr). If you close a file descriptor, you'll have to do so for every numbered descriptor, as
&>
(below) is a special BASH syntax incompatible with>&-
:/your/first/command >&- 2>&-
Be careful to note the order:
>&-
closes stdout, which is what you want to do;&>-
redirects stdout and stderr to a file named-
(hyphen), which is not what what you want to do. It'll look the same at first, but the latter creates a stray file in your working directory. It's easy to remember:>&2
redirects stdout to descriptor 2 (stderr),>&3
redirects stdout to descriptor 3, and>&-
redirects stdout to a dead end (i.e. it closes stdout).Also beware that some commands may not handle a closed file descriptor particularly well ("write error: Bad file descriptor"), which is why the better solution may be to...
Redirect output to
/dev/null
, which accepts all output and does nothing with it. It looks like this:your_command "Hello?" > /dev/null
For output redirection to a file, you can direct both stdout and stderr to the same place very concisely, but only in bash:
/your/first/command &> /dev/null
Finally, to do the same for a number of commands at once, surround the whole thing in curly braces. Bash treats this as a group of commands, aggregating the output file descriptors so you can redirect all at once. If you're familiar instead with subshells using ( command1; command2; )
syntax, you'll find the braces behave almost exactly the same way, except that unless you involve them in a pipe the braces will not create a subshell and thus will allow you to set variables inside.
{
/your/first/command
/your/second/command
} &> /dev/null
See the bash manual on redirections for more details, options, and syntax.
You can redirect stdout to /dev/null.
yum install nano > /dev/null
Or you can redirect both stdout and stderr,
yum install nano &> /dev/null
.
But if the program has a quiet option, that's even better.
A process normally has two outputs to screen: stdout (standard out), and stderr (standard error).
Normally informational messages go to sdout
, and errors and alerts go to stderr
.
You can turn off stdout
for a command by doing
MyCommand >/dev/null
and turn off stderr
by doing:
MyCommand 2>/dev/null
If you want both off, you can do:
MyCommand 2>&1 >/dev/null
The 2>&1
says send stderr to the same place as stdout.
You can redirect the output to /dev/null
. For more info regarding /dev/null read this link.
You can hide the output of a comand in the following ways :
echo -n "Installing nano ......"; yum install nano > /dev/null; echo " done.";
Redirect the standard output to /dev/null
, but not the standard error. This will show the errors occurring during the installation, for example if yum
cannot find a package.
echo -n "Installing nano ......"; yum install nano &> /dev/null; echo " done.";
While this code will not show anything in the terminal since both standard error and standard output are redirected and thus nullified to /dev/null
.
You should not use bash in this case to get rid of the output. Yum does have an option -q
which suppresses the output.
You'll most certainly also want to use -y
echo "Installing nano..."
yum -y -q install nano
To see all the options for yum, use man yum
.
>/dev/null 2>&1
will mute both stdout
and stderr
yum install nano >/dev/null 2>&1
.SILENT:
Type " .SILENT: " in the beginning of your script without colons.
참고URL : https://stackoverflow.com/questions/18062778/how-to-hide-command-output-in-bash
'Program Tip' 카테고리의 다른 글
C 또는 C ++에서 좋은 gotos의 예 (0) | 2020.10.08 |
---|---|
Android GCM SENDER_ID, 어떻게받을 수 있나요? (0) | 2020.10.08 |
Android-R은 변수로 해석 될 수 없습니다. (0) | 2020.10.08 |
Eclipse에서 Android 지원 라이브러리 소스를 어떻게 첨부합니까? (0) | 2020.10.08 |
matplotlib를 사용하여 한 페이지에 여러 플롯을 만드는 방법은 무엇입니까? (0) | 2020.10.08 |