As system administrators, we frequently use shell scripts to automate our operational tasks on Linux systems. It would be helpful to convert these scripts into binary form to improve their performance and security as well. Also, it will be helpful to bundle our scripts and share them with other devices where the interpreter isn’t available.
In this tutorial, we’ll focus on the steps involved in converting a shell script into a binary executable file using the shc compiler. We’ll also illustrate the installation of the shc compiler, binary file creation, and its successful execution.
Without any further ado, let’s get into the nitty-gritty of it.
2. Building a Shell Script
Let’s quickly build a shell script to ask for the user’s name and display the current date and time. The shebang line #!/bin/bash at the beginning of the script instructs the operating system to execute this script using the bash interpreter:
$ cat welcome.sh #!/bin/bash echo -n "Enter your name: " read max echo -e "\nHello $max, Welcome to Baeldung Blogs!" now=$(date +"%Y-%m-%d %H:%M:%S") echo -e "\nCurrent date and time: $now" echo -e "\nHappy Reading!"
Let’s grant executable permission to the script using the chmod command and execute the script:
$ chmod +x welcome.sh $ ./welcome.sh Enter your name: Sriram Ramanujam Hello Sriram Ramanujam, Welcome to Baeldung Blogs! Current date and time: 2023-03-19 17:04:33 Happy Reading! $
That’s pretty much what we need to build our first binary files for shell scripts using the shc compiler.
3. SHC Compiler Installation
Generally, we use the shc compiler module to convert a shell script into a binary executable file. Furthermore, shc performs two standard steps as part of this conversion process. First, it converts the shell scripts into equivalent C code (with *.c extensions), and second, it creates the binary executables (with *.x extensions) after compiling the C source code.
Before we get started with shc, there are a couple of things we need to take care of. First, we’ll have to add the relevant repository to the system package sources automatically by creating a new file in the /etc/apt/source.list.d/ directory. Also, we’ll need to update the package lists using the apt-get update command.
Lastly, we install the shc package using the apt-get install command:
$ sudo add-apt-repository ppa:neurobin/ppa $ sudo apt-get update $ sudo apt-get install shc -y
Let’s execute the shc command to get the basic usage information and also check the installation:
$ shc shc parse(-f): No source file specified shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-o outfile] [-rvDSUHCABhs] -f script
Further, let’s use the which command to identify the installation path of the shc. We’ll need this in the next step:
$ which shc /usr/bin/shc
4. Create and Run the Binary File
Now, we can easily create the binary executable using the shc command by specifying the shell script and its path with the -f argument. Also, we’ll add -v for verbose output and -r to enable the redistributable feature so that we can use the executable on other machines:
$ /usr/bin/shc -vrf welcome.sh -o welcome shc shll=bash shc [-i]=-c shc [-x]=exec '%s' "[email protected]" ... ... output truncated ... ...
By default, the generated binary comes with the .x extension. However, we’re using the -o option and forcing the binary output name to welcome:
$ ls -l | grep welcome -rwxrwxr-x 1 sriram sriram 11240 Mar 19 17:10 welcome -rwxrwxr-x 1 sriram sriram 384 Mar 19 17:02 welcome.sh -rw-rw-r-- 1 sriram sriram 17156 Mar 19 17:10 welcome.sh.x.c
We get only the encrypted special characters when we try to open the newly created binary file:
$ cat welcome ELF>` @(%@8 @@@@��888 ... ... output truncated ... ...
Lastly, let’s execute the binary file and test the script’s functionality.
$ ./welcome Enter your name: Sriram Hello Sriram, Welcome to Baeldung Blogs! Current date and time: 2023-03-19 17:10:51 Happy Reading! $
In this tutorial, we’ve seen the detailed list of steps for converting a shell script into a binary executable file using the shc compiler.
We also covered compiler installation, binary file creation, and successful execution of the generated binary file.