0
# user input a here
vared -p "input something" -c a
case $a { ([y|'\n']) echo something;; }

won't work, can someone give an insight?

I'm trying to test against user input that if contain y or an enter - newline, echo something.

EDIT:

set | grep IFS
IFS=$' \t\n\C-@'
1

You're not saying how you're getting user input.

With read:

read -k 'answer?Are you OK? '
case $answer in
  (y | Y | $'\n') echo 1;;
  # or ([$'yY\n']) echo 1;;
  (*)         echo 2;;
esac

But if you're using read without -k or vared and want to detect when the user presses Enter without entering any answer, that's when you would want to test for an empty value:

answer=; vared -p 'Are you OK? ' answer
case $answer in
  (y | Y | "") echo 1;;
  # or ([yY] | "") echo 1;;
  (*)         echo 2;;
esac

Or seed the answer with y (though the user would have to do BackspaceN to say no):

answer=y; vared -p 'Are you OK? ' answer
case $answer in
  (y | Y) echo 1;;
  (*)     echo 2;;
esac
  • I used vared -p "input something" -c a to get input, I don't think your answer works for me. Did I miss something? – Tuyen Pham Sep 13 '18 at 9:54
  • @TuyenPham, vared is to edit the content of a variable. To get a newline character into that variable, the user would have to type Ctrl+V Ctrl+J, or Alt+Enter, is that really what you want? Or do you want to check whether the user has entered an empty input (like when they press Enter when the editing buffer is empty)? – Stéphane Chazelas Sep 13 '18 at 11:40
  • Yes, with vared that I mentioned above I'd like to capture that user input is enter or not? if enter echo something. Because -c I believe the variable is create if not exist. I only check for enter not an empty input like space. – Tuyen Pham Sep 13 '18 at 11:45
  • @TuyenPham, it's still unclear what you want. "Enter" in vared is for accepting the current value. You also need to press "Enter" after having edited the variable value so as to contain "y" or "n" (for instance by pressing "y" or "n" if the variable was initially empty). Pressing space adds a space character to the content of the variable, it makes it non-empty. Do you want to check that the user didn't modify the value of the variable? – Stéphane Chazelas Sep 13 '18 at 12:38
  • 1
    @TuenPham, sounds like you want a=y; vared -p "input something: " a – Stéphane Chazelas Sep 13 '18 at 15:07

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.