【问题标题】:How do I escape a keyword which is a command in a shell script?如何转义作为 shell 脚本中命令的关键字?
【发布时间】:2017-12-27 00:37:47
【问题描述】:

在我的 shell 脚本中,我有

#!/bin/bash
var1=$1
var2=$2
cat<<new_script<<EOF
...some code...
for i in `find / -perm 6000 -type f`; do chmod a-s $i; done
mkdir $var1
...some code...
EOF

我尝试了很多东西,但我无法逃脱“查找”命令。 当我运行这个 shell 脚本时,它只是在终端中运行 find 命令,而不是将其写入 new_script。

【问题讨论】:

    标签: linux bash shell heredoc


    【解决方案1】:

    用单引号引用EOF

    #!/bin/bash
    cat >>new_script <<'EOF'
    ...some code...
    for i in `find / -perm 6000 -type f`; do chmod a-s $i; done
    ...some code...
    EOF
    

    【讨论】:

    • 你能解释一下放置'EOF'做了哪些改变来使它工作?
    • 如果没有单引号,行为是变量被插值,反引号中的命令被评估等等。这可以通过引用标签的任何部分来禁用,然后以未引用的值结束。 Source.
    • 我也需要使用变量。我已经进行了修改。
    • 'EOF' 替换为EOF,将`find / -perm 6000 -type f` 替换为\$(find / -perm 6000 -type f)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2013-06-10
    • 2011-01-15
    相关资源
    最近更新 更多