简体   繁体   中英

Running bash script after WGET ignores read user input

I'm trying to create a bash script that can be easily downloaded and run from terminal. Inside the bash script there are a couple of user inputs.

Running:

sudo wget -O - https://script.com/script.sh | bash

This will download the script and run it with bash but will ignore the user input in script.sh:

echo "Please enter a username:"
read -p 'Username: ' uservar
echo "Please enter a strong password:"
read -sp 'Password: ' passvar
echo
echo "
user=$uservar
password=$passvar
" >> ~/.data/cred.conf
echo "Succesfully installed."

Running:

sudo wget -O - https://script.com/script.sh && bash ./script.sh

Works fine and prompts for user input. Is there any way to make user input work with a pipe ( | bash ) ?

from comment

| bash <(cat) </dev/tty

why and how it works

  • <(cat) construct allows
    • to create a process which reads from inherited input, at this point, it's the pipe output
    • to redirect output to a file descriptor used as a file and process expansion is changed to a string used as a file name
  • </dev/tty redirects pseudo terminal device /dev/tty to input of bash process

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM