【问题标题】:"/bin/sh 'ls -l -R'" vs "/bin/sh -c 'ls -l -R'""/bin/sh 'ls -l -R'" 与 "/bin/sh -c 'ls -l -R'"
【发布时间】:2011-01-30 19:47:31
【问题描述】:

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

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

【问题讨论】:

  • 第二个传递了一个额外的参数。 :P
  • 什么意思?什么附加参数?
  • 在我系统上的 bash、dash、busybox ash、zsh、csh、rc 和 ksh 中,只有 ksh 在没有 -c 的情况下工作。

标签: unix shell ksh aix


【解决方案1】:

在 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 实例,告诉它运行命令 ls -l -R

【讨论】:

  • >/bin/sh 'ls -l -R' total 80 -rw-rw---- 1 user user 39301 Mar 22 18:17 db.trace
  • 我对命令 /bin/sh 'ls -l -R' 没有任何问题
  • @Vladimir:那么你有不同版本的 sh。在我的系统上,上述命令的结果是:/bin/sh: Can't open ls -l -Rls -l /bin/sh 在您的系统上显示什么?
  • >ls -l /bin/sh -r-xr-xr-x 5 bin bin 246558 2008 年 9 月 20 日 /bin/sh
  • @Vladimir 运行/bin/sh --version的结果是什么?
【解决方案2】:

(这是对 Phil Ross 的回应,但我需要格式化:)

使用我的一个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2014-10-10
    • 2015-02-05
    • 2011-04-28
    相关资源
    最近更新 更多