简体   繁体   English

在Bash Shell的内置命令“ read”中使用选项-i的平台安全方法是什么(用于预加载带有值的编辑bugger)

[英]What's a platform safe way to use Bash Shell's Built-in command “read” with option -i (for preloading the edit bugger with a value)

Example usage that works on Ubuntu and other forms of Linux but not on Mac OS X. 用法示例可在Ubuntu和其他形式的Linux上使用,但不适用于Mac OSX。

while true; do
  read -e -n 1 -p "Do you like Pie? > " -i y ANSWER 
    case $ANSWER in
       [Nn]* ) exit;;
       [Yy]* ) `./calculate-pi.sh`; break;;
       * ) echo "Please answer y or n.";;
    esac
done

Looks like the -i flag was introduced with Bash 4.x. 看起来-i标志是Bash 4.x引入的。 OS X still ships with Bash 3.2.48(1)-release. OS X仍随附Bash 3.2.48(1)-发行版。 I don't think there's any way to preload the answer with text in Bash 3.x. 我认为没有任何方法可以在Bash 3.x中用文本预加载答案。 Typically this is solved by treating the empty response as the default, and indicating this in the prompt (eg "Do you like Pie? [Yn]> " ). 通常,这可以通过将空响应作为默认值并在提示中进行指示来解决(例如"Do you like Pie? [Yn]> " )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM