Sample Header Ad - 728x90

Issue of read with -u and -k in zsh

0 votes
1 answer
204 views
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
Asked by Whimusical (285 rep)
Mar 11, 2024, 12:32 AM
Last activity: Mar 11, 2024, 07:12 PM