I am developing a zsh script that uses read -k. If I execute my script like this (echo a | myscript), it fails to get input. Apparently it is due to the fact that -k uses /dev/tty as stdin invariably, and you must tell read to use stdin as in read -u0.
But then if I change it to -u0 (which makes previous case work) and execute my script without redirecting tty, it breaks the script, it simply does not behave as executing it without -u0.
EDIT: After debugging, it seems the issue is simply that after using -u0, the -k1 option does not read a single char and stops anymore. read works in this case as without -k, simply buffering all input and saving it as soon as an EOL arrives
EDIT2: After more debugging I know it's something related to the raw mode not working with -u0. If I add stty raw/cooked before my read then it works (except enter keystroke is now handled with \r not \n), but then when I execute it with non-tty stdin it breaks.
Is there any way to make both modes compatible?
Indeed I would like to understand why the script behaves different at all, if either I read with -u0 or not, fd0 is by default the same as /dev/tty
readcommand within the script.zshinstalled, but both bash and zsh have a variable called-which contains "Flags supplied to the shell on invocation". In Bash,echo "$-"giveshimBHs, and you can test if the shell is interactive withcase "$-" in (*i*). Also, the promptPS1is empty in non-interactive Bash shells. You could test if either of these in zsh helps you discriminate between keyboard and piped input.