如何隐藏bash命令输出 [英] How to hide command output in bash

查看:2111
本文介绍了如何隐藏bash命令输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的bash脚本更优雅为最终user.How我该隐藏输出时,正在运行bash commmands
对于例如
当bash执行

 百胜安装纳米

下面将显示多达谁执行bash的用户

 加载插件:fastestmirror
基地| 3.7 KB 00:00
底座/ primary_db | 4.4 MB 00:03
2255 | 3.4 KB 00:00
演员/ primary_db | 18 KB 00:00
更新| 3.4 KB 00:00
更新/ primary_db | 3.8 MB 0点02分
设置安装过程
解决依赖
- >运行的事务检查
--->包nano.x86_64 0:2.0.9-7.el6将安装
- >成品依赖决议解决的依赖关系================================================== ==============================
 包门版本库大小
================================================== ==============================
安装:
 纳米x86_64的2.0.9-7.el6基地436ķ事务摘要
================================================== ==============================
安装1包(S)总下载量:436ķ
安装尺寸:1.5米
下载软件包:
纳米2.0.9-7.el6.x86_64.rpm | 436 KB 00:00
警告:rpmts_HdrFromFdno:头V3 RSA / SHA256签名,密钥ID c105b9de:NOKEY
从文件中检索键:///等/ PKI / RPM-GPG / RPM-GPG-KEY-的CentOS-6
导入GPG密钥0xC105B9DE:
 用户名:CentOS的6键(CentOS 6的官方签名密钥)LT; centos-6-key@centos.org>
 包装:CentOS的释放,6-4.el6.centos.10.x86_64(@蟒蛇-的CentOS-201303020151.x86_64 / 6.4)
 来源:在/ etc / PKI / RPM-GPG / RPM-GPG-KEY-的CentOS-6
运行rpm_check_debug
运行的事务测试
交易测试成功
运行的事务
  安装:纳米2.0.9-7.el6.x86_64 1/1
  验证:纳米2.0.9-7.el6.x86_64 1/1安装:
  nano.x86_64 0:2.0.9-7.el6完成!

现在我想从用户隐藏这个,反而显示

 安装纳米......

我怎样才能做到这一点task.I无疑将有助于使脚本的详细用户friendly.In话,那么发生错误,应该显示给用户。

编辑我想知道当正在执行一组命令如何显示同样的信息


解决方案

使用此。

  {
  /你/首/命令
  /你/秒/命令
}&安培;>的/ dev / null的

说明

要消除命令的输出,你有两个选择:


  • 关闭输出描述符文件,这使它接受任何更多的投入。这看起来是这样的:

      your_command是有人听吗? >&安培;  - 

    一般,输出变为要么文件描述符1(标准输出)或2(标准错误)。如果关闭文件描述符,你必须为每个编号的描述符这样做,因为&放大器;&GT; (下图)是一个特殊的BASH语法<$ C不兼容$ C>&GT;&安培; - :

      /您/首/命令&GT;&安培;  -  2  - ;&安培;  - 

    小心地注意顺序:&GT;&安培; - 关闭标准输出,这是你想要做什么; &放大器;&GT; - 重定向输出和错误到一个名为 - (连字符),这是不是你想做。它会看起来是一样的在第一,但后者会在您的工作目录流浪文件。这很容易记住:&GT;和2 标准输出重定向到文件描述符2(标准错误),&GT;及3 将stdout重定向到描述符3,和&GT;&安培; - 标准输出重定向到一个死胡同(即它关闭标准输出)

    另外要注意,某些命令可能无法处理一个封闭的文件描述符特别好(写入错误:错误的文件描述符),这就是为什么更好的解决方案可能是...


  • 输出重定向到的/ dev / null的 ,它接受所有输出并执行与它无关。它看起来是这样的:

      your_command喂? &GT;的/ dev / null的

    有关输出重定向到一个文件,可以直接stdout和标准错误输出到同一个地方很简洁,但只在bash:

      /您/首/命令与放大器;&GT;的/ dev / null的


最后,做了许多相同的命令一次,环绕在大括号整个事情。巴什将此视为一组命令的,汇总的输出文件描述符,所以你可以一次全部重定向。如果你熟悉,而不是使用子shell (命令1;命令;)语法,你会发现在大括号的行为几乎完全一样,只不过,除非你让他们参与管道括号不会创建一个子shell,因此将允许您设置里面的变量。

  {
  /你/首/命令
  /你/秒/命令
}&安培;&GT;的/ dev / null的

查看重定向的 bash的手动的更多细节,选项​​和语法。

I want to make my bash scripts more elegant for the end user.How do i hide the output when bash is executing commmands For e.g when bash execute

yum install nano

The following will show up to the user who executed the 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!

Now i want to hide this from the user and instead show

Installing nano ......

How can i accomplish this task.I will definitely help to make script more user friendly.In case an error occurs then it should be shown to the user.

Edit i would like to know how to show same message while a set of commands are being executed

解决方案

Use this.

{
  /your/first/command
  /your/second/command
} &> /dev/null

Explanation

To eliminate output from commands, you have two options:

  • Close the output descriptor file, which keeps it from accepting any more input. That looks like this:

    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.

这篇关于如何隐藏bash命令输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