英文:
Alternative for bash's 'read -t' in POSIX shell
问题
I'm currently working on a college project where I have to do several shell script files. In one of them, I have to create a file called taunt.sh
that uses the command cowsay
to print some messages depending on some actions:
- If no message is received after 10 seconds then the
taunt.sh
script should issue a funny message and finish/exit. - If it receives the
SIGHUP
signal, then it should issue a special message and exit. - If it receives a
SIGINT
orSIGTERM
, then it should issue a taunt and terminate/exit.
The thing is, I need to use #!/bin/sh
and not #!/bin/bash
.
I know in bash
we can use read -t 10 input
to wait for 10 seconds while an input is received, and we could exit of the waiting process after receiving some input.
But this is not an option in sh
.
So, is there a way to implement some similar functionality in sh
?
I tried this
#!/bin/sh
echo "Enter your input:"
sleep 10 &
read input
kill $! 2>/dev/null
if [ -z "$input" ]; then
echo "No input provided"
else
echo "You entered: $input"
fi
But it never got out after 10 seconds, only if I typed something.
I'm pretty new to sh
and still takes me a while to understand some things. So I hope somebody can help me with this.
英文:
I'm currently working on a college project where I have to do several shell script files. In one of them, I have to create a file called taunt.sh
that uses the command cowsay
to print some messages depending on some actions:
- If no message is received after 10 seconds then the
taunt.sh
script should issue a funny message and finish/exit. - If it receives the
SIGHUP
signal, then it should issue a special message and exit. - If it receives a
SIGINT
orSIGTERM
, then it should issue a taunt and terminate/exit.
The thing is, I need to use #!/bin/sh
and not #!/bin/bash
.
I know in bash
we can use read -t 10 input
to wait for 10 seconds while an input is received, and we could exit of the waiting process after receiving some input.
But this is not an option in sh
.
So, is there a way to implement some similar functionality in sh
?
I tried this
#!/bin/sh
echo "Enter your input:"
sleep 10 &
read input
kill $! 2>/dev/null
if [ -z "$input" ]; then
echo "No input provided"
else
echo "You entered: $input"
fi
But it never got out after 10 seconds, only if I typed something.
I'm pretty new to sh
and still takes me a while to understand some things. So I hope somebody can help me with this.
答案1
得分: 3
以下是已翻译的内容:
这是一个 POSIX
版本的bash的 read -t 10
:
#!/usr/bin/env sh
timeout=10
mystty="$(stty -g)"
trap 'stty "$mystty"' EXIT
stty -icanon min 0 time $((timeout*10))
echo "请输入您的输入"
read input
if [ -z "$input" ]; then
echo "未提供输入"
else
echo "您输入的是: $input"
fi
mystty="$(stty -g)"
:将stty
设置保存在变量中。trap 'stty "$mystty"' EXIT
:这一行为EXIT
信号设置了一个陷阱。在这种情况下,当脚本退出时,无论是通过ctrl+c
还是其他退出方式,它都会恢复终端的设置。stty -icanon min 0 time 100
:这一行使用stty
命令修改终端设置。-icanon
标志禁用规范模式,这意味着输入会立即处理,而不会等待换行符。min 0
设置表示终端不会等待任何字符输入即可返回。time 100
设置了一个100个十分之一秒(10秒)的超时来读取输入。在此模式下,如果在指定的超时内未收到输入,读取操作将立即返回。
【注】:POSIX trap exit
英文:
Here a POSIX
version of the bash's read -t 10
:
#!/usr/bin/env sh
timeout=10
mystty="$(stty -g)"
trap 'stty "$mystty"' EXIT
stty -icanon min 0 time $((timeout*10))
echo "Enter your input "
read input
if [ -z "$input" ]; then
echo "No input provided"
else
echo "You entered: $input"
fi
mystty="$(stty -g)"
savestty
settings in a variabletrap 'stty "$mystty"' EXIT
: This line set a trap for theEXIT
¹ signal. In this case, when the script exits, it will restores those settings in any cases (<kbd>ctrl+c</kbd> or any exit cases) for the following terminal modifications :stty -icanon min 0 time 100
: This line modifies the terminal settings using thestty
command. The-icanon
flag disables canonical mode, which means that input is processed immediately, without waiting for a newline character. Themin 0
setting means that the terminal will not wait for any characters to be typed before returning input. Thetime 100
set a timeout of 100 deciseconds (10 seconds) to read input. In this mode, if no input is received within the specified timeout, the read operation will return immediately.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论