You are not logged in.

#1 2025-01-15 03:20:58

duyinthee
Member
Registered: 2015-06-14
Posts: 232
Website

Simulating read -n1 in dash

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

#2 2025-01-15 12:33:16

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 122

Re: Simulating read -n1 in dash

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

#3 2025-01-15 16:20:51

seth
Member
Registered: 2012-09-03
Posts: 60,466

Re: Simulating read -n1 in dash

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

#4 2025-01-15 16:47:55

ReDress
Member
Registered: 2024-11-30
Posts: 69

Re: Simulating read -n1 in dash

duyinthee wrote:

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

#5 2025-01-15 17:22:00

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,642
Website

Re: Simulating read -n1 in dash

To read one character with POSIX sh use

CHA=$(dd bs=1 count=1 2>/dev/null)

Para todos todo, para nosotros nada

Offline

#6 2025-01-15 17:45:32

seth
Member
Registered: 2012-09-03
Posts: 60,466

Re: Simulating read -n1 in dash

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

#7 2025-01-15 17:53:25

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,642
Website

Re: Simulating read -n1 in dash

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

#8 2025-01-15 21:14:58

seth
Member
Registered: 2012-09-03
Posts: 60,466

Re: Simulating read -n1 in dash

https://man.archlinux.org/man/head.1p.en
Tsss… even busybox and bsd support that roll
"Pathetic Operating System Interface" wink

Offline

#9 Yesterday 11:42:35

Succulent of your garden
Member
Registered: 2024-02-29
Posts: 122

Re: Simulating read -n1 in dash

seth wrote:

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 ?

ReDress wrote:

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 smile

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

#10 Yesterday 12:20:10

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,642
Website

Re: Simulating read -n1 in dash

Succulent of your garden wrote:

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

#11 Yesterday 12:21:06

duyinthee
Member
Registered: 2015-06-14
Posts: 232
Website

Re: Simulating read -n1 in dash

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

#12 Yesterday 16:49:29

seth
Member
Registered: 2012-09-03
Posts: 60,466

Re: Simulating read -n1 in dash

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

Board footer

Powered by FluxBB