Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 25 Sep 2018, 03:28
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Inject a wav file in to a bash script
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [13 Posts]  
Author Message
Geoffrey


Joined: 30 May 2010
Posts: 2361
Location: Queensland

PostPosted: Sat 30 Dec 2017, 20:43    Post subject:  Inject a wav file in to a bash script  

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:
#!/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:
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.
script.png
 Description   
 Filesize   75.14 KB
 Viewed   252 Time(s)

script.png

hour_chime.tar.gz
Description 
gz

 Download 
Filename  hour_chime.tar.gz 
Filesize  49.67 KB 
Downloaded  64 Time(s) 
chime.wav.gz
Description  FAKE gz, remove the .gz for the wav file
gz

 Download 
Filename  chime.wav.gz 
Filesize  55.62 KB 
Downloaded  71 Time(s) 

_________________
Carolina: Recent Repository Additions


Last edited by Geoffrey on Sun 31 Dec 2017, 07:06; edited 1 time in total
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1619
Location: Japan

PostPosted: Sun 31 Dec 2017, 05:27    Post subject: Re: Inject a wav file in to a bash script  

Geoffrey wrote:
I was wondering if was possible for a script to carry binary data
Nothing is impossible but it has the potential for desaster. Bash tries to interpret binary rubbish:
Code:
# /opt/hour_chime
56954+0 records in
56954+0 records out
56954 bytes (57 kB) copied, 0.447853 s, 127 kB/s
/opt/hour_chime: line 6: $'exitRIFFr\336WAVEfmt': command not found
/opt/hour_chime: line 7: $'S\031\365\v\230\363=\b~\E\225\350y\317H\376\274\035\342\231\031&\374 \331\316\242\343\300\004\327\025\260': command not found
/opt/hour_chime: line 10: syntax error near unexpected token `)'
/opt/hour_chime: line 10: `)þÙâä×æ¶ïC % ð\ç«{*×?õrþºã]ڻõ)^0
                                                           wÛ9ãÕë¢õd"³)IüëßÄþá _në1íºõú¨õN¨õô¯µïYÌîóÐ#̹õæôXþ©ëñïö2ù«X+»r÷؍µTæÓ9<:4
              Ííçù÷Ü*û³:Â<÷.̟ÄéϾ'
# 1;2c1;2c^C

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.
Back to top
View user's profile Send private message 
Geoffrey


Joined: 30 May 2010
Posts: 2361
Location: Queensland

PostPosted: Sun 31 Dec 2017, 07:13    Post subject: Re: Inject a wav file in to a bash script  

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.

_________________
Carolina: Recent Repository Additions

Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1619
Location: Japan

PostPosted: Sun 31 Dec 2017, 08:53    Post subject: Re: Inject a wav file in to a bash script  

Geoffrey wrote:
more a curiosity than a having a purpose for it
That's a valid reason Laughing
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:
#!/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:
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.
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12697
Location: Gatineau (Qc), Canada

PostPosted: Sun 31 Dec 2017, 12:34    Post subject:  

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
~~~~~~~~~~
Siempre será canción nueva... (V. Jara, Manifiesto)
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2641

PostPosted: Sun 31 Dec 2017, 15:18    Post subject:  

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.
Back to top
View user's profile Send private message 
Geoffrey


Joined: 30 May 2010
Posts: 2361
Location: Queensland

PostPosted: Sun 31 Dec 2017, 18:02    Post subject:  

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 Laughing

musher0 wrote:
share a few URLs about this technique for our benefit?
BTW, does this technique have a name?

https://stackoverflow.com/questions/27601972/embedding-binary-data-in-a-script-efficiently

amigo wrote:
Think about those *.run files -they are self-extracting scripts.

That had crossed my mind during my search http://makeself.io/

_________________
Carolina: Recent Repository Additions

Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 919

PostPosted: Sun 31 Dec 2017, 18:18    Post subject:  

Here's another way using tail.

Code:
#!/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
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12697
Location: Gatineau (Qc), Canada

PostPosted: Sun 31 Dec 2017, 18:18    Post subject:  

Thanks, Geoffrey.

I wish you and all Puppyists a "Happy, Healthy and Successful New Year 2018"! Smile

_________________
musher0
~~~~~~~~~~
Siempre será canción nueva... (V. Jara, Manifiesto)
Back to top
View user's profile Send private message 
vovchik


Joined: 23 Oct 2006
Posts: 1465
Location: Ukraine

PostPosted: Sun 31 Dec 2017, 20:55    Post subject:  

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
Back to top
View user's profile Send private message 
Geoffrey


Joined: 30 May 2010
Posts: 2361
Location: Queensland

PostPosted: Mon 01 Jan 2018, 02:19    Post subject:  

seaside wrote:
Here's another way using tail.

Code:
#!/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:
#!/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:
SCRIPT="$(readlink -e "$0")"

_________________
Carolina: Recent Repository Additions


Last edited by Geoffrey on Tue 02 Jan 2018, 23:33; edited 4 times in total
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12697
Location: Gatineau (Qc), Canada

PostPosted: Mon 01 Jan 2018, 03:15    Post subject:  

Powerful stuff. Careful what you wish!!! Wink
Someone could hide a < remove all command > in there!

_________________
musher0
~~~~~~~~~~
Siempre será canción nueva... (V. Jara, Manifiesto)
Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 3158
Location: The Blue Marble

PostPosted: Mon 01 Jan 2018, 06:10    Post subject:  

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, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [13 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1490s ][ Queries: 13 (0.0248s) ][ GZIP on ]