ceprio
ceprio

Reputation: 393

Capturing multiple line output into a Bash variable with busybox sh

I'm trying to convert a Debian Bash script into a linux Busybox sh script. I'm stuck trying to convert the following command:

read -r -d '' MESSAGE << EOM
Return code: $retn_code
Start of backup: $DATESTART
End of backup: $DATEEND
$(df -h | grep '/share/USB')
EOM

The problem is with the -d option of read that is not available with Busybox. How can I set a variable ($MESSAGE in this case) to a string with multiple lines that includes values from other variables?

The output MESSAGE is going in a log file and in a message sent by sendmail:

echo "RESULTS: $MESSAGE" >> $LOGFILE
sendmail -S smtp.server.com -f "$FROM" "$RECIPIENTS" <<EOF
subject:$SUBJECT
from:$FROM

$MESSAGE
EOF

Upvotes: 0

Views: 781

Answers (2)

chepner
chepner

Reputation: 530843

You don't need a special command in any shell; just a regular assignment.

message="Return code: $retn_code
Start of backup: $DATESTART
End of backup: $DATEEND
$(df -h | grep '/share/USB')
"

Upvotes: 0

jhnc
jhnc

Reputation: 16642

Simplest answer is not to use read.

MESSAGE=$(cat <<EOM
Return code: $retn_code
Start of backup: $DATESTART
End of backup: $DATEEND
$(df -h | grep '/share/USB')
EOM
)
MESSAGE=$( printf "%s\n%s\n%s\n%s\n" \
    "Return code: $retn_code" \
    "Start of backup: $DATESTART" \
    "End of backup: $DATEEND" \
    "$(df -h | grep '/share/USB')" \
)

Upvotes: 1

Related Questions