gpt4 book ai didi

bash - BASH 脚本中的“换行意外”错误

转载 作者:行者123 更新时间:2023-12-04 16:16:28 24 4
gpt4 key购买 nike

我有 SHARED.SH 文件:

#!/bin/sh

g_dlg_yes=1
g_dlg_no=0
g_dlg_cancel=2
g_dlg_unknown=127

show_confirm_dlg()
{
prompt=$*
resp=""
while [ "$resp" != "y" ] && [ "$resp" != "n" ] && [ "$resp" != "c" ]; do
echo "${prompt} [y/n/c]: "
read resp
done
case "$resp" in
y ) return g_dlg_yes;;
n ) return g_dlg_no;;
c ) return g_dlg_cancel;;
* ) return g_dlg_unknown;;
Esac
}

我还有 INSTALL.SH 文件:

#!/bin/sh

. ./shared.sh

install_pkg()
{
clear
pkg_name=$*
prompt="Do you want to install ${pkg_name}?"
show_confirm_dlg $pkg_name
res=$?
if [ "$res" -eq g_dlg_cancel ]; then
echo "Installation of $pkg_name cancelled."
exit 2
elif [ "$res" -eq g_dlg_no ]; then
echo "Installation of $pkg_name rejected."
elif [ "$res" -eq g_dlg_yes ]; then
echo "Trying to install $pkg_name..."
apt-get install -y $pkg_name
else
echo "Unknown answer. Now quitting..."
exit 2
fi
echo "Press ENTER to continue..."
read key
}

main()
{
install_pkg "dosbox virtualbox"
exit $?
}

main

当我尝试运行 INSTALL.SH 时,出现以下错误:./install.sh: 22: ./shared.sh: 语法错误:换行意外(预期为“)”)

你能帮我解决这个错误吗?

最佳答案

Bash 命令和语句区分大小写。SHARED.SH 文件中的 esac 命令大小写错误。

#!/bin/sh

g_dlg_yes=1
g_dlg_no=0
g_dlg_cancel=2
g_dlg_unknown=127

show_confirm_dlg()
{
prompt=$*
resp=""
while [ "$resp" != "y" ] && [ "$resp" != "n" ] && [ "$resp" != "c" ]; do
echo "${prompt} [y/n/c]: "
read resp
done
case "$resp" in
y ) return g_dlg_yes;;
n ) return g_dlg_no;;
c ) return g_dlg_cancel;;
* ) return g_dlg_unknown;;
esac
}

关于bash - BASH 脚本中的“换行意外”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30207723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com