Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing files inside BASH scripts

Tags:

bash

Is there a way to store binary data inside a BASH script so that it can be piped to a program later in that script?

At the moment (on Mac OS X) I'm doing

play sound.m4a
# do stuff

I'd like to be able to do something like:

SOUND <<< the m4a data, encoded somehow?
END
echo $SOUND | play
#do stuff

Is there a way to do this?

like image 662
JP. Avatar asked Feb 22 '10 01:02

JP.


2 Answers

If it's a single block of data to use, the trick I've used is to put a "start of data" marker at the end of the file, then use sed in the script to filter out the leading stuff. For example, create the following as "play-sound.bash":

#!/bin/bash

sed '1,/^START OF DATA/d' $0 | play
exit 0
START OF DATA

Then, you can just append your data to the end of this file:

cat sound.m4a >> play-sound.bash

and now, executing the script should play the sound directly.

like image 195
Uditha Desilva Avatar answered Oct 23 '22 11:10

Uditha Desilva


Base64 encode it. For example:

$ openssl base64 < sound.m4a

and then in the script:

S=<<SOUND
YOURBASE64GOESHERE
SOUND

echo $S | openssl base64 -d | play
like image 32
Sionide21 Avatar answered Oct 23 '22 12:10

Sionide21