1

Consider this bash script :

#!/bin/bash
while true; do
    read -p "Give me an answer ? y/n : " yn
    case $yn in
        [Yy]* ) answer=true ; break;;
        [Nn]* ) answer=false ; break;;
        * ) echo "Please answer yes or no.";;
    esac
  done

if $answer 
  then 
    echo "Doing something as you answered yes"
      else 
    echo "Not doing anything as you answered no" 
fi

When run from the command line using :

$ ./script-name.sh

It works just as expected with the script waiting for you to answer y or n.

However when I upload to a url and attempt to run it using :

$ curl http://path.to/script-name.sh | bash

I get stuck in a permanent loop with the script saying Please answer yes or no. Apparently the script is receiving some sort of input other than y or n.

Why is this? And more importantly how can I achieve user input from a bash script called from a url?

4
  • You've redirected standard input for the shell already. That's where your script has just come from. So when bash tries to read more information from standard input it just gets EOF and spins. Commented Nov 11, 2014 at 19:26
  • 1
    Try read -p "Give me an answer ? y/n : " yn < /dev/tty. Commented Nov 11, 2014 at 20:01
  • well cat script-name.sh | bash would not work better than your curl command either... Commented Nov 11, 2014 at 20:32
  • use zentity ``` Commented Jul 10, 2021 at 17:04

2 Answers 2

8

Perhaps use an explicit local redirect:

read answer < /dev/tty
Sign up to request clarification or add additional context in comments.

2 Comments

The problem I'm having with this answer is that the read happens before the echos explaining what to type happen...
@DavidSulpy try this: read -p "Enter answer:" answer < /dev/tty
6

You can run it like this:

bash -c "$(curl -s http://path.to/script-name.sh)"

Since you're supplying content of bash script to bash interpreter. Use curl -s for silent execution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.