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() { function main() { 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.
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@localhost test-project]# /root/custom-scripts/zrun mvn |
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 |
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 |
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@localhost test-project]# zrun mvn |
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.
0 Comments