Do you know why does technology rampant? To make our life easy by automating things. By clicking on a single cockpit button, the plane takes off.
But there are series of operations carried out after clicking the button. This is what the script is all about – running a single command to execute many operations.
In this post, I am not going to tell you how to run a plane or the technology behind it. I am listing all the commands to write and execute shell scripts from any directories to pilot or to automate series of operations.
The script is a file that contains more than one command. When we run this script file, all the commands in that script executes.
And write your Hello world shell script in a file.
#!/bin/bash #First shell script by Aniruddha echo "Hello, World!"
Save the file.
cd <path/to/file> ./firstScript
./ depicts running the script from the current directory.
Now, our executable shell script is ready.
Now, how to run the script from anywhere?
Note: Before updating the PATH environment variable, save it. You can check the PATH variable value by running the command
This path variable will last for the given session.
Note: The changes you make in the file
bashrc get reflected only when you start the shell next time.
If you get an error as “Permission denied”, you have to permit executable permission using command
chmod 777 ~/.bashrc
If you get the error as “firstScript: command not found“, you have not set the PATH correctly.
Also, read Bash Scripting vs Python. Which one should you learn?
Let’s see some of the basic usage of bash script with examples.
Write a bash script and save the code in a file ls.
#!/bin/bash ls -la
Above ls is Unix command to list all the directory files and “
la" is an option to print list long format including hidden files. You can find more options for the command “ls”.
Now, whenever you run the command
ls, it executes command
ls -la and gives you a detail description about directories.
If you work on the remote server, you have to log in it first with command
ssh. And you will be asked for the login credential. It is very tedious to type username and password every time you want to work on the remote server.
Instead of this, write a shell script for command
ssh and mention the login username and password. Save it in file
.sh. Now whenever you run this file, you will be login to a remote server without providing a username and password explicitly.
If you are a tester or testing the application, you can run the test case by writing it in a shell script. You don’t need to run each test case manually.
[disclaimer] I am not a testing guy, so I don’t much about testing. But, there are so many testers in our team uses automation script for testing.
There are so many things you can automate on your Linux system by shell scripting. These are some basic applications, you can start with.
This is all about how to run bash script from any directory. Any challenges while executing a shell script from any directories? Feel free to write in a comment. And also, let me know, what are the things you have automated with the shell script?