bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

read built-in command has a problem in shell function


From: Hyunho Cho
Subject: read built-in command has a problem in shell function
Date: Mon, 11 Oct 2021 15:28:18 +0900

Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -flto=auto -ffat-lto-objects
-fstack-protector-strong -Wformat -Werror=format-security -Wall
uname output: Linux EliteBook 5.11.0-25-generic #27-Ubuntu SMP Fri Jul
9 23:06:29 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.1
Patch Level: 4
Release Status: release

###################################################################################


If i want to know whether there are input available from stdin
then i use "read -t 0" command like this

if read -t 0; then    # first check input available
while read line; do ... done
...
fi

this command works well in *shell script file*
but in shell function does not work well

sh$ myfunc() { if read -t 0; then echo yes; fi ;}

sh$ myfunc <<\EOF            # OK
111
EOF
yes

sh$ myfunc <<< 111           # OK
yes

sh$ echo 111 | myfunc        # OK
yes

sh$ cat foo.c | myfunc        # NOT WORK!

sh$ date | myfunc               # NOT WORK!


#### if i change like this. then this time work well

sh$ date | { date > /dev/null; myfunc ;}
yes



reply via email to

[Prev in Thread] Current Thread [Next in Thread]