Automation using shell script

Automation using shell script

Need for Automation

While working with systems (like deploying a service, monitoring it, checking the logs, or managing the system as a whole), we noticed that a certain set of commands were used constantly. These commands were not shorthands but long. Typing them consumed a bit of time and was a repetitive task.

We, on noticing these repetitive tasks, took it as our first and foremost responsibility to come up with a solution to automate this.
The operating system we were using was Linux and so we decided to automate using shell script. As software developers, we know the necessity as well as the need for automation.

In this post, we'll take a simple example of building any project using maven commands. The script will be written in such a way that just passing the argument as mvn should trigger the maven build command with proper parameters included.

Create a shell script

We will create a shell script file in a folder /root/custom-scripts with the name zrun. As mentioned earlier the main task of the script would be to build a project by running the mvn command.

Following is the content of the script:

#!/bin/bash

function execute_mvn() {
    pom_path=$1
    if [[ -z "$pom_path" ]]; then
        pom_path="$PWD/pom.xml"
    fi
    echo "Executing maven for path: $pom_path"
    mvn clean install -f $pom_path
}

function main() {
    operation=$1
    case $operation in
        mvn)
            execute_mvn "$2"
            ;;
        *)
            echo "Available operations: mvn"
            ;;
    esac
}

main "$@"



In the script written above, the line #!/bin/bash is called the shebang line. When we run an executable file using Linux, the execve program is internally called to replace the current process with a new one. The execve expects the first two characters to be #! followed by a path to the interpreter that will be used to interpret the file. In our case, the path is /bin/bash.

You may have also noticed that we have used some variables like $1 and $2 which represent the arguments passed to the script, space-separated, via command-line.

Along with those variables we have used an environmental variable $PWD which stores the current directory path (absolute path). The use of this is, we wanted to have a script which we would only ask to invoke mvn, and internally it should create the full path to the pom file from where the script is invoked.

To further have flexibility to invoke multiple different commands using the same zrun script we used case statement in the script.

Execute script

To make the script executable, we would need to run the following command:

chmod +x zrun


chmod is used to change the file mode of any given file. In our case, we are telling chmod to make the file zrun an executable.

Once the script is made executable, we can now go ahead and run the script.
We would now run the script from the directory which has the pom file with the following command:

[root@localhost test-project]# pwd
/root/test/test-project

[root@localhost test-project]# /root/custom-scripts/zrun mvn
Executing maven for path: /root/test/test-project/pom.xml
[INFO] Scanning for projects... [rpm]


Extend script

The next thing after this is that it is good to always give the path of the script and then run it? Is there a way to just give the script name and the required parameters and run it?

The answer is obviously yes and the way to do this is by adding either the script's absolute path to the $PATH variable or moving the script to a directory that is already added as part of the $PATH environment variable.

Following is the command to print the value of the PATH environment variable:

[root@localhost test-project]# echo $PATH
/usr/lib/apache-maven/apache-maven-3.5.4/bin:/usr/java/latest/bin:/usr/local/sbin: /usr/local/bin:/usr/sbin:/root/bin


In our case, we will add the path of zrun script to the $PATH environment variable.
Following is the command:

[root@localhost test-project]# export PATH=$PATH:/root/custom-scripts

[root@localhost test-project]# echo $PATH
/usr/lib/apache-maven/apache-maven-3.5.4/bin:/usr/java/latest/bin:/usr/local/sbin: /usr/local/bin:/usr/sbin:/root/bin:/root/custom-scripts


The command mentioned above will add the path /root/custom-scripts to the $PATH environment variable while still keeping the old values as it is. While adding paths to the $PATH environment variable, we need to separate multiple paths using : as the separator.
Once we have added the path to the $PATH environment variable, now we can just run the script using the script name only.

[root@localhost test-project]# pwd
/root/test/test-project

[root@localhost test-project]# zrun mvn
Executing maven for path: /root/test/test-project/pom.xml
[INFO] Scanning for projects... [rpm]



Conclusion

A shell script can be used to automate the everyday tasks we do. We can replace the redundant commands we use regularly with a simple script and just on the execution of that script, the whole set of commands will be run.

If you have any doubts/questions, please feel free to drop a comment.

Post a Comment

0 Comments