Bash 'read' 命令在 Mac 上不接受 -i 参数。任何替代方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22634065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash 'read' command does not accept -i parameter on Mac. Any alternatives?
提问by Matt Fletcher
I have a bash script that works fine on my work Ubuntu machine, but sadly breaks when I try and run it on my Mac OSX LionMountain Lion laptop. The line that kills it is this:
我有一个 bash 脚本,它在我的工作 Ubuntu 机器上运行良好,但是当我尝试在我的 Mac OSX LionMountain Lion 笔记本电脑上运行它时,我很遗憾地中断了。杀死它的行是这样的:
while [[ -z "$SSHFS_PATH" ]] ; do
read -e -p "Please enter the path on which to mount your file system: `echo -e $'\n > '`" -i "~/aws-dev" SSHFS_PATH;
done
It throws out this error:
它抛出这个错误:
-bash: read: -i: invalid option
read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
So it seems the OSX version of the read command doesn't accept -i
, which is used to suggest default values. Why? And what can be done to fix this?
所以似乎 OSX 版本的 read 命令不接受-i
,它用于建议默认值。为什么?可以做些什么来解决这个问题?
Thanks :)
谢谢 :)
回答by Adrian Frühwirth
Mac OS X 10.7 Lion (and to this date all more recent versions as well, thanks @kojiro) ships with bash 3.2
whereas read -i
was introduced with bash 4.0-alpha
(see the ChangeLog).
Mac OS X 10.7 Lion(以及迄今为止所有更新的版本,感谢@kojiro)随附bash 3.2
而read -i
引入bash 4.0-alpha
(请参阅ChangeLog)。
You can either install a more recent version of bash
using homebrew
or provide a non-readline default value yourself, e.g.
您可以安装更新版本的bash
usinghomebrew
或自己提供非 readline 默认值,例如
read -p "Path? (default: /bar): " var
[ -z "${var}" ] && var='/bar'
回答by Maxim Belkin
One could write a wrapper function that detects whether -i
is supported and use the appropriate syntax:
可以编写一个包装函数来检测是否-i
受支持并使用适当的语法:
function readinput() {
local CLEAN_ARGS=""
while [[ $# -gt 0 ]]; do
local i=""
case "$i" in
"-i")
if read -i "default" 2>/dev/null <<< "test"; then
CLEAN_ARGS="$CLEAN_ARGS -i \"\""
fi
shift
shift
;;
"-p")
CLEAN_ARGS="$CLEAN_ARGS -p \"\""
shift
shift
;;
*)
CLEAN_ARGS="$CLEAN_ARGS "
shift
;;
esac
done
eval read $CLEAN_ARGS
}
and then
进而
readinput -e -p "This is a test of... " -i "default value" variable
variable=${variable:-default value}
Note: if you call this function read
, it will then replace the not-so-functional builtin.
注意:如果您调用此函数read
,它将替换功能不那么强大的内置函数。
回答by suspectus
OSX bash is version 3. Linux users enjoy the additional features of bash version 4.
OSX bash 是版本 3。Linux 用户可以享受 bash 版本 4 的附加功能。
Using parameter substitution ${VAR:-def-value}
if $VAR is unset then def-value is returned.
${VAR:-def-value}
如果 $VAR 未设置,则使用参数替换,则返回 def-value。
DEFPATH="~/aws-dev"
while [[ -z "$SHFS_PATH" ]] ; do
echo "Please enter the path on which to mount your file system"
read -p "default is $DEFPATH " SHFS_PATH
SHFS_PATH=${SHFS_PATH:-~/aws-dev}
echo $SHFS_PATH
done