Results 1 to 5 of 5

Thread: Embedding files in a bash script?

  1. #1
    Join Date
    Jan 2012
    Beans
    753

    Embedding files in a bash script?

    How do I embed a data file (e.g. audio, video, compressed, etc) in a bash script, so that the bash script can extract/create a file from itself? Would something like this work?

    Code:
    #!/bin/bash
    
    cat << _AUDIO > /tmp/music.mp3
    contents
    of
    audio
    file
    _AUDIO
    
    mpg123 /tmp/music.mp3
    rm /tmp/music.mp3
    exit 0
    Or how else can this be done? I know the TrueCrypt installer does this, so I know it can be done.

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,319
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Embedding files in a bash script?

    I am pretty sure that I have seen installer scripts which contain binary data, but I would be concerned about:
    a) risk of corruption by my text editor
    b) the unwanted but necessary newline at the end of file

    There may be a workaround, e.g. if you embed a tar archive, but I'm not sure. Personally I'd opt for encoding the data. This makes your install script slightly larger, which may or may not be significant.

    Code:
    base64 < music.mp3 > temp.tmp
    Load temp.tmp into your script, then:

    Quote Originally Posted by Stonecold1995 View Post
    Code:
    #!/bin/bash
    
    base64 -d << _AUDIO > /tmp/music.mp3
    contents
    of
    temp.tmp
    _AUDIO
    
    mpg123 /tmp/music.mp3
    rm /tmp/music.mp3
    exit 0
    You'd need to check whether base64 is installed by default, or at least whether you could rely on it being on all intended targets. There are alternative encoding/decoding mechanisms.

    It might be worth looking at some scripts that do this.

  3. #3
    Join Date
    Jan 2012
    Beans
    753

    Re: Embedding files in a bash script?

    Thank you, it worked.

    Are there other encoding methods that encode/decode quicker or provide smaller file sizes?

  4. #4
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,319
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Embedding files in a bash script?

    No, I don't know of anything better. uuencode is older and no better than base64.

  5. #5
    Join Date
    Jul 2012
    Beans
    318
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Embedding files in a bash script?

    This is very clever, but I am struggling to find a useful application for this...?
    The best things in life are free, so what are we paying for?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •