Inject a wav file in to a bash script

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

Inject a wav file in to a bash script

#1 Post by Geoffrey »

I was wondering if was possible for a script to carry binary data, after a bit of searching I found the answer I was looking for, so made this example.

This script when run will extract the data to /tmp as chime.wav, then play the wav file as a sequence derived from the clock, to chime the hours.

Will work to chime the hours if run with Pschedule on the hour.

Code: Select all

#!/bin/bash

dd if="$0" of=/tmp/chime.wav bs=1 skip=131
hours=`date +"%-l"`
seq "$hours" | xargs -I{} aplay -q /tmp/chime.wav
exit;
In a working directory to build this place the chime.wav, then create a script with the above code and name it hour_chime

Code: Select all

cat chime.wav >> hour_chime
chmod +x hour_chime
Open a terminal in the working directory and paste the above code and run it, this injects the wav data in to the script.

The script is now a standalone not requiring any external file to function.

The important thing is the skip=? this needs to be precise else the extracted data will be corrupted.
Attachments
script.png
(75.14 KiB) Downloaded 277 times
hour_chime.tar.gz
(49.67 KiB) Downloaded 129 times
chime.wav.gz
FAKE gz, remove the .gz for the wav file
(55.62 KiB) Downloaded 143 times
Last edited by Geoffrey on Sun 31 Dec 2017, 11:06, edited 1 time in total.
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

Re: Inject a wav file in to a bash script

#2 Post by MochiMoppel »



User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

Re: Inject a wav file in to a bash script

#3 Post by Geoffrey »

MochiMoppel wrote:Your exit command bumps into the header of the wav file, which makes it "exitRIFFr", i.e. your script will not exit at this point. Make it exit; and change the skip value to 131. Still I can't see the usefulness of this exercise.
Ah! thanks, updated the first post, more a curiosity than a having a purpose for it, then you never know when it may find a purpose, just handy to know it can be done.
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

Re: Inject a wav file in to a bash script

#4 Post by MochiMoppel »

Geoffrey wrote:more a curiosity than a having a purpose for it
That's a valid reason :lol:
Let's try to make it a bit smarter. There is no need to extract the audio data into a tmp file. You can make the bash script "play itself". Create a script, let's call it header:

Code: Select all

#!/bin/bash
for ((c=1;c<=$(date +%l);c++));do dd if="$0"  bs=1 skip=90 | aplay; done
exit;
Make sure that the code has 90 characters, then combine header and chime.wav into a new script hourlychime.sh:

Code: Select all

cat header chime.wav > hourlychime.sh ; chmod +x  hourlychime.sh
Running hourlychime.sh will pipe the embedded audio data directly to aplay.

From here there is no limit to insanity: You can add a timer loop, so you won't need pschedule, you can pack more than one audio file into this script and even a player ... OK, I leave it to your curiosity.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#5 Post by musher0 »

Hi guys.

This technique is fascinating. Thanks to MochiMoppei for revisions and suggestions, and of
course to Geoffrey for initiating this thread.

Geoffrey, in the 1st line of your OP, you mention some research you did. Would you care to
share a few URLs about this technique for our benefit? TIA.

BTW, does this technique have a name?

BFN
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#6 Post by amigo »

Think about those *.run files -they are self-extracting scripts. The payload is a tarball or zipped file or whatever. Inside the script you have marker or other method to skip over the first script part, starting from the marker or offset and procesing that with whatever. You can use 'sed', 'head', 'cut', 'dd' or many other ways to skip to the payload and pipe that through whatever.

By appending such binary data, you can even have an 'exit' in your script to make sure the script will never get there.

User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

#7 Post by Geoffrey »

MochiMoppel wrote:Let's try to make it a bit smarter. There is no need to extract the audio data into a tmp file. You can make the bash script "play itself"
Brilliant, that was my initial want, I just got lazy :lol:
musher0 wrote:share a few URLs about this technique for our benefit?
BTW, does this technique have a name?
https://stackoverflow.com/questions/276 ... fficiently
amigo wrote:Think about those *.run files -they are self-extracting scripts.
That had crossed my mind during my search http://makeself.io/
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#8 Post by seaside »

Here's another way using tail.

Code: Select all

#!/bin/bash 
#chime.wav
#cat header chime.wav > hourlychime.sh ; chmod +x  hourlychime.sh
hours=`date +"%-l"` 
size=`stat -c %s chime.wav`
for (( i=1; i <= $hours; i++ )); do  tail -c $size hourlychime.sh | aplay;  done
exit;
Cheers,
s

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#9 Post by musher0 »

Thanks, Geoffrey.

I wish you and all Puppyists a "Happy, Healthy and Successful New Year 2018"! :)
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#10 Post by vovchik »

Dear all,

Thanks for the examples. I learned something, as I usually from many posts on this forum. I wish you all a happy, prosperous and rewarding new year!

WIth kind regards,
vovchik

User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

#11 Post by Geoffrey »

seaside wrote:Here's another way using tail.

Code: Select all

#!/bin/bash 
#chime.wav
#cat header chime.wav > hourlychime.sh ; chmod +x  hourlychime.sh
hours=`date +"%-l"` 
size=`stat -c %s chime.wav`
for (( i=1; i <= $hours; i++ )); do  tail -c $size hourlychime.sh | aplay;  done
exit;
Cheers,
s
That's good, here's a script to create the hourlychime.sh, just drag the wav file to it

Code: Select all

#!/bin/bash
SCRIPT="hourlychime.sh"
touch "$SCRIPT"
echo '#!/bin/bash
SCRIPT="$(readlink -e "$0")" 
for (( i=1; i <= `date +"%-l"`; i++ )); do  tail -c '$"`stat -c %s "$1"`"' "$SCRIPT" | aplay -q;  done
exit;' > "$SCRIPT"
cat "$1" >> "$SCRIPT" ; chmod +x  "$SCRIPT"
Happy new year

EDIT: Strangely enough this doesn't work from outside the directory it's in, I need to change directory in terminal for it to run, even if it's in /usr/bin

EDIT 2: fixed tail couldn't find the path

Code: Select all

SCRIPT="$(readlink -e "$0")"
Last edited by Geoffrey on Wed 03 Jan 2018, 03:33, edited 4 times in total.
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#12 Post by musher0 »

Powerful stuff. Careful what you wish!!! ;)
Someone could hide a < remove all command > in there!
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#13 Post by jamesbond »

amigo wrote:Think about those *.run files -they are self-extracting scripts. The payload is a tarball or zipped file or whatever.
Here: http://makeself.io/
It's actually quite popular. VirtualBox generic Linux binaries for example are packaged with makeself. Nvidia proprietary drivers too.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

Post Reply