Let’s take a look at basic Bash programming syntax and tools, as well as how to use variables and control operators, in this first of three articles.

The Shell is the command interpreter for the operating system, of which Bash is my favorite. Every time a user or system administrator types commands into the system, the Linux shell interpreter translates these commands into a form that the operating system can understand. When the results are returned to the shell program, it prints the results to STDOUT (standard output), which by default is displayed on your terminal. Every shell I’m familiar with is also a programming language.

Bash is a powerful shell with many convenient features such as TAB completion, command backtracking and reediting, aliases, and more. The default command line editing mode is Emacs, but one of my favorite Bash features is that I can change it to Vi mode to use those editing commands that are stored in my muscle memory.

However, if you use Bash as a pure shell, you won’t experience its true capabilities. In designing a three-volume Linux self-study course (on which this series of articles is based), I learned a lot about Bash that I hadn’t learned in my 20 years of Linux experience, some of it about Bash programming. It has to be said that Bash is a powerful programming language and a perfect design for both command line and shell scripts.

This series explores how to use Bash as a command line interface (CLI) programming language. The first article briefly covers Bash command-line programming, variables, and control operators. Other articles will discuss such things as: the type of Bash files; Strings, numbers, and logical operators that provide logical control over the flow of code execution; Different types of shell extensions; Loop operations are controlled through for, while, and until.

Shell

Bash is short for Bourne Again Shell, as Bash Shell is based on the earlier Bourne Shell, which was developed by Steven Bourne in 1977. There are plenty of other shells available, but here are the four I see most often:

  • cshThe C shell is suitable for developers who are used to the C language syntax.
  • ksh: Korn shell, developed by David Korn, is more popular among Unix users.
  • tcsh: a CSH variant that adds some ease of use.
  • zsh: Z Shell, which integrates many other popular shell features.

All shells have built-in commands that complement or replace the core toolset. Open the man page of the shell and go to the “built-in” section to see what commands are available.

Each shell has its own features and syntax style. I’ve used CSH, KSH, and ZSH, but I still prefer Bash. You can try a few more to find a shell that works better for you, though it may take some work. Fortunately, switching between shells is easy.

All of these shells are both programming languages and command interpreters. Let’s take a quick look at the programming structures and tools integrated within Bash.

Bash as a programming language

In most cases, Bash is used by system administrators to send straightforward commands. But Bash can do more than just type a single command. Many system administrators can write simple command-line programs to perform a series of tasks. These programs can serve as general-purpose tools that save time and effort.

The purpose of writing CLI programs is to be efficient (being a “lazy” system administrator). In a CLI program, you can list several commands in a specific order and execute them one by one. Instead of staring at the screen, waiting for one command to complete and then typing another, you can use the time saved to do other things.

What is a program?

The Free Online Computer Dictionary (FOLDOC) defines a program as “instructions executed by a computer, not the physical hardware on which they are run.” WordNet from Princeton University defines a program as: “… A set of instructions that a computer can understand and execute…” Wikipedia also has a good entry on computer programs.

To summarize, a program consists of one or more instructions for the purpose of performing a specific related task. For a system administrator, a program typically consists of a series of shell commands. All shells under Linux (at least the ones I’m familiar with) have basic programming capabilities, and Bash, as the default shell for most Linux distributions, is no exception.

This series uses Bash as an example (because it’s ubiquitous), and it doesn’t matter if you use a different shell. The structure and syntax are different, but the programming philosophy is the same. Some shells support certain features that others do not, but they all provide programming capabilities. Shell programs can be stored in a file for repeated use, or they can be created as needed.

Simple CLI program

The simplest command line programs have only one or two statements, which may or may not be related, that are entered into the command line before the enter key is pressed. The second statement (if any) in the program may depend on the operation of the first statement, but it is not necessary.

There is one punctuation mark in particular. When you type a command on the command line and press enter, there is an implied semicolon (;) at the end of the command. . When a CLI shell program is strung together as a single line of instruction on the command line, a semicolon must be used to terminate each statement and separate it from the next. But the last statement in a CLI shell program can use either an explicit or implicit semicolon.

