0

I have written small Shell/bash script, where I have some data in sampleData.txt file which I want to convert in Base64 and to pass it in variable $scriptPayload:

value=$(cat sampleData.txt)
echo "$value"
encoded= echo  $value | base64 
scriptPayload='{"scriptText":"$encoded" }'
echo "$scriptPayload"

but the output which Ii am geting is literally {"scriptText":"$encoded"}. I t should take value from $encoded variable like {"scriptText":"Test the Shell Script and its behaviour" }

Please suggest. I am new to this.

1

Your script has a syntax error (not actualy, but it's definitely not doing what you think it does):

encoded= echo  $value | base64 

This could, assuming you're using a shell such as bash, be written as

encoded=$( base64 <<<"$value" )

or even

encoded=$( base64 <sampleData.txt )

which captures the base64-encoding of the data in the file sampleData.txt into the variable encoded using a command substitution, in a similar way to how you're already using cat in a command substitution on your first line of code.

Since variables are not expanded within single-quoted strings, the code

scriptPayload='{"scriptText":"$encoded" }'

will not do what you think it does (it sets scriptPayload to the literal string {"scriptText":"$encoded" }).

In bash, this is better written as

printf -v scriptPayload '{"scriptText":"%s"}' "$encoded"

or

printf -v scriptPayload '{"scriptText":"%s"}' "$( base64 <sampleData.txt )"

The printf utility built into bash prints directly into a variable with -v varname.

Note that some base64 utility implementations may produce data with CRLF line-endings though.


Using jo:

scriptPayload=$( jo scriptText=%sampleData.txt )
printf '%s\n' "$scriptPayload"

The argument scriptText=%sampleData.txt tells jo to create a JSON object with a key scriptText. The data for that key should be the base64-encoded contents of the file sampleData.txt (it's the % that decides that it should be base64-encoded).

With a file, sampleData.txt, containing the string Hello World (followed by a newline), this would output

{"scriptText":"SGVsbG8gV29ybGQK"}

jo is a tool for effortlessly creating properly encoded and quoted JSON data on the command line or in shell scripts.

1
  • <<< in bash (like in zsh but contrary to Byron Rakitzis's clone of rc, the two shells where the feature originate from) adds a newline character (and value=$(cat sampleData.txt) strips trailing newline characters if any). You'd need printf %s "$value" | base64 Sep 28 '20 at 7:25
0

Your line for encoding was not doing anything, I changed it. Plus I also changed the line for the final scriptPayload and used escaping.

#!/bin/bash
value=5
echo $value
encoded=`echo -n "$value" | base64`
scriptPayload="{\"scriptText\":\"${encoded}\" }"
echo $scriptPayload
0
encoded=`base64 -w0 < sampleData.txt`
scriptPayload='{"scriptText":"'"$encoded"'" }'
echo "$scriptPayload"

Try above piece of code. The reason your variable was not evaluating is because you encapsulated it inside single quotes. I happen to close single quote just before variable. That way shell would recognise a variable and replace it with actual contents.

0

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.