anishathalye / dotbot

A tool that bootstraps your dotfiles ⚡️
MIT License
6.91k stars 288 forks source link

`read` command ignored when script executed as part of `install.conf.yaml`, but works when executed manually in the terminal. #295

Closed harluss closed 2 years ago

harluss commented 2 years ago

I have a script asking user to sign into App Store:

#!/usr/bin/env zsh

echo "App Store signin is required for MAS cli."

while true; do
  read "answer?Open App Store? [y/n]: "
  case ${answer} in
    [Yy]) echo "Sign in and close App Store to continue."; open -a "App Store" -W; break;;
    [Nn]) break;;
    *) echo "Incorrect answer.";;
  esac
done

When executed directly (by itself) in the terminal, it will prompt Open App Store? [y/n] and wait for user's input. But when executed as part of install.conf.yaml, like this:

- shell:
    - description: 'Checking App Store signin'
      command: scripts/appstore.zsh

It will display the first echo, then go into infinite loop ignoring everything inside, including the read line.

I have tried it the bash way as well i.e. read -r -p "Open App Store? [y/n] :" answer but it still goes into infinite loop and throws an error as -p in zsh works differently than in bash. I have tried the read command without the loop and it's completely ignored when the script is executed as part of install.conf.yaml, but again works as expected when I execute the script manually.

Any idea why?

anishathalye commented 2 years ago

You need to enable stdin (to get user input), stdout (to show the echos), and stderr (to show the prompt for read). Try this:

- shell:
    - description: 'Checking App Store signin'
      command: scripts/appstore.zsh
      stdin: true
      stdout: true
      stderr: true
harluss commented 2 years ago

Ah, ok. I had stdout and stderr enabled through defaults, but completely missed stdin. It worked like a charm, thank you.