Console to the masses. Go to the bright side. Bash
Introduction
Ease of use of a tool is how much it helps in solving a specific problem. It is also important that we can customize this tool to your needs. A nice bonus is the fact that we can expand and complement our tool with new features.
We got to the most interesting and fascinating topic - these are bash scripts. When you start a terminal, a special shell program works inside it - the shell (English) - the command interpreter. Shell understands all the commands you type on the keyboard and processes them. It also displays error messages, monitors the correctness of commands and their syntax. An example of such commands might be: change directory, create a new directory, add a text file, edit a text file, save changes, and others . Shell programs are quite a few. One of the first successful implementations is called sh . It was developed by Stephen Born in 1977 ( wiki ). In 99% of cases, when you are working on a computer running OS *nix , the default shell that processes commands is bash. Of course, there is also a shell like zsh and others, but that’s another story. ')
An important point. This article is not a complete guide to bash scripts. The main goal is to examine the basics of bash programming, show examples, give the reader basic knowledge that is enough to move on. At the end of the article there are useful links and you can study this topic in more detail.
The basics
The first section is devoted to the basics. Consider how to start any script. How variables are determined and how to change the values of these variables. We will also understand how to pass arguments to the script and process these arguments inside the script. The next section is branching. And the last, but no less important section is cycles.
Any bash script should start with the line:
#!/bin/bash
Thus, we tell the interpreter what program to call if the script is executable.
To improve the portability of the code, the following line should be indicated:
#!/usr/bin/env bash
It is not guaranteed that on different systems the path to bash will be the same. By specifying the env we protect ourselves from such surprises. Again, this is if you plan to use your scripts on different systems, computers, etc. If this is not necessary, the first option is perfect. All examples use the first option.
By default, all files created by the user are not executable. We must explicitly specify this parameter for a specific file using the command:
chmod +x <filename>
You can run the script without this parameter using the command:
bash <filename>
Variables
A variable is a named area in memory. This means that the variable has a name and it refers to a specific memory location in the computer. Usually variables are used to store some information. Any variable consists of two parts: name and value.
Variable Name:
letters, numbers, underscore ( _ )
cannot start with a number
Variable value:
numbers, strings (if there are spaces, then in quotes), separate characters
Create (rewrite) variable:
path="$HOME"
Please note that before the equal sign and after it there should be no spaces.
Read variable:
"$path""${path}"
In most cases, both options work the same way. In some cases, when an ambiguity of interpretation occurs, only the following form of writing will work correctly: "${path}"
Just like that, on bash you can implement an example with “Hello, world!”. I remind you that all the examples you can find in the repository . Let's look at another example. This time we will work with variables, as well as with the arguments that the user passes to the script.
#!/bin/bash
var1="$1"
var2="$2"
echo"Arguments are ${var1} and ${var2}"
# ./variables.sh Hello world - how this script should be called
Notice that there is a semicolon at the end of each line. It is possible to write the conditions (and not only) in one line, then a semicolon is needed.
When working with branches, we need to somehow check whether the string is empty or not, whether the number is zero or not equal, whether we are working with a file or this is a directory. For such tasks, bash has its own syntax. I divided it into four categories: strings, numbers, files, logical. Let's look at them.
The script expects to receive one argument as input — the file name or the directory name. Next is checking whether it is really a file or directory. If so, do the removal. If the argument passed is neither a file nor a directory, output a message to the user that deletion is impossible.
It remains for us to consider the last variant of branching - this is case/in/esac . If we draw an analogy with JavaScript, then this is a variant of the implementation of switch/case . Consider this construction also by example.
This script expects to receive two numeric arguments: one and two. And depending on which argument is the first, a file or directory is created. There is also a check for the number of arguments that the user passed. The number of arguments should not be less than two. If the user passed incorrect arguments, we deduce to him the message on it. The default value is responsible for this.
Cycles
The next section, which we will look at, is working with cycles. For the organization of cycles in the arsenal of bash there are such constructions: for/in and while .
i is the name of the variable to which the array element will be passed. array is the array itself. At each subsequent iteration, i obtains the next value from the array. Consider an example:
There is a list of values: 1 2 3 4 5 . We start a cycle that runs through all values. Create a variable file_name . Then we check if there is a file with that name. If there is no file, we create it, if we skip the creation step.
You may have noticed the word continue is a keyword. Allows you to skip the iteration and continue to work on. Also there is a keyword break - interrupts the execution of the script. These keywords are used only in the context of if/else .
The following construction, which we consider to work with cycles, will be while :
In this example, there are two lines with the read keyword. Let us consider in more detail how these lines work. Namely: line number twelve and line number eight. Let's start with line number twelve. While the $again variable is equal to the value "yes" , the program asks to enter the user name and displays it on the screen. After this, the script asks if we want to continue. And so on until the user enters any other string instead of yes or simply presses Enter . But much more interesting is the code on line number eight. There is a variable $name , which is not declared before. This variable is created dynamically and then its value is read.
On this positive note, we’ll end up with bash. This information is enough to implement simple scripts, as well as to automate everyday tasks.
Looking ahead, I’ll say that in the next article there will be less theory and even more examples on how to automate routine tasks using bash scripts. Keep for updates.
That's all. Thanks for attention. Who read to the end, special thanks. See you in the next article.
UPD. The text of the article is updated based on the discussions in the comments. Thanks to everyone who took an active part in the discussions. You made the article even better. And not just an article.