加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – / bin / sh:变量和直接命令之间的区别

发布时间:2020-12-14 02:42:00 所属栏目:Linux 来源:网络整理
导读:想象一下,我有以下bash脚本: #/bin/sh#next line will work correctly,outputting user name and current directorystart-stop-daemon --start --exec /bin/su -- root -c 'whoami; ls'MYVAR="start-stop-daemon --start --exec /bin/su -- root -c 'whoami
想象一下,我有以下bash脚本:
#/bin/sh
#next line will work correctly,outputting user name and current directory
start-stop-daemon --start --exec /bin/su -- root -c 'whoami; ls'

MYVAR="start-stop-daemon --start --exec /bin/su -- root -c 'whoami; ls'"
#next line will fail with following error:
#ls': -c: line 0: unexpected EOF while looking for matching `''
$MYVAR

问题 – 为什么通过变量的第二种方法不起作用?如何使其工作?

解决方法

正如@lled解释的那样,问题是shell在扩展变量之前处理引号,因此在变量中添加引号并没有做任何有用的事情.但是有几种选择,取决于你想要存储命令的原因(而不是直接执行它).

如果您只想定义一次命令,然后重复使用它,请使用以下函数:

myfunc() {
    start-stop-daemon --start --exec /bin/su -- root -c 'whoami; ls'
}
# ...
myfunc

如果您需要在一个地方构建/选择命令,然后在另一个地方使用它,您可以使用bash数组来存储它.将命令的每个“单词”存储为数组元素,然后如果正确引用它,则会保留这些单词分隔符:

myarray=(start-stop-daemon --start --exec /bin/su -- root -c 'whoami; ls')
# ...
"${myarray[@]}"

这里发生的是阵列被定义为具有元素“start-stop-daemon”,“ – start”,“ – exec”,“/ bin / su”,“ – ”,“root”,“ -c“和”whoami; ls“.单引号不作为数组的一部分存储,但它们具有使“whoami; ls”成为单个数组元素的效果.然后,在数组的扩展中,[@]告诉shell将每个数组元素扩展为一个单独的单词,并且它周围的双引号可防止对结果值进行任何额外的单词拆分.

有关更多信息(以及其他几个选项),请参阅BashFAQ #50:I’m trying to put a command in a variable,but the complex cases always fail!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读