Home > other >  bash: filling user read input automatically with param list
bash: filling user read input automatically with param list

Time:03-06

I'm trying to use Wireguard road-warrior script from here https://github.com/Nyr/wireguard-install

But i cant make it run in noninteractive mode with predefined parameters. I've read some other similar topics here about how to provide answers to bash "read", but suggestions from there doesn't work.

I've tried this:

# bash wireguard-install.sh < params
# printf '%s\n' 51822 clientcustom 2 y | bash wireguard-install.sh
# echo "51822 clientcustom 2 y" | bash wireguard-install.sh

in every case installer just uses default values. What am i doing wrong?

CodePudding user response:

https://github.com/Nyr/wireguard-install/blob/master/wireguard-install.sh#L15

Indeed, this is a bit problematic. Typically, not caring, you would just:

( sleep 1; printf '%s\n' 51822 clientcustom 2 y ) | ...

A real robust solution, you would parse the output of the process to know when to write response, either with expect or Bash or something better.

coproc bash wireguard-install.sh
while IFS= read -r -u "${COPROC[0]}" line; do
    case "$line" in
    "IPv4 address [1]:"*) echo something >&"${COPROC[1]}"; ;;
    "other prompt"*) echo other stuff >&"${COPROC[1]}"; ;;
    "etc...") echo ... ;;
    esac
done
  •  Tags:  
  • bash
  • Related