【发布时间】:2016-01-25 14:45:56
【问题描述】:
我想从文件中读取存储库名称,显示 YES-NO-CANCEL 对话框,然后如果用户回答“是”则添加存储库。到目前为止,我已经写了这段代码:
g_dlg_yes=1
g_dlg_no=0
g_dlg_cancel=127
show_confirm_dlg()
{
local prompt="$* [y(es)/n(o)/c(ancel)]: "
local resp=""
while [ "$resp" != "y" ] && [ "$resp" != "n" ] && [ "$resp" != "c" ]; do
echo "$prompt"
read resp
done
if [ "$resp" = "y" ]; then
g_dlg_res=$g_dlg_yes
elif [ "$resp" = "n" ]; then
g_dlg_res=$g_dlg_no
elif [ "$resp" = "c" ]; then
g_dlg_res=$g_dlg_cancel
else
g_dlg_res=$g_dlg_cancel
fi
}
add_repo()
{
local filename="/home/stojan/repo.list"
while read -r line
do
local repo_name=$line
echo "Name read from file - $line"
show_confirm_dlg "Add repository $repo_name?"
local rc=$g_dlg_res
if [ $rc -eq $g_dlg_yes ]; then
add-apt-repository -y $repo_name
##echo $repo_name
elif [ $rc -eq $g_dlg_no ]; then
echo "Repository $repo_name rejected"
elif [ $rc -eq $g_dlg_cancel ]; then
echo "Script cancelled"
exit 1
else
echo "Unknown response. Now cancelling..."
exit 1
fi
echo "Press ENTER..."
read _
done < "$filename"
}
add_repo
问题是当我运行这个脚本时,我卡在show_confirm_dlg() 并进入无限循环。那就是脚本不会等待我的输入,而是一遍又一遍地重复确认。
【问题讨论】:
-
如何运行脚本?那里有任何重定向/管道吗?此外,显示的代码只是声明了函数,它不运行任何东西。
-
哦,对不起。复制/粘贴错误。我编辑了我的代码。