2

在 AIX 上运行时,以下两个命令有什么区别?

/bin/sh 'ls -l -R'
/bin/sh -c 'ls -l -R'
4

2 回答 2

10

在 AIX 上,/bin/sh默认为 Korn Shell (ksh)。

/bin/sh 'ls -l -R'

使用 ksh,这会运行 shell,告诉它执行名为ls -l -R(在当前目录或路径上)的脚本。如果找不到具有此名称的脚本,则 ksh 将参数视为命令并运行它。

请注意,使用 bash,如果找不到脚本,则会导致错误。

/bin/sh -c 'ls -l -R'

这将启动一个新的 shell 实例,告诉它运行 command ls -l -R

于 2010-03-23T14:10:24.947 回答
5

(这是对菲尔罗斯的回应,但我需要格式化:)

使用我的一个 Linux 帐户:

sh> /bin/sh --version
GNU bash, version 3.2.39(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
sh>  /bin/sh 'ls -l -R'
/bin/sh: ls -l -R: No such file or directory
sh> /bin/sh ls
/bin/ls: /bin/ls: cannot execute binary file
sh> /bin/sh -c 'ls -l | head -1'
total 147296
sh> 

使用我的 Cygwin 安装之一:

sh> /bin/sh --version
GNU bash, version 3.2.49(23)-release (i686-pc-cygwin)
Copyright (C) 2007 Free Software Foundation, Inc.
sh> /bin/sh 'ls -l -R'
/bin/sh: ls -l -R: No such file or directory
sh> /bin/sh ls
/usr/bin/ls: /usr/bin/ls: cannot execute binary file
sh> /bin/sh -c 'ls -l | head -1'
total 264744
sh> 

/bin/sh可能会有所不同,但这似乎是适当的行为:

  • 向 /bin/sh 提供文件名参数会将其解释为 shell 脚本的文件名并尝试执行它
  • 提供 -c 参数将将该参数解释为 /bin/sh 命令行并尝试执行该参数

有关更多详细信息,请键入

man sh
于 2010-03-23T15:48:54.697 回答