【发布时间】:2016-07-01 16:26:55
【问题描述】:
我有一个脚本,我正在读取用户的输入。这是我的代码:
if [ -z $volreadexists ]; then
echo -e "\tThis will overwrite the entire volume (/dev/vg01/$myhost)...are you sure?"
read REPLY
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "\t\tContinuing"
syncvolume
else
echo "Fine...skipping"
fi
fi
我不得不使用read REPLY,因为read 本身不插入标签。我正在寻找的是类似于:
read -p "\tDoes this look OK? (n for No)" -n 1 -r
\t 将在读取提示上切换。
如何在阅读提示中添加标签?
更新:感谢@gniourf 的精彩回答!:
read -p $'\tDoes this look OK? (n for No)' -n 1 -r
但是,我发现了一个问题。当我尝试在那里使用变量时,它不会翻译它:
read -p $'\tThis will overwrite the entire volume (/dev/vg01/$myhost)...are you sure? ' -n 1 -r
变成
This will overwrite the entire volume (/dev/vg01/$myhost)...are you sure?
我想要的地方:
This will overwrite the entire volume (/dev/vg01/server1)...are you sure?
使用双引号也不起作用:(
有什么想法吗?
【问题讨论】:
-
就用ANSI-C quoting:
read -p $'\tDoes this look OK? (n for No)' -n 1 -r -
看起来不错!!亲爱的,谢谢!
-
@gniourf_gniourf 您真的应该将其添加为答案,以便它可以被投票和(更重要的是)接受。
-
是的,添加一个答案 - 得到业力:)
-
那是因为
$"..."完全做了别的事情。但您并不局限于一组引号。
标签: bash