相关疑难解决方法(0)

带有位置参数的 Bash -c

通常,$0在脚本中设置为脚本的名称,或者它被调用的任何内容(包括路径)。但是,如果我使用bash-c选项,$0则设置为在命令字符串之后传递的第一个参数:

bash -c 'echo $0' foo bar 
# foo 
Run Code Online (Sandbox Code Playgroud)

实际上,位置参数似乎已经发生了变化,但包括$0. 但是shift在命令字符串中不影响$0(正常):

bash -c 'echo $0; shift; echo $0' foo bar
# foo
# foo
Run Code Online (Sandbox Code Playgroud)

为什么命令字符串的这种明显奇怪的行为?请注意,我正在寻找实施这种奇怪行为背后的原因和基本原理。


人们可以推测这样的命令字符串不需要$0通常定义的参数,因此为了经济起见,它也用于普通参数。但是,在这种情况下, 的行为shift很奇怪。另一种可能性是$0用于定义程序的行为(bash称为 asshvim称为 as vi),但不能如此,因为$0这里仅在命令字符串中可见,而不是在其中调用的程序中可见。我想不出任何其他用途$0,所以我无法解释这一点。

bash

18
推荐指数
2
解决办法
5414
查看次数

如何在没有替换或解释的情况下将字符串文字存储到变量中?

要扁平化目录结构,我可以这样做:

find . -type f -exec sh -c 'mv "{}" "./`basename "{}"`"'  \;
Run Code Online (Sandbox Code Playgroud)

我想将以下内容作为 $FLATTEN 存储在我的个人资料中

-exec sh -c 'mv "{}" "./`basename "{}"`"'  \;
Run Code Online (Sandbox Code Playgroud)

这样以后我就可以执行 find . $FLATTEN

我在存储变量时遇到问题,因为它过早地被解释。我希望将它存储为字符串文字,并且仅在 shell 上使用时进行解释,而不是在来源时进行解释。

shell variable

3
推荐指数
1
解决办法
1689
查看次数

仅使用“sh -c”调用 shell 脚本时出现错误:“意外的运算符”

我有以下脚本:

installRequiredFiles=$1

install_with_pkg()
{
    arg1=$1
        echo $arg1
    echo "[+] Running update: $arg1 update."
}

if [ $installRequiredFiles = "true" ]; then
    if [ -x "$(command -v apt)" ]; then
          install_with_pkg "apt"
        elif [ -x "$(command -v yum)" ]; then
          install_with_pkg "yum"
        elif [ -x "$(command -v apk)" ]; then
          install_with_pkg "apk"
        else
          echo "[!] Can't install files."
    fi
fi
Run Code Online (Sandbox Code Playgroud)

当我直接运行它时,它工作正常:

root@ubuntu:/# ./myscript.sh "true"
apt
[+] Running update: apt update.
Printing installRequiredFiles: true
Printing arg1: apt
Run Code Online (Sandbox Code Playgroud)

但是当我使用时,sh -c我收到以下错误:

root@ubuntu:/# …
Run Code Online (Sandbox Code Playgroud)

linux shell ubuntu shell-script

0
推荐指数
1
解决办法
649
查看次数

标签 统计

shell ×2

bash ×1

linux ×1

shell-script ×1

ubuntu ×1

variable ×1