Hardware

How to make a shell script in linux

Table of contents:

Anonim

We have already talked enough about the commands in Linux and the console, we have learned to manipulate them to carry out specific activities, how we can combine them and the ease they give us to perform certain tasks. In this case, we are going to introduce ourselves to the operation of a shell script in Linux, a fabulous tool for automating tasks and executing them from the console. If you want to know how to make a shell script in Linux, keep reading our article.

How to make a shell script in Linux

What is a Script?

A script is nothing more than a text file whose content is a set of command lines, which are executed sequentially from start to finish. In this way, we can structure in a script the commands that we would like to execute by keyboard and also automate it through a task, if it is something that is done frequently.

How to create a Shell Script

The process to create a shell script is very simple. As simple as creating a new file and giving it the.sh extension. There are two alternatives to do it, it can be through the graphical interface or from the console using the touch command.

For example, touch test.sh

And in this way, the file test.sh would be created in the current directory.

This file can be opened, also with two alternatives, from a text editor of the graphical environment (for example, gedit) or from the terminal with Vim.

Script first line

Now that we have the file created and open, we must indicate to Linux that said file will be a script. Therefore, all shell scripts must have a first line that is:

#! / bin / bash

This line can be divided into two parts, the first corresponds to #! This sequence is called sha bang. Basically, its function is to indicate to the system that a set of instructions will be presented below and thus be processed. The second part, / bin / bash, indicates the shell that will be used to execute the commands.

Comments

At the moment we will not cover in depth what corresponds to shell script programming, but it is essential that you know how to add comments within your script. If you are a programmer, you will understand how useful and important they are. For those who do not know how to program, a comment does not add functionality to the system, but they are relevant to explain the operation of a program, the script in this case.

Comments can be added using the # symbol. And we add the text that seems relevant to us, after the pad. Normally the comment is placed before the instruction, in order to explain some functionality, but they can be used less or more frequently in the shell script.

Adding commands

Within the shell script we can use all the commands that we have learned from Linux. In other words, any instruction that we can enter through the console can be included in the script. But, additionally you can add many other tools such as conditional structures, arithmetic operators, comparators, among others.

We are going to use a fairly simple example in this case, including basic commands:

#! / bin / bash # Script from ovtoaster.com # We put ourselves in the directory of our user cd ~ # We print the Kernel that we use uname -r on screen # We print on screen the current date date # We create a folder called Documents mkdir TestDocuments # We move to the Documents folder cd TestDocuments # We create a txt called tips touch tips.txt #… We can continue writing all the commands we want, the script will execute them all sequentially.

Finally we save the changes in our file and with that the script is almost ready to work…

Running the script

Before executing the shell script, we must grant execution permissions to the file. This is a very simple thing to do. We go to the terminal and we are located in the directory of our script and we use the command chmod:

WE RECOMMEND YOUHow to edit files in linux: Vi Text Editor is your best friend

If we want to grant permissions to the current user, we use:

sudo chmod 775 test.sh

In case we want to grant permissions to all users, the sentence would be:

sudo chmod 777 test.sh

Once we have already granted the permissions, we run the script:

./test.sh

With this we finish, our fully functional script and perfect to run when we need it and even to schedule it on task.

We recommend reading the guide for beginners in Linux.

We hope that the topic has been useful and do not forget to share your experiences and opinions in our comments?

Hardware

Editor's choice

Back to top button