Bash Read Input-Tab显示提示
我有一个脚本,我正在读取用户的输入。这是我的密码:Bash Read Input-Tab显示提示,bash,Bash,我有一个脚本,我正在读取用户的输入。这是我的密码: 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\tCo
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
将在读取提示上单击tab键
如何将选项卡添加到读取提示
更新:感谢@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?
使用双引号也不起作用:(
有什么想法吗?我最后引用了这个答案: 并创建了一个变通方法。它并不完美,但它可以:
myhost="server1"
if [ -z $volreadexists ]; then
read -e -i "$myhost" -p $'\tJust checking if it\'s OK to overwrite volume at /dev/vg01/'
echo
if [[ $REPLY =~ ^$myhost[Yy]$ ]]; then
echo -e "\t\tContinuing"
else
echo "Fine...skipping"
fi
fi
只需使用:
现在,如果您也想使用变量展开式,可以混合使用不同的引号,如下所示:
read -p $'\t'"This will overwrite the entire volume (/dev/vg01/$myhost)...are you sure? " -n 1 -r
这里我只对制表符使用了ANSI-C引号。请确保在
$'\t'
和“This will….”
之间不要留下任何空格,只需使用:read-p$'\t这个看起来好吗?(n代表否)--n1-r
看起来不错!!太好了,thx!@gniourf\n你真的应该把它作为一个答案添加进去,这样就可以进行升级和修改了(更重要的是)接受。是的,添加一个答案-get karma:)这是因为$“…”
完全做了其他事情。但你不局限于一组引号。
read -p $'\t'"This will overwrite the entire volume (/dev/vg01/$myhost)...are you sure? " -n 1 -r