a.p*_*anz 8 linux bash shell keystroke special-characters
不确定我是否应该将它放在stackoverflow或unix.stackexchange上,但我在这里发现了一些类似的 问题,所以这里就是这样.
我正在尝试创建一个由.bashrc调用的脚本,它允许我根据单个击键选择两个选项中的一个.这通常不会很难但我希望对应于这两个选项的两个键是空格并输入.
这是我到目前为止所得到的:
#!/bin/bash
SELECT=""
while [[ "$SELECT" != $'\x0a' && "$SELECT" != $'\x20' ]]; do
echo "Select session type:"
echo "Press <Enter> to do foo"
echo "Press <Space> to do bar"
read -s -N 1 SELECT
echo "Debug/$SELECT/${#SELECT}"
[[ "$SELECT" == $'\x0a' ]] && echo "enter" # do foo
[[ "$SELECT" == $'\x20' ]] && echo "space" # do bar
done
Run Code Online (Sandbox Code Playgroud)
如果我按下enter,space,backspace和x,我得到的是以下输出:
:~$ bin/sessionSelect.sh
Select session type:
Press <Enter> to start/resume a screen session
Press <Space> for a regular ssh session
Debug//0
Select session type:
Press <Enter> to start/resume a screen session
Press <Space> for a regular ssh session
Debug//0
Select session type:
Press <Enter> to start/resume a screen session
Press <Space> for a regular ssh session
Debug//1
Select session type:
Press <Enter> to start/resume a screen session
Press <Space> for a regular ssh session
Debug/x/1
Run Code Online (Sandbox Code Playgroud)
因此,输入和空格都会产生一个空的SELECT.无法区分这两者.我试图在读取选项中添加-d'D',但这没有帮助.也许有人可以指出我正确的方向.
bash版本将是4.2 btw.
尝试将读取分隔符设置为空字符串,然后检查内置 $REPLY 变量:
read -d'' -s -n1
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我无法让它工作指定变量。
#!/bin/bash
SELECT=""
# prevent parsing of the input line
IFS=''
while [[ "$SELECT" != $'\x0a' && "$SELECT" != $'\x20' ]]; do
echo "Select session type:"
echo "Press <Enter> to do foo"
echo "Press <Space> to do bar"
read -s -N 1 SELECT
echo "Debug/$SELECT/${#SELECT}"
[[ "$SELECT" == $'\x0a' ]] && echo "enter" # do foo
[[ "$SELECT" == $'\x20' ]] && echo "space" # do bar
done
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6861 次 |
| 最近记录: |