1

I use Ubuntu 16.04 (xenial) via WSL.

I copy this code

#!/bin/bash
read -p "Have you created db credentials already?" yn
case $yn in
    [Yy]* ) break;;
    [Nn]* ) exit;;
esac

When I paste it in Ubuntu I get only part of it:

read -p "Have you created db credentials already?" yn
Have you created db credentials already?

Why when it is pasted, it is pasted partially?

Update

I copy the code from GitHub, when the case conditions are indented with tabs and when the code is without a line break in the last line (no extra empty note after the last line).

  • Can't understand why this was down voted. – JohnDoea Nov 18 '18 at 6:02
  • This usage is incorrect. Use shell script file instead of copying and pasting into terminal. – Alvin Liang Nov 18 '18 at 19:28
  • @AlvinLiang I see no reason why it's necessarily incorrect, I have good experience with copy-pasting (when I find it elegant) or executing a script directly from GitHub without downloading it with git which is even more elegant: askubuntu.com/questions/992448/… but yes, indeed the usual way is to download a program with git and execute locally, it's just not something I want to do here. – JohnDoea Nov 18 '18 at 22:41
  • 1
    It's incorrect because paste is an input behavior, and read -p expects user input. Even if it does not stop your paste the behavior will not be correct. – Alvin Liang Nov 19 '18 at 2:09
  • 1
    @AlvinLiang seems what you write indicated it can only be run as part of a script and not as a command-set pasted in the terminal. This should be an answer IMO. – JohnDoea Nov 19 '18 at 2:11
3

As soon as "read -p" line is invoked, terminal clears your original paste buffer because it's expecting meaningful user input, and you cannot just paste it into terminal like this.

If you don't feel like creating a script file, press ctrl-x and then ctrl-e in the terminal, it will bring up the default editor. Paste it into the editor, save and exit, dash will run you script correctly from a temp file.

  • The correct default behavior: after press ctrl+x and then ctrl+e, the nano editor should be brought up. Paste you script in to the nano editor and save/exit, dash will run the script. – Alvin Liang Nov 19 '18 at 2:59
  • nano does bring nano up. Anyway, I created x.sh with the code and executed the file. Hitting N executed break but hitting Y brought an error; x.sh: line 4: break: only meaningful in a for', while', or until' loop`. Do you have an idea why? – JohnDoea Nov 19 '18 at 3:11
  • 1
    This is correct response, because your break command is meaningless here. You don't need to add break in every switch option like in C. – Alvin Liang Nov 19 '18 at 3:15

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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