Bash basic programming Summary

Source: Internet
Author: User
Tags case statement

Bash should be the most popular shell script interpreter in Linux. (another shell is called Dash. I hate it too much .), As long as you work on Linux and want to enjoy yourself, you should be familiar with the most basic bash programming, because it will bring enough happiness to your work. This article will summarize some basic bash programming knowledge that I usually use, and share it with you for ease of Self-query.

Variable

1. Bash variable names are case-sensitive and cannot start with numbers. I have read a lot of code. To be honest, I have never seen anyone whose code starts with a variable name. I think that even if the language permits, there are very few people doing this, unless you are really special.


2. variable definition and assignment

AAA = 123

Note that there must be no spaces before and after the equal sign when defining a variable, and it must be closely followed by writing. Although there are spaces after the equal sign, the syntax may not be wrong, but the result is absolutely wrong.


3. Variable concatenation

Bbb =$ {AAA} 123

Most of the time, we may need to use some variables, constant strings, and so on to splice a new variable. In this case, note that the variable to be spliced may need to be added {}, otherwise, a variable Identification error may occur and the variable cannot be found. In this case, I prefer to add {} to all variables {}.

4. local and export

When defining a variable, there are two common keywords: local and export. In the following example, the export defines the local of the local variable, but I don't need it. I will make a summary when I use it.

The definition of variables is also the case. It is enough to understand such a little bit without chewing on the details. If your goal is to become a shell expert, you need professional-level learning. There are a lot of huge shells in Linux to learn.


Condition judgment

If condition expressions have a long-term comparisonString,IntegerAndFile AttributesComparison.

If statement format:

If [expr]; then

Do something

Fi


If [expr]; then

Do something

Else

Do something

Fi


If [expr]; then

Do something

Elif [expr]; then

Do something

Else

So something

Fi


The IF statement is different but similar to other languages (C, Java. The key of then can be another line, so that the semicolon after the conditional expression can be omitted. Note that at least one space is required before and after "[" and.

 

1. Integer comparison

The operators involved in integer size comparison include-lt,-Le,-EQ,-GT,-Ge, and-ne.

Example:

A = 1
B = 2

If [$ A-lt $ B]; then
Echo "A <B"
Else
Echo "A> = B"
Fi

If [$ A-ne 3]; then
Echo "! = 3"
Else
Echo "A = 3"
Fi

If [1-GT 3]; then
Echo "1> 3"
Else
Echo "1 <= 3"
Fi


When we use the six operators for integer size comparison, the two operands involved will be processed as numerical values rather than strings, even if you use double quotation marks to compare objects.


2. String comparison

Possible operators for comparing strings include -- = ,! =,>, <,-N, and-Z.

Example:
S1 = aaa
S2 = bbb

If [$ S1 \ <$ s2]; then
Echo "$ S1 <$ S2"
Else
Echo "$ S1 >=$ S2"
Fi

If [[$ S1 <$ s2]; then
Echo "$ S1 <$ S2"
Else
Echo "$ S1 >=$ S2"
Fi

If [$ S1 = $ s2]; then
Echo "$ S1 = $ S2"
Else
Echo "$ S1! = $ S2"
Fi

If [-N $ S1]; then
Echo $ S1
Fi

For character string size comparison,> and <are special. Escape is required when writing in []. Otherwise, [[] can be used instead of []. Do not escape,> and <are interpreted as IO redirection operations. Second, the IF statement body cannot be empty. At least one thing must be done.


The operator-N is used to judge whether the string is not null, that is, the length is not 0. -Z determines that the string is null, that is, the length is 0.


3. File Attribute judgment

Many operators are involved in File Attribute judgment, as shown below:

-E and-A: The file exists.

-D: Directory

-F: File

-R: readable

-W: writable

-X: executable

F1-nt F2: F1 is newer than F2

F1-ot F2: F1 is older than F2


The usage of these operators is basically the same as that of the string operators. Among these operators, I use-E and-F most frequently, and others use less.

In fact, in addition to these three types of commonly used judgments, there should also be more commonly used judgments for Executing command results. This article will not summarize them.

For Loop

There are two main forms of common for loops:

Form 1

For E in [LIST]

Do

Do something

Done

In this format, for is often used to traverse all elements in a list set and process them. For example:

1. traverse all files in a directory

For file in 'ls dir'

Do

Echo $ File

Done


2. traverse a given set

List = "a B C D E F G"

For E in $ list

Do

Echo $ E

Done


When traversing a set, in the for in format, strings are separated by spaces to obtain each element, in the above example, the 'ls dir' and $ list are both strings with blank characters.


Form 2

For (I = 0; I <n; I ++ ))

Do

Do something

Done

This form of for is basically the same as the C language, but it only requires double brackets. It is better at calculating the number of definite cycles. For example:


For (I = 0; I <10; I ++ ))

Do

Echo $ I

Done


For ((;;))

Do

Echo "aaaa"

Done

A For Loop without Counters is an endless loop, which is consistent with the C language.


Having mastered this for loop statement, we can do a lot of things, basically enough for us to play.

While Loop

I usually don't need this bird's eggs. I need to use for everything that needs to be recycled. The reason for listing the while loop is to use it to spray various while, do while, and until in various languages. A loop has to come up with a variety of expressions, but golang is the best. There is too much syntactic sugar, and you will get bored one day. Haha.

Case statement

The case statement is equivalent to the switch statement in most languages. In addition to the IF-Elif feature, this feature also supports wildcards, which are quite useful. Let's look at the example.

Example:

Url = www.tmall.com

Case $ URL in
Www.taobao.com) Echo 1 ;;
* .Taobao.com) Echo 2 ;;
* .Tmall.com) Echo 3 ;;
Www.tmall.com) Echo 4 ;;
*) Echo 5 ;;
Esac


