我使用bash内置命令编写了一个shell脚本select
创建选择菜单。调用时效果很好bash
直接地。但如果我使用管道|
例如cat script.sh | bash
, the select功能将无法工作。
例如,代码片段显示
#!/usr/bin/env bash
arr=("Red Hat" "SUSE" "Debian")
PS3="Choose distribution:"
result=${result:-}
select item in "${arr[@]}"; do
result="${item}"
[[ -n "${result}" ]] && break
done
echo "Result is ${result}"
unset PS3
直接使用bash script.sh
, 效果很好。
$ bash /tmp/test.sh
1) Red Hat
2) SUSE
3) Debian
Choose distribution:1
Result is Red Hat
使用管道|
,它会输出
$ cat /tmp/test.sh | bash
1) Red Hat
2) SUSE
3) Debian
Choose distribution:1) Red Hat
2) SUSE
3) Debian
Choose distribution:Choose distribution:Choose distribution:
这使得脚本无法工作。
我不知道为什么?做select
不支持管道|
或者其他什么?
select
从标准输入读取。stdin
来自管道。如果你想从tty获取数据,你可以尝试:
#!/usr/bin/env bash
arr=("Red Hat" "SUSE" "Debian")
PS3="Choose distribution:"
result=${result:-}
select item in "${arr[@]}"; do
result="${item}"
[[ -n "${result}" ]] && break
done < /dev/tty # <------------------
echo "Result is ${result}"
unset PS3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)