0

I want to assign the following multiple line string value to a variable in a shell script with the the exact indentations and lines.

Usage: ServiceAccountName LogFile
Where:
      ServiceAccountName - credentials being requested.
      LogFile            - Name of the log file

I have been trying to do this following all the suggestions in: How to assign a string value to a variable over multiple lines while indented? But with no result. Please suggest.

REASON="$(cat <<-EOF
    Usage: ServiceAccountName LogFile
    Where:
      ServiceAccountName - credentials being requested.
      LogFile            - Name of the log file
EOF
)"
echo "$REASON"

Here is my script:

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

This scripts is called from the following script:

. /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
  • What output are you getting from the echo? What you have there works for me. – Andy Dalton Jan 21 '16 at 19:48
  • I am getting n error: "unexpected EOF while looking for matching `)'" and "syntax error: unexpected end of file" – devlperMoose Jan 21 '16 at 20:33
0

Your script works for me. All I did differently is add #!/bin/sh at top. Then made it executable and ran it. You could also use sh and the name of your original script.

#!/bin/sh
REASON="$(cat <<-EOF
Usage: ServiceAccountName LogFile
Where:
      ServiceAccountName - credentials being requested.
      LogFile            - Name of the log file
EOF
)"
echo "$REASON"
  • I copy pasted the above code from your post to my shell script and it started working. Not sure what made it working though. Thank you. – devlperMoose Jan 21 '16 at 20:52
  • 3
    @pavanbairu The reason is that the EOF end-marker is now not indented and is found. In your original, it is indented and not parsed as the end-marker. – Jens Jan 21 '16 at 20:55
1

Instead of a here-document with a useless cat, what about either

REASON="\
Usage: ServiceAccountName LogFile
Where:
      ServiceAccountName - credentials being requested.
      LogFile            - Name of the log file"

or

REASON="$(printf '%s\n' \
    "Usage: ServiceAccountName LogFile" \
    "Where:" \
    "      ServiceAccountName - credentials being requested." \
    "      LogFile            - Name of the log file")"
  • ya this is more simpler than having to use extra commands, and it works. Thank you. – devlperMoose Jan 21 '16 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.