我如何在Bash中输出一个多重字符串,而不使用多个echo调用,像这样:
echo "usage: up [--level <n>| -n <levels>][--help][--version]"
echo
echo "Report bugs to: "
echo "up home page: "
我正在寻找一种可移植的方法来做到这一点,只使用Bash内置程序。
我如何在Bash中输出一个多重字符串,而不使用多个echo调用,像这样:
echo "usage: up [--level <n>| -n <levels>][--help][--version]"
echo
echo "Report bugs to: "
echo "up home page: "
我正在寻找一种可移植的方法来做到这一点,只使用Bash内置程序。
当前回答
在这里,文档通常用于此目的。
cat << EOF
usage: up [--level <n>| -n <levels>][--help][--version]
Report bugs to:
up home page:
EOF
所有boure派生的shell(包括所有版本的Bash)都支持它们。
其他回答
同样,对于缩进的源代码,您可以使用<<-(带末尾破折号)来忽略前面的制表符(但不能忽略前面的空格)。
比如这个:
if [ some test ]; then
cat <<- xx
line1
line2
xx
fi
输出不带前导空格的缩进文本:
line1
line2
或者你可以这样做:
echo "usage: up [--level <n>| -n <levels>][--help][--version]
Report bugs to:
up home page: "
以下是我的做法:
function help_text {
printf "\n\
Usage: ./cpanel-to-cc.sh [arguments] ... \n\
Examples: \n\
\t ./cpanel-to-cc.sh --client-id 123123 --api-key abc123def456 --domain example.com \n\
\t ./cpanel-to-cc.sh --client-id 123123 --tmp-dir /home/user/cpanel-to-cc \n\
\t ./cpanel-to-cc.sh --resync --domain example.com \n\
\t ./cpanel-to-cc.sh --purge \n\
\n\
Arguments: \n\
Option \t\t\t Long option \t\t\t Function \n\
-c <id> \t\t --client-id <id> \t\t Specify the SiteHost Client ID \n\
-k <key> \t\t --api-key <key> \t\t Specify the SiteHost API key with access to Cloud, Job and Server modules \n\
-d <domain> \t\t --domain <domain> \t\t The cPanel domain to migrate. If not specified we try migrate all \n\
-t <directory> \t --tmp-dir <directory> \t\t Directory to store temporary files and logs. Default is: $TMP_DIR \n\
-v \t\t\t --verbose \t\t\t Print debugging/verbose information \n\
-y \t\t\t --assume-yes \t\t\t Automatic yes to prompts. Assume \"yes\" as answer to all prompts \n\
-r \t\t\t --resync \t\t\t Use credentials stored and copy data into Container already created. \n\
-p \t\t\t --purge \t\t\t Remove any metadata stored on the the server. This removes any files in: $TMP_DIR \n\
-h \t\t\t --help \t\t\t Display this help and exit \n\
\n"
}
还有一件事,使用printf和预定义的变量(这里:msg)作为模板。
msg="First line %s
Second line %s
Third line %s
"
one='additional message for the first line'
two='2'
tri='this is the last one'
printf "$msg" "$one" "$two" "$tri"
这个^^^将按指定的顺序打印整条消息,并插入额外的vars而不是%s。
Since I recommended printf in a comment, I should probably give some examples of its usage (although for printing a usage message, I'd be more likely to use Dennis' or Chris' answers). printf is a bit more complex to use than echo. Its first argument is a format string, in which escapes (like \n) are always interpreted; it can also contain format directives starting with %, which control where and how any additional arguments are included in it. Here are two different approaches to using it for a usage message:
首先,你可以在格式字符串中包含整个消息:
printf "usage: up [--level <n>| -n <levels>][--help][--version]\n\nReport bugs to: \nup home page: \n"
注意,与echo不同,必须显式地包含最后的换行符。此外,如果消息碰巧包含%字符,则必须将它们写成%%。如果你想包含bugreport和主页地址,它们可以很自然地添加:
printf "usage: up [--level <n>| -n <levels>][--help][--version]\n\nReport bugs to: %s\nup home page: %s\n" "$bugreport" "$homepage"
其次,你可以使用format字符串让它在单独的行中打印每个额外的参数:
printf "%s\n" "usage: up [--level <n>| -n <levels>][--help][--version]" "" "Report bugs to: " "up home page: "
使用这个选项,添加bugreport和主页地址是相当明显的:
printf "%s\n" "usage: up [--level <n>| -n <levels>][--help][--version]" "" "Report bugs to: $bugreport" "up home page: $homepage"