我有一个包含简单“getopts”的脚本。
我发现只有这样才能调用它:
bash <(http://domain.com/myscript.sh) -a
为什么这样调用它不起作用?
curl –L http://domain.com/myscript.sh | bash -s -a
用curl调用它有什么不同的方法吗? (除了我提供的)
我不想下载它,只是使用curl来执行它。
#!/bin/bash
while getopts ":a" opt; do
case $opt in
a)
echo "-a was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
答案 0 :(得分:1)
-s
以-
开头后的参数似乎仍然需要被解析为bash的选项(参见help set
):
printf "arguments: %s\n" "$*"
shopt -o | grep noglob
试验:
$ bash -s -f < script.sh
arguments:
noglob on
$ bash -s -a < script.sh
arguments:
noglob off
$ bash -s -z < script.sh
bash: -z: invalid option
$ bash -s x < script.sh
arguments: x
noglob off
要阻止它,只需使用--
:
curl –L http://domain.com/myscript.sh | bash -s -- -a
来自bash的手册:
A - 表示选项结束并禁用其他选项 处理。 - 之后的任何参数都被视为文件名和 参数。 - 的参数相当于 - 。
#!/bin/bash
while getopts ":a" opt; do
case $opt in
a)
echo "-a was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
测试:
$ bash script.sh -s -- -a < script.sh
-a was triggered!
答案 1 :(得分:0)
简单:
curl -L http://domain.com/myscript.sh | bash /dev/stdin -a