我想将以下多行字符串值分配给 shell 脚本中的变量,并具有精确的缩进和行。
Usage: ServiceAccountName LogFile
Where:
ServiceAccountName - credentials being requested.
LogFile - Name of the log file
我一直在尝试遵循以下所有建议来做到这一点:如何在缩进的情况下将字符串值分配给多行变量? 但没有结果。请建议。
REASON="$(cat <<-EOF
Usage: ServiceAccountName LogFile
Where:
ServiceAccountName - credentials being requested.
LogFile - Name of the log file
EOF
)"
echo "$REASON"
这是我的脚本:
GetBatchCredentials.sh
if [ $# -ne 2 ]
then
# RETURN INVALID USAGE
GetBatchCredentials_Result="Error"
GetBatchCredentials_Reason="$(cat <<-EOF
Usage: ServiceAccountName LogFile
Where:
ServiceAccountName - credentials being requested.
LogFile - Name of the log file
EOF
)"
else
//coding...
fi
该脚本是从以下脚本调用的:
. /www/..../scripts/GetBatchCredentials.sh arg1 arg2
if [ "$GetBatchCredentials_Result" != "Success" ]
then
echo "Error obtaining FTP Credentials"
echo "$GetBatchCredentials_Reason"
ret=1
else
echo "Obtained Credentials"
fi
答案1
与其在此处文档中添加一只无用的猫,不如
REASON="\
Usage: ServiceAccountName LogFile
Where:
ServiceAccountName - credentials being requested.
LogFile - Name of the log file"
或者
REASON="$(printf '%s\n' \
"Usage: ServiceAccountName LogFile" \
"Where:" \
" ServiceAccountName - credentials being requested." \
" LogFile - Name of the log file")"
答案2
你的剧本对我有用。我所做的只是在顶部添加 #!/bin/sh 。然后使其可执行并运行它。您还可以使用 sh 和原始脚本的名称。
#!/bin/sh
REASON="$(cat <<-EOF
Usage: ServiceAccountName LogFile
Where:
ServiceAccountName - credentials being requested.
LogFile - Name of the log file
EOF
)"
echo "$REASON"