dwkns
dwkns

Reputation: 2449

How do you get user input when running a bash script from a url?

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?

Upvotes: 1

Views: 3187

Answers (2)

anubhava
anubhava

Reputation: 785128

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.

Upvotes: 6

Scrutinizer
Scrutinizer

Reputation: 9926

Perhaps use an explicit local redirect:

read answer < /dev/tty

Upvotes: 8

Related Questions