如何从终端在 bash 管道中传递参数

  • 本文关键字:参数 管道 bash 终端 bash
  • 更新时间 :
  • 英文 :


我在一个名为 test.sh 的文件中显示了下面的 bash 脚本

#!/usr/bin/env bash
echo $1
echo "execution done"

当我使用

案例-1

./test.sh "started"
    started
    execution done

正确显示

案例-2

如果我执行

bash test.sh "started"

我正在把输出作为

开始

执行完成

但我想使用带有参数的 cat 或 wget 命令来执行它例如喜欢。

第一季度

猫 test.sh |bash

或使用命令

第 2 季度

wget -qO - "url contains bash" |bash

所以在 Q1 和 Q2 中,我如何传递参数

在这个 github 中显示的类似的东西

https://github.com/creationix/nvm

请参考安装脚本

$ bash <(curl -Ls url_contains_bash_script) arg1 arg2

解释:

$ echo -e 'echo "$1"necho "done"' >test.sh
$ cat test.sh
echo "$1"
echo "done"
$ bash <(cat test.sh) "hello"
hello
done
$ bash <(echo -e 'echo "$1"necho "done"') "hello"
hello
done

你不需要管道来bash;bash在你的终端中作为标准运行。

如果我有一个脚本并且我必须使用 cat,这就是我要做的:

cat script.sh > file.sh; chmod 755 file.sh; ./file.sh arg1 arg2 arg3

script.sh是源脚本。您可以将该呼叫替换为所需的任何内容。

不过,这具有安全隐患;只需在shell中运行任意代码 - 特别是对于代码来自远程位置的wget。

最新更新