gpt4 book ai didi

Bash 读取输入 - Tab 提示符

转载 作者:行者123 更新时间:2023-11-29 09:27:35 28 4
gpt4 key购买 nike

我有一个脚本可以读取用户的输入。这是我的代码:

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

现在,如果你也想使用变量扩展,你可以像这样混合不同的引号:

read -p $'\t'"This will overwrite the entire volume (/dev/vg01/$myhost)...are you sure? " -n 1 -r

这里我只对制表符使用了 ANSI-C 引号。确保在 $'\t'"This will..." 之间没有任何空格。

关于Bash 读取输入 - Tab 提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38150104/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com