3

I'm working on an "edgemax feature-wizard" which is kind of a plugin system. There are only three files allowed in a plugin tarball: a bash script as backend, a HTML file as frontend and a validation.json file for input validation from frontend, so please don't ask why I don't just put the deb packages into the tarball -- they get ignored.

I want to ship deb packages with that "wizard" so I have to base64 encode the files and put it into the bash script to be extracted when the script is run. Now, I know how to do all this, but I'm doing it manually by copy-pasting the base64 part into the bash file and then cut-paste into right position, which is unhandy when updating those packages to recent version.

Would there be a possibility to encode the deb files in base64 and write it to line 65 of the bash script?

I've read this article and I tried

sed -i "65i\\$(base64 package.deb)" wizard-run

but that complains with:

bash: /bin/sed: Argument list too long

Here's my code; the base64-part has to go within the double-quotes from the echo statement:

if [ $arch == 'mips' ]; then
    # base64: olsrd_0.9.0.3-1_mips.deb
    echo "
    <base64encoded-pkg>
    " | base64 -d > $cfgusrdir/olsrd_0.9.0.3-1_mips.deb
    # base64: olsrd-plugins_0.9.0.3-1_mips.deb
    echo "
    <base64encoded-pkg>
    " | base64 -d > $cfgusrdir/olsrd-plugins_0.9.0.3-1_mips.deb
fi
  • 1
    use a here document – mikeserv Oct 24 '16 at 19:41
  • 1
    +1 to the heredoc - however, at least if you have GNU sed, you could probably do base64 package.deb | sed -i '65r /dev/stdin' wizard-run – steeldriver Oct 24 '16 at 20:27
  • great, that did it, thanks! is there a way to also remove newline at the end? – vchrizz Oct 24 '16 at 21:26
2

Using sed and I/O redirection:

{
  sed -n '1,64p' wizard-run;
  base64 package.deb;
  sed -n '66,$p' wizard-run;
} > wizard-run.tmp && mv wizard-run.tmp wizard-run
  • good idea, but i dont understand why, that fills the wizard-run with the base64 part running in loop until i press ctrl+c .. – vchrizz Oct 24 '16 at 21:01
  • 1
    @vchrizz Oops, writing to the file you are writing to is a bad idea (see github.com/koalaman/shellcheck/wiki/SC2094). My bad, will fix it. – phk Oct 24 '16 at 21:32
  • ok that worked now, how do i remove newline from beginning and end? – vchrizz Oct 24 '16 at 21:52
  • @vchrizz Beginning and end of what? wizard-run? – phk Oct 24 '16 at 22:21
  • 1
    @vchrizz Try the -i parameter of base64. – phk Oct 24 '16 at 22:30

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.