我正在编写将以方式运行的安装脚本/bin/sh
。
有一行提示输入文件:
read -p "goat can try change directory if cd fails to do so. Would you like to add this feature? [Y|n] " REPLY
我想把这条长行分成多行,以使它们都不超过80个字符。我说的是脚本源代码中的几行;与执行脚本时要在屏幕上实际打印的行无关!
我尝试过的
第一手方法:
read -p "goat can try change directory if cd fails to do so. " \ "Would you like to add this feature? [Y|n] " REPLY
这不起作用,因为它无法打印
Would you like to add this feature? [Y|n]
。第二种方法:
echo "goat can try change directory if cd fails to do so. " \ "Would you like to add this feature? [Y|n] " read REPLY
效果不佳。在提示后,它将打印换行符。向中添加
-n
选项echo
无济于事:它只会打印:-n goat can try change directory if cd fails to do so. Would you like to add this feature? [Y|n] # empty line here
我目前的解决方法是
printf '%s %s ' \ "goat can try change directory if cd fails to do so." \ "Would you like to add this feature? [Y|n] " read REPLY
我想知道是否有更好的方法。
请记住,我正在寻找/bin/sh
兼容的解决方案。
1
我建议您1.使用更宽的终端和/或编辑器窗口(例如,我在1440p显示器上使用192列乘51行的终端-非常适合拖尾日志文件和在vim中编辑源代码)和2.学会接受有时源代码行会超过80个字符-这是不可避免的。如果我使用较小的字体,则可以拥有更宽的终端,但是当前的设置是宽度和易读性之间的良好折衷。
—
cas