我有一个脚本,用于读取用户输入。以下是我的代码:
然而,我发现了一个问题。当我尝试在那里使用变量时,它不会将其翻译:
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?
where I want:
This will overwrite the entire volume (/dev/vg01/server1)...are you sure?
使用双引号也不起作用 :(
有什么想法吗?
read -p $'\t这看起来可以吗?(n表示否)' -n 1 -r
- gniourf_gniourf$“…”
完全做了另一件事。但你不必局限于单引号或双引号。 - Ignacio Vazquez-Abrams