im currently writing tests for a cbor en-/de-coder and i'm using a cli tool called cbor-diag to create the check values. however its a lot to constantly write the whole command when throughout all the runs the only value that changes is the value to decode/encode. my idea was to write a script that automagically inserts the value to en-/de-code.
the problem seems to be that the cli tool uses a heredoc for the variable, which i cant insert into. the errormessage is : "/opt/cbor_dec: line 7: warning: here-document at line 6 delimited by end-of-file (wanted `END') /opt/cbor_dec: line 8: syntax error: unexpected end of file"
#!/bin/bash
if [ $# -eq 0 ]
then
echo "no args passed"
else
variable="$1 END"
cbor-diag --to bytes <<-END ${variable}|xxd
fi
Any help would be greatly appreciated. also im a complete noob with bash scripting...
UPDATE:
as a commenter suggested i could use heredoc syntax, hoewever this leads to the same errormessage. updated code:
if [ $# -eq 0 ]
then
echo "no args passed"
else
cbor-diag --to bytes <<-END | xxd
${1}
END
fi
UPDATE 2: as commenter suggested the spaces infront of the END delimiter were the problem! updated code:
#!/bin/bash
if [ $# -eq 0 ]
then
echo "no args passed"
exit 1
fi
cbor-diag --to bytes <<-END | xxd
${1}
END
CodePudding user response:
You can't embed a heredoc delimiter in a variable without relying on hacky tools like eval
(and maybe not even that). Heredocs are evaluated at a similar time as things like command substitutions, which happens before variables expand.
But, there's also no reason to embed it in the variable here. Normal heredoc syntax will do the job.
variable=$1
cbor-diag --to bytes <<-END | xxd
${variable}
END