Reputation:
I have shell script prompting an answer y/n. at the prompt before giving input, I used control-c signal which calls Signal Handling function. In Signal Handling function there is a prompt "q" to exit or "y" and "y" should be used with the FIRST read prompt.
I tried to (( echo "y" | read ))
but didn't work
==========================================
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
.
.
.
.
.
================
output should be like below:
no valid user id entered, new user ids? [y\n]: #control-c entered
' Use 'q' to quit ' y ------> here "y" entered rather "q" in Siganl Handeling function then it is saved in "ans" variable which gets the condition true to prompt a username.
usernames: Larry -----> the name which is entered after true condition. . . .
Upvotes: 0
Views: 274
Reputation: 360605
Instead of
echo "y" | read
Try:
ans=y
The definition of the trap function needs to be made before the trap is triggered. So I would move it to the top of the file.
When the function finishes, control is returned to the read
statement that was interrupted by the Ctrl-C. So what I would do instead of echo "y" | read
or ans=y
is to reissue the original prompt:
IntHandle ()
{
echo -e "\nUse 'q' to quit "
read var1
if [[ $var1 == q ]]
then
exit 1
else
echo -n 'no valid user id entered, new user ids? [y\n]: '
fi
}
Upvotes: 1