【发布时间】:2021-06-04 07:20:55
【问题描述】:
[更新]请注意,为了满足minimal reproducible example,这是本文的简化代码。脚本中会有更多的功能。
我在a Github repo 上有一个 Bash 脚本:
#!/usr/bin/env bash
fn_test1() {
echo "starting ..."
read -rp "Do you want to continue? yes/y or no/n " PANS
ans=$(echo "$PANS" | cut -c 1-1 | tr "[:lower:]" "[:upper:]")
if [[ "$ans" = "Y" ]]; then
echo "confirmed 1."
fi
echo "done"
}
fn_main() {
if [ $# -eq 1 ]; then
case $1 in
"test")
fn_test1
;;
esac
else
echo "Something wrong."
exit 1
fi
}
fn_main "$@"
我想像这样从这个脚本中调用test 函数,但它不起作用:
bash -c "$(curl -s https://raw.githubusercontent.com/shinokada/awesome/main/test; test)"
输出是:
Something wrong.
如何在 bash 脚本中调用函数? 有可能吗?
【问题讨论】:
-
bash -c "$(curl -s https://raw.githubusercontent.com/shinokada/awesome/main/test)" bash test。这根本不是一个好习惯。 -
@shin:您的脚本中没有函数
test(顺便说一句,将函数命名为 test 是个坏主意)。脚本中的唯一函数是fn_test1和fn_main。 -
脚本没有接收到任何参数
-
@user1934428 OP 想要以
test作为单纯的位置参数运行此脚本,并期望fn_test1中的read -rp能够工作。 -
@user1934428 啊~。也许我应该删除主函数并调用 fn_test1?