为什么在运行”/bin/sh”时使用”-c”选项时会得到空白输出

huangapple go评论57阅读模式
英文:

Why getting blank output when running "/bin/sh" with "-c" option

问题

你好,这是翻译的部分:

我正在以如下方式使用/bin/sh-c选项运行,但却得到了空白输出:

[root@dockerhost dproj]# /bin/sh -c echo helloworld
[root@dockerhost dproj]#

为什么上述命令没有打印出helloworld

我尝试阅读man页面,但无法理解任何内容。

man sh中:

-c        从command_string操作数中读取命令。 
          从command_name操作数的值以及剩余的参数操作数中按顺序设置特殊参数0的值
          (参见第2.5.2节,特殊参数)。
          不应从标准输入读取任何命令。
英文:

I am running /bin/sh with -c option as below but getting a blank output:

[root@dockerhost dproj]# /bin/sh -c echo helloworld
[root@dockerhost dproj]#

Why the above command is not printing helloworld?

I have tried to read the man page but not able to understand anything.

From man sh:

   -c        Read  commands  from the command_string operand. 
             Set the value of special parameter 0 
             (see Section 2.5.2, Special Parameters) from the
             value of the command_name operand and the positional
             parameters ($1, $2, and so on) in sequence from the 
             remaining argument  operands.
             No commands shall be read from the standard input.

答案1

得分: 4

/bin/sh -c echo helloworld 运行 echo 命令,会打印一个空行。您的意思是输入:

/bin/sh -c "echo helloworld"

这将运行命令 echo helloworld

-c 选项用于 sh,会导致第一个非选项参数被视为要在新 shell 中运行的命令字符串。其余的参数用于填充该 shell 的编号参数,从 $0 开始(即 shell 进程正在运行的“名称”)。这些参数不会自动解析为由该 shell 执行的任何实用程序。

因此,如果您使用 /bin/sh -c echo helloworld 调用 sh

  • 传递给 shell 解释器 sh 的输入只是 echo
  • helloworld 变成了 sh 会话中的 $0

通常,shell 调用中的 $0 应该是 shell 的名称;这通常是默认设置的:

bash$ /bin/sh
$ echo $0
/bin/sh
$ exit
bash$ echo $0
bash

-c 给 shell 的命令会被解释为同样输入到 shell 本身那样,您可以在命令中使用 $n 来引用 shell 解释器的额外参数。如果您想这样做,需要记住对 -c 选项参数使用单引号,这样其内容就不会被外部 shell 解释。也许学习以下内容会有所帮助:

bash$ /bin/sh -c 'echo $0' fakename   # 正确使用了单引号
fakename
bash$ /bin/sh -c "echo $0" fakename   # 这里,$0 被外部 shell 展开了
bash
bash$ /bin/sh -c 'echo "$@"' sh arg1 arg2 arg3  # $0 不是 $@ 的一部分
arg1 arg2 arg3
英文:

/bin/sh -c echo helloworld runs the command echo, which prints a blank line. You meant to type:

/bin/sh -c "echo helloworld"

which runs the command echo helloworld.

The -c option to sh causes the first non-option argument to be treated as a string of commands to run in a new shell. The remaining arguments are used to fill in the that shell's numbered arguments, starting with $0 (the "name" under which the shell process is running.) These arguments are not automatically parsed to any utility executed by that shell.

So if you invoke sh with /bin/sh -c echo helloworld:

  • the input passed to the shell interpreter sh is simply echo
  • helloworld becomes $0 in the sh session.

Normally, $0 in a shell invocation should be the name of the shell; that's what it will be set to by default:

bash$ /bin/sh
$ echo $0
/bin/sh
$ exit
bash$ echo $0
bash

Since the command given to the shell by -c is interpreted as though it were input to the shell itself, you can use $n in the command in order to refer to the extra arguments to the shell interpreter. If you want to do that, you need to remember to single-quote the -c option argument so that it's contents are not interpreted by the outer shell. Perhaps studying the following will help:

bash$ /bin/sh -c 'echo $0' fakename   # Correctly single-quoted
fakename
bash$ /bin/sh -c "echo $0" fakename   # Here, $0 is expanded by the outer shell
bash
bash$ /bin/sh -c 'echo "$@"' sh arg1 arg2 arg3  # $0 is not part of $@
arg1 arg2 arg3

</details>



huangapple
  • 本文由 发表于 2023年2月6日 09:35:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定