Is Shell Scripting a Good Fit for Your Business?

Expand the capability of your developers from within the Linux shell.
December 24, 2021
Share on facebook
Share on twitter
Share on linkedin

Get the best of
The Daily Bundle in your inbox every week

Get the best of The Daily Bundle in your inbox every week

Your developers work hard to create the applications and services your business needs to stay in motion. After all, forward progress is the only way to keep up with the competition. And given just how competitive the business world has become, every edge you can get is one step closer to remaining relevant.

That means your development team must stay up on the latest trends, use the best tools, and collaborate effectively and efficiently. It also means you shouldn’t ignore some of the tools that have been around for decades. When something is both proven and still relevant, it should be employed as often as necessary.

One such old-school tool is shell scripting. 

What is Shell Scripting?

Shell scripting is a small computer program, run within a UNIX or Linux shell, that can execute just about any kind of functionality your developers can dream up. Shell scripts generally consist of one or more commands that function together for a single purpose, such as:

  • Backups
  • Code compilation automation
  • Programming environment creation
  • Batch processing
  • File manipulation
  • Program linking
  • System monitoring
  • System tasks

You could even combine any of the above purposes to create a multi-task script that tackles numerous problems. For example, you could come up with a script that creates a programming environment, does automated code compilation, and backs up the results. 

The beauty of shell scripting is that it is incredibly flexible and generally limited to the imagination of the person creating the script.

Advantages of Shell Scripts

Shell scripts have numerous advantages, such as:

  • What is contained within a shell script is very similar to what would be entered at the command line interface.
  • Shell scripts are easier to write than traditional applications.
  • Shell scripts don’t require compilers and can be easily modified on the fly.
  • Shell scripts are portable and almost always very small in size.
  • Shell scripts make it easy to run multiple commands from a single command.
  • Debugging is interactive.

Disadvantages of Shell Scripts

Shell scripting isn’t perfect, nor is it suited for every use case. Shell scripting can be prone to errors, so anyone writing such a script must do so with caution. Shell scripts are also slower than a traditional application, so if speed is of the essence, you’ll want to go another route. 

Finally, shell scripts aren’t exactly suited for larger and very complex tasks. The best use case for shell scripts is a small task (or collection of small tasks) that would normally be handled from the command line, and you’d prefer to link those commands together or automate them.

A Shell Script Example

Let’s take a look at a simple example. Say, for instance, you want to back up a directory from one machine, over the network, to another machine. You would create this shell script with a command like:

nano backup.sh

In that file you could paste the following:

#!/bin/sh

rsync -av --delete /home/USER [email protected]:/home/USER/data

Where USER is a user on both the local and remote machine and SERVER is either the IP or domain name of a remote server.

Effectively what this script will do is run a command you could issue from the bash prompt. That command is:

rsync -av --delete /home/USER [email protected]:/home/USER/data

After you save and close the file, give it executable permissions with:

chmod u+x backup.sh

Now, if you issue the command backup.sh, the rsync command will run and backup the data directory.

But how do you automate that? For that, you’d employ cron. Create a cron job with:

crontab -e

Past the following at the bottom of your crontab file:

0 0 * * * /home/USER/backup.sh > /dev/null 2>&1

Save and close the crontab file and the backup script will run every midnight.

With shell scripting, you just created an automated backup (that will run every night at midnight) in about 2 minutes and without spending a single penny on software. Of course, that’s a very simple shell script. Your developers can make shell scripts far more complex, adding functions, loops, and variables. 

For example, you can create a jump script that will take you to whatever parent directory you want. Say, for example, you’re in the ~/projects/java/projectx/v1/functions and you want to jump back to ~/projects. Instead of typing cd ~/projects, you could simply type jump projects. Such a script might look like this:

# !/bin/bash
 # A simple bash script to move up to desired directory level directly
 function jump()
{
# original value of Internal Field Separator
OLDIFS=$IFS
  # setting field separator to "/"
IFS=/
  # converting working path into array of directories in path
# eg. /my/path/is/like/this
# into [, my, path, is, like, this]
path_arr=($PWD)
# setting IFS to original value
IFS=$OLDIFS
  local pos=-1
  # ${path_arr[@]} gives all the values in path_arr
for dir in "${path_arr[@]}"
do
     # find the number of directories to move up to
     # reach at target directory
     pos=$[$pos+1]
     if [ "$1" = "$dir" ];then
         # length of the path_arr
         dir_in_path=${#path_arr[@]}
          #current working directory
         cwd=$PWD
         limit=$[$dir_in_path-$pos-1]
         for ((i=0; i<limit; i++))
         do
             cwd=$cwd/..
         done
         cd $cwd
         break
     fi
done
}

In the above bash script, you’d use various commands and statements to create something usable. With shell scripts, there’s practically no limit to what your developers can do. And if they already know how to use the Linux command line, they already know how to write shell scripts.

Conclusion

If you’re looking for a way to help make your developer teams even more agile, empower them with the ability to write shell scripts. The return on investment for these little gems can be incredibly high because there’s next to no upfront cost (in finances or time). 

With a bit of imagination and know-how, your developers can create incredibly functional, unique, and specific scripts to handle all sorts of functionality.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

How useful was this post?

Click on a star to rate it!

Please enter a valid E-mail address.

Get in Touch

Access the Top 1% IT Talent, leverage our expertise to
jump-start your business.​

  • This field is for validation purposes and should be left unchanged.
If you previously need to sign a non-disclosure agreement, please email us at [email protected].
Scroll to Top

Get in Touch

Jump-start your Business with the
Top 1% of IT Talent.

Need us to sign a non-disclosure agreement first? Please email us at [email protected].

ACCELERATE YOUR DIGITAL TRANSFORMATION

By continuing to use this site, you agree to our cookie policy.