![쉘 스크립트에서 들여쓰기가 있는 변수에 여러 줄 문자열 값을 할당하는 방법은 무엇입니까?](https://linux55.com/image/81824/%EC%89%98%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%EC%84%9C%20%EB%93%A4%EC%97%AC%EC%93%B0%EA%B8%B0%EA%B0%80%20%EC%9E%88%EB%8A%94%20%EB%B3%80%EC%88%98%EC%97%90%20%EC%97%AC%EB%9F%AC%20%EC%A4%84%20%EB%AC%B8%EC%9E%90%EC%97%B4%20%EA%B0%92%EC%9D%84%20%ED%95%A0%EB%8B%B9%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
정확한 들여쓰기와 줄을 사용하여 쉘 스크립트의 변수에 다음 여러 줄 문자열 값을 할당하고 싶습니다.
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"