AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 992255
Accepted
Smile
Smile
Asked: 2018-01-05 05:57:48 +0800 CST2018-01-05 05:57:48 +0800 CST 2018-01-05 05:57:48 +0800 CST

grep -q 命令选项和结果[重复]

  • 772
这个问题在这里已经有了答案:
什么是`$?`?它是一个变量吗? (4 个回答)
4年前关闭。
echo red | grep -q red
echo $?

结果是

0

grep -q关闭写入标准输出。如果$?找到匹配项,则退出状态为 0,否则不为 0。这几乎是对上面代码的解释。我想了解更多关于$?. 它是什么??

command-line
  • 2 2 个回答
  • 13178 Views

2 个回答

  • Voted
  1. Best Answer
    steeldriver
    2018-01-05T06:05:13+08:002018-01-05T06:05:13+08:00

    从Special Parameters部分man bash:

       Special Parameters
           The  shell  treats  several parameters specially.  These parameters may
           only be referenced; assignment to them is not allowed.
           *      Expands to the positional parameters, starting from  one.   When
                  the  expansion  is  not  within  double  quotes, each positional
                  parameter expands to a separate word.  In contexts where  it  is
                  performed, those words are subject to further word splitting and
                  pathname expansion.  When the  expansion  occurs  within  double
                  quotes,  it  expands  to  a  single  word with the value of each
                  parameter separated by the first character of  the  IFS  special
                  variable.   That  is, "$*" is equivalent to "$1c$2c...", where c
                  is the first character of the value of the IFS variable.  If IFS
                  is  unset,  the  parameters  are separated by spaces.  If IFS is
                  null, the parameters are joined without intervening separators.
           @      Expands to the positional parameters, starting from  one.   When
                  the  expansion  occurs  within  double  quotes,  each  parameter
                  expands to a separate word.  That is, "$@" is equivalent to "$1"
                  "$2"  ...   If the double-quoted expansion occurs within a word,
                  the expansion of the first parameter is joined with  the  begin‐
                  ning  part  of  the original word, and the expansion of the last
                  parameter is joined with the last part  of  the  original  word.
                  When  there  are no positional parameters, "$@" and $@ expand to
                  nothing (i.e., they are removed).
           #      Expands to the number of positional parameters in decimal.
           ?      Expands to the exit status of the most recently  executed  fore‐
                  ground pipeline.
           -      Expands  to  the  current option flags as specified upon invoca‐
                  tion, by the set builtin command, or  those  set  by  the  shell
                  itself (such as the -i option).
           $      Expands  to  the  process ID of the shell.  In a () subshell, it
                  expands to the process ID of the current  shell,  not  the  sub‐
                  shell.
           !      Expands  to  the process ID of the job most recently placed into
                  the background, whether executed as an asynchronous  command  or
                  using the bg builtin (see JOB CONTROL below).
           0      Expands  to  the name of the shell or shell script.  This is set
                  at shell initialization.  If bash is invoked with a file of com‐
                  mands,  $0  is set to the name of that file.  If bash is started
                  with the -c option, then $0 is set to the first  argument  after
                  the  string to be executed, if one is present.  Otherwise, it is
                  set to the filename used to invoke bash, as  given  by  argument
                  zero.
           _      At  shell  startup,  set to the absolute pathname used to invoke
                  the shell or shell script being executed as passed in the  envi‐
                  ronment  or  argument  list.   Subsequently, expands to the last
                  argument to the previous command, after expansion.  Also set  to
                  the  full  pathname  used  to  invoke  each command executed and
                  placed in the environment exported to that command.  When check‐
                  ing  mail,  this  parameter holds the name of the mail file cur‐
                  rently being checked.

    请注意,“最近执行的前台管道”并不总是最后一个命令(例如,如果中间命令失败);在 中,如果您需要更细粒度的退出状态信息bash,您可以通过数组在管道中分别访问每个命令的退出状态:PIPESTATUS

       PIPESTATUS
              An  array  variable (see Arrays below) containing a list of exit
              status values from the processes in  the  most-recently-executed
              foreground pipeline (which may contain only a single command).
    

    前任。

    $ echo 'foo bar' | grep 'foo' | sed 's/f/g/'
    goo bar
    $ echo "${PIPESTATUS[*]}"
    0 0 0
    $ echo 'foo bar' | grep 'goo' | sed 's/g/f/'
    $ echo "${PIPESTATUS[*]}"
    0 1 0
    
    • 4
  2. nixpower
    2018-01-05T06:02:55+08:002018-01-05T06:02:55+08:00

    bash 中的$?变量存储最后执行的命令的退出代码。在您的示例中,这意味着echo red | grep -q redexited with code 0,这几乎总是命令成功的标志。

    您可以使用各种其他命令对此进行测试,以查看它们的返回码。例如,

    commandthatdoesntexist; echo $?
    

    将返回错误代码127,依此类推。

    • 2

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve