How to write Script in Linux – Step by Step Tutorial
Shell scripting is a programming skill, which you can use to automate the Linux environment. If you know shell scripting then you can easily manage Linux servers by automating its services and its work that will reducing the burden of your work.
There are more advantages of using the shell scripting over using other scripting technologies, which was available in the market. Shell scripting environment comes with every Linux distributions.
Here we can see how to create script in Linux to manage the Linux services and it functions.
To write a script, you need to now and have the following things.
- Linux Installed Operating System
- Access to the Linux Terminal
- Root Permission
- Editor Tools
Before learning how to write script in Linux, you need to know what are the script editors available in Linux that will be used to write the shell scripts. At the end of the post, you can decide which text editor you can use to write your own script.
Script Editors for Linux:
Gedit is a Linux tool which was used to edit you text files and scripts. It can also be used for coding purpose. It is one of the best graphical text editor program for Linux.
The advantages of using gedit is as follows
- Easy to Use
- Graphical Program
- Opens any kind of Text based file
- Auto color coding for scripts
Using gedit will great while you are using the graphical user interface in Linux Operating system. But If you want text based editors which you need to create and edit scripts remotely then at that place gedit cannot be used. You need to go for an alternative.
You don’t need to worry about it, because Linux has more number of text editors that supports both the graphical and text based editors.
nano is a simple command line based text editor. By default it comes pre-installed while installing any of your Linux distribution.
It has great advantages because of the following features.
- Easy to use
- Supports command line
- Supports pine email program
- Has more shortcuts
So if you are a newbie, then you can opt for nano because of its simple interface and its robust features.
vi or vim
vi or vim is the famous command line text editor which was available for all the Linux distributions. It was used by more number of people around the world. Still it was considered one of the best command line text editor for the Linux distributions.
There are more advantages in using vi or vim in Linux over using the nano, emacs and gedit.
- It was powerful
- Faster that any other text editor
- Available in Unix
There are more difference between vi and vim. vim is the latest version of the vi text editor. vim supports almost all the coding and it can color code the programs with unique algorithm. But vi does not support this feature.
One of the advantage of using the nano and vi or vim is you can use these text editor while you remotely connect your Linux servers using putty or another SSH client. These command line text editors are supported in the SSH and TELNET, so programmers will use these tool to configure their Linux server using this editor.
Even though there are more number of GUI and command line text editors available in the market for Linux distributions, I have mentioned the top 3 editors, from this you can select one of the editor and learn the shortcuts that will make you to create scripts fast.
Now lets go and learn how to create scripts in Linux.
TO create scripts in Linux, first you need to have a Linux operating system with valid root credentials for running and executing the scripts. Here I am using the CentOS.
Here is the tutorial of How to install CentOS. Follow this tutorial and prepare the Linux Operating system to create the script.
After preparing the CentOS, now you will have the root credentials in our hand. Now boot your operating system and open “Terminal” which you can see in the Applications.
How to create the Script using vim editor:
After opening the terminal, you just need to use the vim editor to create a document using the below code.
# vim test_script.sh
Here we need to create the document ending with the extension “sh”, it’s because all the shell scripts will run with the extensions as “sh”. Since you are using the vim editor, while you do the coding part in your script, it will automatically color the code so that you can clearly understand at which part you are standing on.
Now copy and paste the below code in the document, which you have opened using the vim editor with the name “test_script.sh”. While copying the scripts from the web browser, make sure that you have content was correct after copying and make sure the quotes and the space are in correct place.
echo '''This script is working is correct"
After pasting the script in the vim editor, you need to save the script using the following command keystrokes inside the vim editor.
To save the script inside the vim editor press -> “ESC + : + wq” and then hit Enter. Now you have successfully created the script in your Linux distribution using the vim editor. It’s time to run the script.
How to give execute permission to the script:
By default, Linux will not allow you to run the executable script. Whenever you are creating a document or a script, the execute permission will not be given to the script. So you need to manually give execute permission to the account which you are operating to run the script.
Even for root user the execute permission will not be given by default in all the Linux distribution.
chmod +x <Script Name>
chmod 755 <Script Name>
Run the above command with the script name to give the execute permission to the script which you have created. The first command will give execute permission to the current user and the second command will give execute permission to all the users.
How to run the script using Terminal:
So you have completed the tutorial of how to create a script in Linux and now we are going to see how to run the script in the Linux. Already we have seen how to give permission to run the script in Linux, so now use the below come at to run the script in the Linux.
Before that make sure you are in the directory which you have the script file using the command “ls”.
Above command will run the script and will show you the output of that script. Now you know how to successfully create a script and run the script in the Linux, but this script only runs on the folder where you have the file.
If you want to run the script all over the place where ever you are running the name, then you need to edit your profile. Editing your profile is nothing a big process, you just need to give the following command with the folder path where you have saved your script.
How to add profile entry:
As the first step, run the following command to “echo” the location of the path variable in your profile.
# echo $path
Like specified above, the output will show you the result of the path variable. Like that, you need to add the following command with the directory location where you have saved your script.
# export PATH=$PATH:directory
Now save the above command in your “.bash_profile”, so that whenever you are logging in or after rebooting, it automatically run the script from the specified location in your path variable.
The latest Linux distributions suggested to use a separate folder for each and every profile you are using in your Linux to run the scripts. They have also suggested to create a folder in the name of “bin” to save and run your scripts.
How to Run Scripts in GUI:
If you have created a Script that will open an application or it will run some of the GUI tools in your Linux distributions, then you just need to simply double click the script file which you want to run.
You also need to make sure that, you have given execute permission for the script which you are running through GUI tools. This can be done under the properties of the script file and inside the permission tab.
After reading this step by step tutorial, now you can able to create the script and you can also able to run the script in any of your Linux Distributions. Since I have used CentOS for this tutorial, you can use same CentOS or redhat or Ubuntu or Fedora to create and run the script.
Please do comment and give your feedback.