Some basic syntax

The following examples illustrate this grammatical rule. The program consists of a single command with an explicit terminator:

[student@studentvm1 ~]$ echo "Hello world." ;
Hello world.
Copy the code

It doesn’t look like a program, but it’s the first program I write as I learn every new programming language. Different languages may have different syntax, but the output is the same.

Let’s extend this trivial but ubiquitous code. Your results may differ from mine because my home directory is a bit messy and you may be logging in to your account for the first time from a GUI desktop.

[student@studentvm1 ~]$ echo "My home directory."; ls ; My home directory. chapter25 TestFile1.Linux dmesg2.txt Downloads newfile.txt softlink1 testdir6 chapter26 TestFile1.mac  dmesg3.txt file005 Pictures Templates testdir TestFile1 Desktop dmesg.txt link3 Public testdir Videos TestFile1.dos dmesg1.txt Documents Music random.txt testdir1Copy the code

Isn’t it more obvious now? The results are related, but the two statements are independent of each other. You may have noticed that I like to type an extra space before and after the semicolon to make the code more readable. Let’s run the program again, this time without the closing semicolon:

[student@studentvm1 ~]$ echo "My home directory." ; ls
Copy the code

The output makes no difference.

About the variable

Like all other programming languages, Bash supports variables. A variable is a symbolic name that refers to a location in memory where the corresponding value is stored. The value of a variable can be changed, so it is called “variable”.

Unlike languages such as C, Bash enforces variable types such as integer, floating point, or character. In Bash, all variables are strings. Integer variables can be used for integer operations, which is the only mathematical type that Bash can handle. More complex operations require commands like BC, which can be used in command-line programming or scripting.

The values of variables are pre-assigned and can be used in command line programming or scripting. You can assign a value to a variable by its name, but you cannot start it with a $character. For example, VAR=10 will set the value of VAR to 10. To print the value of a variable, you can use the statement echo $VAR. Variable names must start with text (that is, not a number).

Bash saves defined variables until they are cancelled.

In this example, the value of a variable is null before it is assigned. And then assign it a value and print it out, check it out. You can do this in the same LINE of CLI:

[student@studentvm1 ~]$ echo $MyVar ; MyVar="Hello World" ; echo $MyVar ;

Hello World
[student@studentvm1 ~]$
Copy the code

Note: Variable assignment syntax is very strict, equal sign (=) no Spaces on either side.

The blank line indicates that the initial value of MyVar is null. Variables are assigned and revalued. this example shows the original and new values.

As mentioned earlier, Bash supports integer arithmetic, which is useful when you want to calculate the position of an element in an array or do some simple arithmetic operations. However, this approach is not suitable for scientific calculations, or for some scenarios that require decimals, such as financial statistics. There are other, better tools for these scenarios.

Here’s a simple math problem:

[student@studentvm1 ~]$ Var1="Seven" ; Var2="9" ; echo "Result = $((Var1*Var2))"
Result = 63
Copy the code

That seems fine, but what happens if the result is a floating-point number?

[student@studentvm1 ~]$ Var1="Seven" ; Var2="9" ; echo "Result = $((Var1/Var2))"
Result = 0
[student@studentvm1 ~]$ Var1="Seven" ; Var2="9" ; echo "Result = $((Var2/Var1))"
Result = 1
[student@studentvm1 ~]$
Copy the code

It’s going to be rounded. Note that the calculation is included in the echo statement and is actually done before the echo command ends because of Bash’s internal priority. To learn more, search for “precedence” on Bash’s man page.

Control operator

The Shell’s control operator is a syntactic operator that makes it easy to create some interesting command-line programs. The simplest CLI program is to string several commands together in sequence on the command line:

command1;command2;command3;command4. ...; etc. ;Copy the code

As long as there are no mistakes, these commands can be executed smoothly. But what if it goes wrong? You can set a good way to go wrong, it will use the Bash the built-in control operator, && and | |. These operators provide flow control that allows you to change the order in which code is executed. The semicolon can also be thought of as a kind of Bash operator, indicating the start of a new line.

The && operator provides the simple logic, “If Command1 succeeds, then command2 follows. If Command1 fails, skip Command2.” The syntax is as follows:

command1 && command2
Copy the code

Now, let’s use the command to create a new directory and, if successful, switch it to the current directory. Make sure your home directory (~) is the current directory, try to create it under /root first, you should not have permissions:

[student@studentvm1 ~]$ Dir=/root/testdir ; mkdir $Dir/ && cd $Dir
mkdir: cannot create directory '/root/testdir/': Permission denied
[student@studentvm1 ~]$
Copy the code

The above error message was thrown by the mkdir command because the directory creation failed. The && operator receives a non-zero return code, so the CD command is skipped, preventing the latter from continuing because the directory creation failed. This control process prevents subsequent errors from accumulating and causing more serious problems. It’s time for a little more complicated logic.

When a program return code is greater than zero, use the | | operator can let you in the back and then perform another program. The simple syntax is as follows:

command1 | |command2
Copy the code

To read, “If command1 fails, execute Command2.” The hidden logic is that if Command1 succeeds, skip Command2. To practice, create a new directory:

[student@studentvm1 ~]$ Dir=/root/testdir ; mkdir $Dir || echo "$Dir was not created."
mkdir: cannot create directory '/root/testdir': Permission denied
/root/testdir was not created.
[student@studentvm1 ~]$
Copy the code

As expected, the first command failed because the directory could not be created, and the second command was executed.

&& and | | combined two operators to maximize their efficacy. Look at the process control method in the following example:

Front commands;command1 && command2 | |command3; Follow commandsCopy the code

Syntax explanation: “If command1 exits with a return code of zero, command2 is executed, otherwise command3 is executed.” Try this with specific code:

[student@studentvm1 ~]$ Dir=/root/testdir ; mkdir $Dir && cd $Dir || echo "$Dir was not created."
mkdir: cannot create directory '/root/testdir': Permission denied
/root/testdir was not created.
[student@studentvm1 ~]$
Copy the code

Now let’s try again and replace /root with your home directory. You’ll have permission to create this directory:

[student@studentvm1 ~]$ Dir=~/testdir ; mkdir $Dir && cd $Dir || echo "$Dir was not created."
[student@studentvm1 testdir]$
Copy the code

A control statement like Command1 && Command2 works because the shell sends a return code at the end of each command to indicate its success. By default, a return code of 0 indicates success and any other positive value indicates failure. Some systems administrator tools use a return code of 1 to indicate failure, but many other programs use other numbers to indicate failure.

Bash’s built-in variable $? The return code for the previous command can be displayed, which can be easily checked in a script or on the command line. To see the return code, let’s start by running a simple command. The result of the return code is always given by the previous command.

[student@studentvm1 testdir]$ ll ; echo "RC = $?"
total 1264
drwxrwxr-x  2 student student   4096 Mar  2 08:21 chapter25
drwxrwxr-x  2 student student   4096 Mar 21 15:27 chapter26
-rwxr-xr-x  1 student student     92 Mar 20 15:53 TestFile1
drwxrwxr-x. 2 student student 663552 Feb 21 14:12 testdir
drwxr-xr-x. 2 student student   4096 Dec 22 13:15 Videos
RC = 0
[student@studentvm1 testdir]$
Copy the code

In this case, the return code is zero, which means the command executed successfully. Now test root’s home directory, you should have no permissions:

[student@studentvm1 testdir]$ ll /root ; echo "RC = $?"
ls: cannot open directory '/root': Permission denied
RC = 2
[student@studentvm1 testdir]$
Copy the code

In this case, the return code is 2, indicating that non-root users do not have permission to access the directory. You can use these return codes to change the order in which the program is executed using control operators.

conclusion

This article looks at Bash as a programming language and introduces its simple syntax and basic tools from that perspective. We learned how to output data to STDOUT and how to use variables and control operators. In the next article in this series, we will focus on logical operators that control the flow of instruction execution.


Via: opensource.com/article/19/…

Author: David Both

This article is originally compiled by LCTT and released in Linux China