1

I made a bash script for my personal usage which sets up selenium webdriver with appropriate options . Here is its raw link - https://del.dog/raw/edivamubos

If i execute this script using curl after writing it to a file first like..

curl https://del.dog/raw/edivamubos -o test.sh && \
chmod u+x test.sh && \
bash test.sh

The script works perfectly as its intended to work

But usually i like to execute scripts directly using curl , so when i do..

curl https://del.dog/raw/edivamubos | bash

The script works very weirdly , it keeps repeating line 22,23 and 29 infinitely on loop. i couldnt beleive it as first so i tested this 3,4 times and can confirm it.

Now

  1. what is the reason for same script acting differently in both cases ?
  2. How do i fix it ( ie make it work correctly even after executing it directly without writing to a file )

Edit - If someone want they can quickly test this in google colab ( in case someone intending to test but don't want to install any packages locally ) . I am mentioning this thing because you won't be able to reproduce this properly in any bash IDE.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Sachin
  • 1,217
  • 2
  • 11
  • 31
  • Your question should be self-contained and present a [mre]. We should not need to click external links (which often stop working after a while anyway, or worse, lead to sites with undesirable content) to view your code. – tripleee Jul 19 '20 at 19:50

1 Answers1

3

When you pipe the script to bash, this command (line 24):

read -p "Enter your input : " input

reads the next line (i.e. line 25, case $input in) because bash's stdin is connected to curl's stdout, and read reads from the same descriptor as bash.

To avoid that, the developer can change the script so that all input is read from /dev/tty (i.e. the controlling terminal). E.g.:

read -p 'prompt' input </dev/tty

Or the user can use one of the below, so that read reads from the terminal, not the descriptor it was read from.

bash -c "$(curl link)"
bash <(curl link)
oguz ismail
  • 1
  • 16
  • 47
  • 69