You are not logged in.
Pages: 1
I am trying to simulate
read -n 1
of bash in dash script.
The following code works but it can not handle arrow keys like ^[[A and it will show [A in terminal after exiting the script if up arrow key is pressed.
#!/bin/dash
echo -n "Enter Character: "
stty -echo -icanon time 0 min 1
CHA=$(head -c 1)
stty sane
echo "you pressed: $CHA"
How can I handle that residue [A after exiting the script.
I know read -n1 of bash but I want that in dash in purpose of testing.
Last edited by duyinthee (2025-01-15 03:22:23)
Offline
Since you are likely to make a posix script. Have you know this tool ? https://archlinux.org/packages/extra/x86_64/shellcheck/
Let you check your shell script syntax to know if it's posix or not.
If I'm not wrong, I think you lack some " " in the variable of CHA. At least that's what my memory about posix scripts is saying to me, but not quite sure. Can you tell me what shellcheck is saying to you ? try to fix the issues if you have it.
Also why not use read -n 1 ? Are you trying to not use gnu core utils ? If you are not trying to avoid gnu flags in your commands you should be able to use the read -n1 because it is in your core utils.
Last edited by Succulent of your garden (2025-01-15 12:35:07)
Offline
Also why not use read -n 1
Cause that doesn't work in dash.
it can not handle arrow keys like ^[[A and it will show [A
That has actually nothhing to do w/ your script, try running "dash -E" (man dash - ther's also a vi mode for really real men
Offline
Dash (Debian Almquist shell) is a modern POSIX-compliant implementation of /bin/sh (sh, Bourne shell).
This presumably means it can run on any *nix?
Offline
To read one character with POSIX sh use
CHA=$(dd bs=1 count=1 2>/dev/null)
Para todos todo, para nosotros nada
Offline
Whether you're using head or dd, the magic is in "stty -echo -icanon time 0 min 1"
read -n1 (or read -q in zsh) isn't about getting only one byte, but about terminating immediately afterwards (w/o having to enter or EOF)
Offline
My point was that the head command has no -c option in POSIX:
https://pubs.opengroup.org/onlinepubs/9 … /head.html
Portability ftw! :-)
Para todos todo, para nosotros nada
Offline
https://man.archlinux.org/man/head.1p.en
Tsss… even busybox and bsd support that
"Pathetic Operating System Interface"
Offline
Also why not use read -n 1
Cause that doesn't work in dash.
Really ? D: Why not ? does the -n flag is not posix and dash can't understand it even if the core util is installed ?
This presumably means it can run on any *nix?
It can run in any *nix . Dash is one of the fastest shells available. Not the best looking feel and probably no the best everyday shell for many development environments, but for operations that need to have a fast response or if you want to optimize you time response because you can, then it's a good choice
BTW bash is slow like a snail in comparison to Dash, but most of the time is a good snail.
EDIT: BTW your terminal also could affect the time response. If you go Alacrity with Dash probably you will go back in time because of how fast that could be.
Last edited by Succulent of your garden (Yesterday 11:47:00)
Offline
does the -n flag is not posix and dash can't understand it even if the core util is installed ?
$ type read
read is a shell builtin
$
Para todos todo, para nosotros nada
Offline
That has actually nothhing to do w/ your script,
This made me think outside the box. Found a workaround for this moment but not good. Thanks anyway.
$ ./myscript && wtype -P home -p home -M ctrl k -m ctrl
btw, wtype man page
Offline
Hold on, you want to be able to read the arrow?
"read -n1"/"read -q" will however act exactly the same since it's actually 3 bytes.
Offline
Pages: 1