0

I have a simple script.

# test.sh

read -p ">> " user
read -p ">> " -n1 yes

echo

echo "user: $user"
echo "yes: $yes"

When executed as bash test.sh it prompts to input values and stores them in variables user and yes. So output is like this

user@host:~/static/dcc$ bash test.sh 
>> foo
>> b
user: foo
yes: b

However when cat test.sh is piped to bash, it doesn't work, instead, read eats everything after it and stores it in user variable as it seems (doesn't prompt for input).

user@host:~/static/dcc$ cat test.sh | bash

user: read -p ">> " -n1 yes
yes: 

Using process substitution works fine, however not if bash is run as sudo (which is what i need)

user@host:~/static/dcc$ bash <(cat test.sh)
>> user
>> y
user: user
yes: y
user@host:~/static/dcc$ sudo bash <(cat test.sh)
bash: /dev/fd/63: No such file or directory

In real world example, I'm trying to get script from web using curl and to run it as with sudo. I would like to avoid additional step of downloading the script first if at all possible.

1 Answer 1

0

Well, read reads from stdin, and you're piping in the rest of your script through stdin... so, this can't work.

You will have to download the file. Your substitution trick works just fine, but it works so well, it's done in the shell that calls sudo, not in the shell that sudo calls. You can trivially circumvent that:

sudo bash -c 'bash <(curl https://scripttodownload)'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .