Tools for writing scripts in Linux, Linux operating system course of lectures Shymkent

Tools for writing scripts in Linux, Linux operating system course of lectures Shymkent

Articles

In the previous article, we touched upon such an important and interesting topic as Linux shell scripts. It is worth noting that the lion's share of automation tools is essentially based on scripts. Therefore, it is very important to master this tool if you plan to work in the field of system administration. Or to become an engineer DevOps/DevNet. Today we will tell you about the tools that will help you write a complex script - this is included in Linux operating system lecture course Shymkent.

Variables in Linux shell scripts, Linux operating system lecture course Shymkent

As we have already written in previous articles, shell scripts are the simplest programs. And they are written in full compliance with the general rules for writing programs. Which are characteristic to one degree or another of all high-level programming languages. Therefore, it is completely natural that in this matter one cannot do without the use of variables. Variables are named special memory areas used for temporary storage of data of different types. Our Linux security course Shymkent on the platform SEDICOMM University.

At the same time, declaring a variable and assigning it a value is quite simple. To do this, use the following syntax: [variable name]=[variable value]. To get the value of a variable, just use the following construction — $[variable name]. For example:

  • str=”Text” — create a variable with a value in the form of a text string;
  • $str — we get the value of the variable.

It is worth noting that in the shell bash, which we use by default in this article, a space is a separator. Therefore, there are no spaces before or after the equal sign. Otherwise, the logic of code execution will not be as needed for our examples.

Linux Shell Script Arguments

Scripts can work not only with the information they have in the code or received by executing commands. Among other things, the user can pass input data as arguments when running the script. In this case, their numbering starts from one, since the zeroth argument is the name. To call the value of an argument, use the following syntax - $[argument number].

This value can then be used in a similar way to variables. Our Linux torrent courses ShymkentIt is worth noting that when processing a large number of arguments, it is very useful to use a simple for loop.

Conditional statements and loops for Linux scripts

Of course, the main tools when writing scripts are conditional operators and cycles. Let's start with conditional operators, which allow the algorithm to branch when specified conditions are met. To do this, use the following syntax: if [condition] then [command] else [command] fi.

In this case, the comparison of the values ​​of arguments, variables, etc. using mathematical operations is usually used as a condition. The text of the condition should be enclosed in double square brackets, for example: [[$1>0]] or [[$str!=”Text”]]. Operator fi - this is a mirror operator if, symbolizing the completion of the conditional branching block. You can learn more by going through Linux online courses Shymkent on the platform SEDICOMM University.

The simplest loop in Linux scripts is the loop for. To create it, use the following syntax - for [variable] in [list] do [command] done. In such a construction, the variable is a counter that will take values ​​from the list one by one. For example, a sequence of numbers separated by spaces can be used as a list.

Next, after the operator do there is a command or block of commands executed at each iteration. At the same time, the done operator completes the loop construction. This will be more than enough for the beginning. In one of the following articles, we will continue to introduce you to tools for writing programs (shell scripts) Linux.

Our team SEDICOMM University: Cisco Academy, Linux Professional Institute, Python Institute.