In the preceding example, the condition branch not only has a constant string but also a string containing a wildcard, which is very convenient for pattern matching. Second, you need to note that the case language matches one by one from the first in the matching process. The output result of all the above cases is 3, rather than the exact match 4. I think this is a small pity. It would be more fun to support exact matching first.

Note that the end of each condition branch must be a double semicolon.


Well, the case language is relatively simple, but it is very practical.

Function Definition

Function Hello ()

{

Echo "Hello World"

}

Function defines the practical keyword function. The brackets behind the function name are optional.

Function call

For a function without parameters, you only need to give the function name. The previously defined function can be called directly using hello. For a function with parameters, you only need to give the parameters following the function name in sequence, for example, func_name arg1 arg2.

Function Parameters

When a function is called, parameters can be passed to the function. How can we obtain these parameters in the function body? Function parameters are obtained by $1, $2, and so on. For example:

Function add ()
{
N = $1
M = $2
Echo $ (n + M ))
}

Call Add 1 2 to output 3.

In a function, several parameters are passed when the value of $ # is used to determine the function call.


Return is rarely used in bash functions. However, you can call a function to obtain the calculation result, for example:


Res = 'add 1 2'

The value of the res variable is 3. Note that the above is not a single quotation mark, but a character next to number 1.

String processing

String processing is almost the most frequent task in the computer world. Many people playing C language are writing string processing programs. Most Java users do not need to write character string processing programs themselves, but they are basically calling the character string processing method. Linux actually has a very powerful tool that allows us to perform string processing. Before getting familiar with it, every tool looks very complicated. Here is a brief summary of the strings you have used.


1. substring

STR = abcdefg

Echo $ {STR: 2: 3} returns BCD. 2 In the expression represents the offset, and 3 represents the length.


2. Evaluate the string length

Str= 123456

Echo $ {# STR}


3. String replacement

$ {Variable/pattern/XX} replaces the first match in the variable with xx.

$ {Variable // pattern/XX} replaces all matches in the variable with xx.


STR = aaabbbccc

Echo $ {str/AAA/xxx}

Echo $ {str/A/x}


There are many tools for string processing, such as using cut, awk, and other programs to split a string.

Integer Operation

The integer operation is generally performed using $ (expr. For example:

Echo $(1 + 2 ))
A = 1
Echo $ (a + 2 ))
Echo $ (A ++ ))
Echo $ (++ ))
B = 2
Echo $ (A + B)/2 ))

You can see that you only need to insert the calculation expression to $ (). The variable involved in calculation does not need the $ symbol. Integer operations involve the following operators: + +, --, +,-, *,/, %, + =, and ** (power operation), and are not commonly used: <,>, ^ ,&,! , | ,~, These are quite common in the C language, and the demand for shell to perform these bitwise operations is too 2B. In addition to these operations, the following logics are supported: <,>, <=, >=, =, and ,! =, &, |.

