Linux include file in script

How to embed a binary file in a bash shell script

Have you ever been in a situation where you want to include a binary file inside your shell script before sharing it with others? Why would you want to do that? For example, you are a sysadmin in an IT department of your company, supporting Linux users. You want the users to be able to install the company’s VPN software and configure everything necessary on their work computer simply by running a shell script you provide. In this case, you create a tarball that contains everything (e.g., VPN software to install, an XML profile and config files to be placed in /etc ), and ship the tar file as embedded in an installation script. In another scenario, you are writing a shell script that relies on an external executable for certain tasks. For portability of the script, you want to embed the executable itself within the script. Whether or not embedding a binary blob in a shell script is recommended, the ability to do so may come in handy in certain situations.

In this tutorial, let’s find out how you can include a binary file in a bash script and how to retrieve it from the script.

Embed a Tar File in a Shell Script

The way you can embed a binary file such as a tarball in a shell script is by appending the content of the file at the end of the script. Read on to find out how to embed a tar file in a shell script.

First, prepare a shell script ( script.sh ) as follows. What the script does is to extract an embedded tar file and perform some actions (to be defined in process_tar() ) with the extracted content. So fill out process_tar() based on your requirement.

#!/bin/bash # define the tasks that need to be done with the extracted content process_tar() < cd $WORK_DIR # do something with the extracted content ># line number where payload starts PAYLOAD_LINE=$(awk '/^__PAYLOAD_BEGINS__/ < print NR + 1; exit 0; >' $0) # directory where a tarball is to be extracted WORK_DIR=/tmp # extract the embedded tar file tail -n +$ $0 | tar -zpvx -C $WORK_DIR # perform actions with the extracted content process_tar exit 0 __PAYLOAD_BEGINS__

Be careful that __PAYLOAD_BEGINS__ should be the last line of the script. Even empty lines or newline characters are not allowed beyond this line.

Читайте также:  Linux usb to usb connection

Once the shell script is finalized and ready, the next step is to append an actual tar file ( dummy.tgz ) to the script by running:

At this point, the script is ready to be distributed. When invoked from the command line, the script will extract the embedded tarball in WORK_DIR directory, and process the extracted content as defined in process_tar() .

Beware that since the tar file is appended to the shell script as binary data, it is not safe to re-edit the shell script (even the text portion of it) after embedding. If you modify and save the shell script after a tarball is appended, you may see the following error when running it.

gzip: stdin: unexpected end of file tar: Child returned status 1 tar: Error is not recoverable: exiting now

Embed a Tar File in Encoded Format in a Shell Script

Instead of embedding a binary file as is inside a shell script, it is often safer to embed the file in an encoded form. A popular binary-to-text encoding scheme is Base64, which is implemented by the base64 command-line utility.

In order to use Base64-based encoding/decoding, you can replace the line in the above script:

tail -n +$ $0 | tar -zpvx -C $WORK_DIR
tail -n +$ $0 | base64 -d | tar -zpvx -C $WORK_DIR

This will decode the embedded content before untarring it.

Then you need to append a tar file in an encoded format by running:

At this point, the shell script is ready to be distributed. You will notice that the tar file is encoded in a textual format, and you may re-edit the script.

__PAYLOAD_BEGINS__ H4sIAJ0WOGAAA+y8B1RUydY/2kgSBBGkJQsiGaHpppscJEvOGcktNJkmZwEBJSoKKCoIKEgWSZJF chSQKCBZsuQM3a8RZ0bHe+fOvPXd7//+b1msTZ3Tlfbev1371Kmzals4OtxAWIEA/83Eg0n8MNhx DuaH8Xyf/5YAYCgPmI8HzAvm4QfwgCF8YB4AA+y/ytW35ObiaoZkYABYe7k5uMAdbf9dvf9U/n9p sjjB39LN3t6L+/jmvzDGMcB8UOjfwR/CB4Vg8Ify8fACGHj+C7z8lH7h/wf+LnCkO8IC/j89xj/A /9v8h/GCf+H/v5KczCxszazg/9UHwD/w/xB+2PH8h/BDeX/5//+N9Bv+Xx2AiTuYG8bHbQk3/x8d 4y/mPxjMA+HlB0N/n/8QCPR4/kPBsF/z/38jMatJy3KBufmJCZmLMYmYEMzAw+BobkNMKCIC0vJy

Embed a Binary Executable in Encoded Format in a Shell Script

If you want to include a binary executable in a shell script, you can use the same technique described here to save the embedded binary as an executable, and invoke it as needed. See the example script ( script2.sh ) below.

#!/bin/bash # line number where payload starts PAYLOAD_LINE=$(awk '/^__PAYLOAD_BEGINS__/ < print NR + 1; exit 0; >' $0) # directory where a binary executable is to be saved WORK_DIR=/tmp # name of an embedded binary executable EXE_NAME=dummy_executable # extract the embedded binary executable tail -n +$ $0 | base64 -d | cat > $/$ chmod +x $/$ # run the executable as needed $/$ exit 0 __PAYLOAD_BEGINS__

