쉘 스크립트에서 들여쓰기가 있는 변수에 여러 줄 문자열 값을 할당하는 방법은 무엇입니까?

쉘 스크립트에서 들여쓰기가 있는 변수에 여러 줄 문자열 값을 할당하는 방법은 무엇입니까?

정확한 들여쓰기와 줄을 사용하여 쉘 스크립트의 변수에 다음 여러 줄 문자열 값을 할당하고 싶습니다.

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"

관련 정보