$ (Expr) expressions can only calculate integers and cannot be used for decimal calculations. If you want to calculate decimals, you can use the command line calculator BC on Linux.

Example:

Echo "1.5 + 1" | BC

Code Generation

Programmers should be "lazy" and "lazy" programmers can create more automation tools. I like to use Bash to help me automatically generate some code. Of course, not all code can be automatically generated. If so, no programmer is needed. In my opinion, good programs must be extensible. The best extensible program is that you do not need to write too much code in the development phase of functional requirements after the program framework is developed, you only need to fill in the Code ". The code that can be filled in must be automatically generated, so that program development will form a virtuous circle. Recently, I am very keen on refactoring my code and try to make my code automatically generated using the bash script, which will greatly improve my development efficiency.


How to automatically generate code and what kind of code is based on your actual situation. Here we cannot introduce specific code generation. We can only introduce the tool-Cat used by Bash to generate code. Anyone who has used Linux knows cat, which is not detailed here. Only a simple bash script is used to output the hello World Program.


#! /Bin/sh


Cat <End> hello. c
# Include <stdio. h>

Int main (void)
{
Printf ("Hello world \ n ");
Return 0;
}
End

Between cat and end, you can write your own code like in the editor. what is written here, and what is output to the source file, including indentation and other formats. Of course, truly meaningful code generation won't be as simple as Hello world here. You must "splice" the automated code based on your actual situation. Here we only show how to use Cat to generate a source code file. To automatically generate truly meaningful code that can improve your development efficiency, you must learn the vast majority of bash programming knowledge.

Source (.) and Export

Linux users all know that the terminal we beat the command is actually running in a shell, which defines a lot of environment variables, such as path and home. Then, after I run a bash script program on the terminal, the bash script is actually running in a sub-shell process from the shell Fork where the terminal is located. Why can this sub-shell also obtain environment variables such as path and home? This is because these variables are declared as export in the parent shell. The Export command allows variables to be passed to the sub-shell, which is useful when a bash program calls another bash program.

Export allows variables to be passed to the sub-shell, but it does not allow the sub-shell variables to be passed to the parent shell. Write it here. I think this turning point is really awkward. However, you can use the source command in Bash to complete similar functions. For example, we can use source in the terminal to run a bash program. The variables defined in this program still exist after the program ends. This is because executing a script through source does not go to fork to generate a sub-shell for execution, but directly executes all statements in the script in the current shell. Speaking of this, I think you will think like me that source is equivalent to the include command in C language.

When a script is too large, I am keen to modularize it, And then separate some modules as a separate file, and then execute it through the source command in the main file. If you are as lazy as you are, and you are too lazy to even do not want to write the source, you can use the dot (.) to replace the source.

Read files row by row

When you are doing text processing, you may need to read the content of a file one by one row and then process it. Bash is very convenient to do this.

Example:

While read line
Do
Echo $ line
Done <TXT. Log

The example shows how to assign a value to the variable line from the row-by-row reading in the TXT. log file. If the content of your file is structured, for example, each row has two columns, and you want to process the content of each column separately, bash provides a more friendly way to read files row by row.

Example:

While read C1 C2

Do

Echo $ C1: $ C2

Done

In this way, the first column is assigned to C1, and the second column is assigned to C2. This is a bit of a taste of multi-variable assignment supported by ruby, Python, go, and other excellent languages.

Format output

ECHO is the most commonly used output in bash, but Echo cannot be formatted. In the case of automatic generation of code, I think you may need to format the output to make the code more neat and beautiful. C language functions printf support formatting output, and Linux also provides printf tools, allowing you to output as you like in C language.

General Usage example of printf:

Printf "My name is % s, I am % d years old. \ n" skoo 25

I think the use of Linux printf is almost the same as that of C language. The detailed usage can be done by man printf.

Appendix: SED and awk

I think SED and awk are two major artifacts, but there are not many people who can really control them. I just know a little bit about it, so I won't talk nonsense here. When learning any programming language to reflect productivity, it is a combination of libraries. Bash has no library function statement, but it has a variety of Linux commands. To write a truly powerful and meaningful bash program, you have to learn some Linux commands and only have enough Linux commands, to be easy to learn. Just like most Java programmers, without rich libraries, they may not be able to do anything meaningful just to grasp the syntax. C/C ++ programmers are no exception, although they are keen to use the only syntax to recreate the wheel.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.