0

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. . . .

2

This is a X-Y problem. Why on earth use read for setting a variable in a script?

Set it the normal way; ans=y

This would make the function look like this:

IntHandle ()
{ 
  echo -e "\nUse 'q' to quit "
  read var1 
  if [[ $var1 == q ]]
  then
      exit 1
  else
      ans=y
  fi
}
  • First, I did before "ans=y" but did not work because I am still being prompted to enter "y" or "n" in the first prompt which it should not. Just when I enter "y" rather than "q" in the function prompt, it would set "ans" variable by "y" and skip me the prompt. – RedHat Jun 9 at 0:58
  • I need to skip the stdin in the first prompt when I use control-c signal before giving "y" or "n" to ans variable. – RedHat Jun 9 at 1:00
  • No, you need to modify the flow of the program to skip that prompt if the value is set. Whenever ^C is pressed, your code will jump to IntHandle(). How can that be used to skip the prompt? I'm trying to nudge you along here, because, as I said, you have put your eyes on a solution, which is wrong. – vidarlo Jun 9 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.