Is Shell Scripting a good option for your business?

Expand your developers' capabilities in the Linux shell.

Imagem em destaque

Your developers work hard to create the apps and services your business needs to keep moving. After all, progress is the only way to keep up with the competition. And given how competitive the business world has become, every advantage you get will be one step closer to staying relevant.

This means your development team must be aware of the latest trends, use the best tools, and collaborate effectively and efficiently. This also means that you shouldn't ignore some of the tools that have been around for decades. When something is proven and still relevant, it should be used as often as necessary.

One of these old-school tools is shell scripting.

What is shell scripting?

Shell scripting is a small computer program, running in a UNIX or Linux shell, that can perform virtually any type of functionality its developers can imagine. Shell scripts usually consist of one or more commands that work together for a single purpose, such as:

  • Backups
  • Code compilation automation
  • Creating a programming environment
  • Batch processing
  • File handling
  • Program linking
  • System monitoring
  • System Tasks

You can even combine any of the above purposes to create a multitasking script that solves multiple problems. For example, you can create a script that creates a programming environment, performs automated code compilation, and backs up the results.

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

Advantages of Shell Scripts

Shell scripts have numerous advantages such as:

  • What is contained in a shell script is very similar to what would be entered into the command line interface.
  • Shell scripts are easier to write than traditional applications.
  • Shell scripts do not 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 execute multiple commands from a single command.
  • Debugging is interactive.

Disadvantages of Shell Scripts

Shell scripting is not perfect or suitable for all use cases. Shell scripts can be prone to errors, so anyone writing such a script should do so with caution. Shell scripts are also slower than a traditional application; therefore, if speed is of the essence, you will have to take another route.

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

An example shell script

Let's take a look at a simple example. Say, for example, 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:

 nanobackup.sh

In this file you can paste the following:

 #!/bin/sh

 rsync -av --delete /home/USER USER@SERVER:/home/USER/data

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

Effectively, what this script will do is execute a command that you can issue from the bash prompt. This command is:

 rsync -av --delete /home/USER USER@SERVER:/home/USER/data

After saving and closing the file, give it executable permissions with:

 chmod u+x backup.sh

Now if you issue the backup.sh command the resync command will run and back up the data directory.

But how do you automate this? For this, you would employ cron. Create a cron job with:

crontab -e

Paste 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've just created an automated backup (which will run every night at midnight) in about 2 minutes and without spending a single penny on software. Of course, this is a very simple shell script. Your developers can make shell scripts much more complex by adding functions, loops, and variables.

For example, you can create a jump script that will take you to whatever parent directory you want. Let's say, for example, you are in ~/projects/java/projectx/v1/functions and you want to switch back to ~/projects. Instead of typing cd ~/projects you could just type jump projects. This 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
 location pos=-1
 # ${path_arr(@)} gives all the values ​​in path_arr
 for dir in "${path_arr(@)}"
 of
 # 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++))
 of
 cwd=$cwd/..
 done
 cd $cwd
 break
 fi
 done
 }


In the bash script above, you would use various commands and statements to create something usable. With shell scripting, there is virtually 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 is almost no upfront cost (in finances or time).

With a little imagination and knowledge, your developers can create incredibly functional, unique and specific scripts to handle all types of functionality.

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.