Then same as before, append the binary executable in an encoded form:

$ base64 dummy_executable >> script2.sh

Summary

This tutorial demonstrates how to embed a binary file in a bash shell script. While this tutorial can be used to cover a variety of use cases involving binary data, if all you need is a self-extracting archive, there is a dedicated tool for that.

If you find this tutorial helpful, I recommend you check out the series of bash shell scripting tutorials provided by Xmodulo.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Читайте также:  Linux построчный вывод информации

Источник

How to include file in a bash shell script?

When writing a bash shell script in Linux, it is often necessary to include external files within the script. This could be for a number of reasons such as reusing code, modularizing code, or including configuration files. There are several methods for including files in a bash script, each with its own advantages and disadvantages. In this answer, we will explore the various methods to include files in a bash script in Linux.

Method 1: Source Command

To include a file in a bash shell script on Linux, you can use the «source» command. This command reads and executes commands from the specified file in the current shell environment. Here’s how to do it:

That’s it! The «source» command will execute the commands in «myfile.sh» in the current shell environment, so any variables or functions defined in that file will be available to your main script.

You can also use relative or absolute paths with the «source» command to include files from other directories:

#!/bin/bash source /path/to/myfile.sh

Or you can include multiple files in one command:

#!/bin/bash source myfile1.sh myfile2.sh myfile3.sh

And if you want to check if a file exists before including it, you can use the «test» command:

#!/bin/bash if test -f myfile.sh; then source myfile.sh fi

That’s how you can include a file in a bash shell script on Linux using the «source» command.

Method 2: Dot Command

To include a file in a bash shell script using the «Dot Command», follow these steps:

  1. Create a file named «included_file.sh» with the code you want to include in your main script. For example, let’s say you want to define a function in this file:
#included_file.sh my_function()
  1. In your main script, use the «Dot Command» followed by the path to the file you want to include. For example:
#!/bin/bash . ./included_file.sh my_function

Note that the path to the included file is preceded by a dot and a space. This tells the shell to execute the contents of the file in the current shell environment.

  1. You can also include multiple files using the «Dot Command». For example, let’s say you have another file named «constants.sh» that defines some constants:
#constants.sh readonly PI=3.14159 readonly G=9.81

In your main script, you can include both files like this:

#!/bin/bash . ./included_file.sh . ./constants.sh my_function echo "The value of PI is: $PI"

Note that the «Dot Command» can be used to include any valid shell script file, including files with functions, variables, and other shell commands.

That’s it! By using the «Dot Command», you can easily include code from other files in your bash shell scripts.

Читайте также:  Проверить статус службы линукс

Method 3: Read File with a While Loop

To include a file in a bash shell script in Linux using «Read File with a While Loop», you can follow these steps:

  1. Create a file that contains the code or variables you want to include in your script. For example, let’s call this file «included_file.sh» and add the following code:
#!/bin/bash my_var="Hello World!" echo $my_var
  1. In your main script, use the while loop to read the lines of the included file and execute them. Here is an example:
#!/bin/bash while IFS= read -r line; do eval "$line" done  included_file.sh echo $my_var

In this example, the while loop reads each line of the «included_file.sh» file and executes it using the eval command. This allows you to include code or variables from the included file into your main script.

$ bash main_script.sh Hello World! Hello World!

In this example, the included variable «my_var» is printed twice, once from the included file and once from the main script.

That’s it! You have successfully included a file in a bash shell script using «Read File with a While Loop».

Method 4: Read File with a For Loop

To include a file in a bash shell script, one way is to use the «Read File with a For Loop» method. Here are the steps:

  1. Create a file with the code you want to include. For example, let’s call it «included_code.sh».
  2. In your main script, use the «for» loop to read the lines of the included file and execute them. Here’s an example:
#!/bin/bash for line in $(cat included_code.sh) do eval "$line" done
  1. Save the file and run it. The included code should now be executed as if it were part of the main script.

Here’s another example that uses a function:

#!/bin/bash source included_code.sh my_function "Hello, world!"

In this case, we use the «source» command to include the file, and then we call a function defined in the included code.

Note that using «eval» to execute code from a file can be dangerous, as it can execute arbitrary commands. Make sure to only include code that you trust.

That’s it! With these steps, you can include code from another file in your bash shell script using the «Read File with a For Loop» method.

Method 5: Parameter Expansion

To include a file in a bash shell script using «Parameter Expansion», you can use the source command with the $(

  1. Create a file with the code you want to include, for example, my_functions.sh .
  2. In your main script, use the source command with the $(

This will execute the contents of my_functions.sh in the current shell.

Here are some additional examples:

source $(/home/user/scripts/my_functions.sh)
path_to_file="./my_functions.sh" source $($path_to_file)

That’s it! Now you know how to include a file in a bash shell script using «Parameter Expansion».

Источник

Оцените статью
Adblock
detector