【发布时间】:2019-10-22 15:00:21
【问题描述】:
我有 shell 脚本提示回答 y/n。在给出输入之前的提示下,我使用了调用信号处理函数的 control-c 信号。在信号处理函数中有一个提示“q”退出或“y”和“y”应该与第一个读取提示一起使用。
我尝试(( echo "y" | read )),但没有成功
==========================================
This is part of my script:
IntHandle ()
{
echo -e "\nUse 'q' to quit "
read var1
if [[ $var1 == q ]]
then
exit 1
else
echo "y" | read ----->here I need "y" to be an input to read prompt
directly and being saved in "ans" variable in
main body where I used control-c
fi
}
trap 'IntHandle' SIGINT
read -p "no valid user id entered, new user ids? [y\n]: " ans ----> here
used control-c signal before give y/n to ans
if [[ $ans == "y" ]]
then
read -p " username :" name
fi
.
.
.
.
.
=================
输出应如下所示:
没有输入有效的用户 ID,新的用户 ID? [y\n]: #control-c 输入
' 使用 'q' 退出 ' y ------> 这里 "y" 输入了 Siganl Handeling 中的 "q" 函数然后它被保存在“ans”变量中 条件为真提示用户名。
usernames: Larry -----> 真实条件后输入的名字。 . . .
【问题讨论】:
-
4 个空格 before 脚本的每一行,以便其格式正确。在段落中将反引号(例如
'`')与format as code之间的内联代码括起来。"============================"和"In main body of my script:"不是您的脚本的一部分。
标签: shell