shell basic learning notes

Shell provides users with a simple interface to interact with the operating system, and natively supports redirection, f...

Shell provides users with a simple interface to interact with the operating system, and natively supports redirection, file operation and other features, so that they can more easily complete some script tasks. A shell script is actually a collection of a series of statements supported by the shell to perform a series of tasks. Here's the main record Missing Semester of Your CS Education And the contents about Shell and its script in Uncle bird's Linux private dishes. At the same time, man bash is also an important reference source for introduction and notes.

shell script execution

Take the test.sh script in the current directory as an example, you can execute shell scripts in a variety of ways.

./test.sh #test.sh is required to execute by directly specifying the script location Have Execution Authority bash test.sh #Use bash to explain the execution. / test.sh The script in the file is not required at this time test.sh Have executable permissions source test.sh #Read the command from test.sh and support it

Among the above three execution methods, the first two execute scripts in the new process, so they will not affect the parent process, while the third execution method using the source command directly executes the corresponding statements in the current process, so the changes are visible in the current process.

Debugging of shell script

When executing a script through bash, such as bash test.sh, you can debug through the built-in parameters of bash. The more useful parameters are - n and - x parameters.

bash -n test.sh #For test.sh Syntax checking without actual execution bash -x test.sh #Output the content actually expanded during test.sh execution

Shebang  - Start of shell script

The "#!" character sequence at the beginning of the script is called Shebang. When a file starting with Shebang is used as a shell script, the program loader will other contents of the initial line where "#!" (more common, such as "/ bin/bash") As interpreter directive. The loader will execute the corresponding interpreter and pass the execution path of the original script as a parameter to the executor. Note that the above Shebang's function description can not only use the shell as the interpreter, for example, you can specify the Python interpreter in the Shebang section to execute the python script (#! / user/bin/env python).

Some notes in Bash:

1. Use escape character to eliminate the special meaning of some characters. In the shell, the character '\' is treated as a transfer character.

2. In addition to executing commands line by line on the terminal, the shell generally supports simple scripting language to support variables, control flow, loops, etc.

3. Multiple commands can be split with ".  

Variable

Variables in the shell are case sensitive. By default, variables in the shell are character type, assigned by "=" and accessed by "$". Variables of numeric type can be declared by declare command. At the same time, it should be noted that bash only supports integer type calculation. If the assignment operation is not performed, the default value of the variable is null string

var=test //variable var Assigned as test var= //default var Empty $var //Access variable var $ //Access variable var

It should be noted that no spaces should be added on the left and right sides of the assignment statement, because the shell uses blank characters as token boundaries. If spaces are inserted, they will be regarded as a form similar to command calls. Compared with the first variable access method, the second method can more easily distinguish variables, such as $vartest and $test. The former will expand the variable vartest, while the latter will expand the var.

The shell supports the extended use of special characters (mainly wildcards? And *), variables and commands.

*.conf // Wildcard expansion will be performed during execution,* You can match any number of characters, such as test.conf, a.conf Can match ?.conf // ? You can match a single character, such as a.conf, b.conf $var // Accessing values in variables $(command) // Command replacement, return value is command Results of execution `command` // Another way of writing command substitution, using `` Symbol

shell globbing

When writing shell scripts, wildcards including "*" and "?" can be used to expand names with the same pattern, where "*" can match any number of characters and "? Can match one character. You can also use "{}" to extend the same type for some substrings.

rm foo* //Similar will be deleted foo1, foo12 Similar mv ×{.py, .sh} folder //All are moved to .sh and .py End of file touch / //establish foo.a - foo.h as well as bar.a - bar.h

Common substitution

Use $(CMD) to get the output of executing the command CMD. For example, $(date) get the result of the date command execution.

Process substitution ([rpcess substitution)

< (CMD) executes CMD and stores the results in a temporary file, replacing < (CMD) with the name of the temporary file.

diff <(ls foo) <(ls bar) //compare foo Catalog and bar Different files in the directory

Quote

Correspondingly, the shell supports three different quote methods, which can limit the shell's default expansion behavior of special characters, variables and commands, mainly double quotation marks' ', single quotation marks'', and backslashes \

handle Example "xxx" Supports variable expansion "$test" - expands the value of the variable test Do not process wildcards "*. conf" - treat * as a normal character Support command replacement "$(ls)" - returns the result after executing ls as a command 'xxx' Do not expand variables '$test' - treat $test as a normal string Do not process unifiers "*. conf" - treat * as a normal character No command replacement "$(ls)" - treat $(ls) as a normal string \x Transfer of special characters through \ Line breaks, special characters such as' $',' * ','? 'and quotation marks can be escaped

It should be noted here that the escape operation of \ transfer only works on the next character. There should be no space between \ and the character to be escaped, otherwise it will be regarded as the escape of the space

Numerical calculation

By default, the content in the shell is regarded as a string without numerical operation. There are two main methods to use numerical operation. One is to declare the variable as an integer type through the declare command, and the other is to use the "((xxx))" symbol to include the operation content. Some of them will be regarded as numerical calculation rather than ordinary string operation.

Note that the assignment method in the form of test=((expr)) cannot be used. In this case, the content on the right side of "=" will be regarded as the string assignment content, and "(" cannot be used as an ordinary character (unless you use \ for escape operation). The correct approach is to test=$((expr)), and expand it with $.

((expr)) //expr It will be evaluated in the way of arithmetic calculation $((expr)) //get expr Calculation results of declare -i test=5 //definition test Is an integer type, which can be integer operated test=$test+5 //The variable value is 10

If other types are assigned to an integer type such as test above, the assigned content will be converted to an integer type, such as test=a. at this time, the value of test is 0 (note that it is not the ascii code value of a).

Control statement

shell supports loop, condition judgment and other operations to control the program flow.

Conditional statement

In shell scripts, the and operator "& &" and or operator "|" can be used for program control. It is often used to process the return value of a function. Note that these two operators have slightly different meanings from those in C/C + +.

command1 && command2 //If and only if the execution result of command1 is 0 command1 || command2 //If and only if the execution result of command1 is not 0

Shell scripts support rich comparison operations, which can be used by "[]" (test) or "[[]]" (test contract) operators, and the operation results return true or false. The supported comparison parameters can directly refer to the operations supported by the test command. For details, refer to the man test content (in fact, the writing method in the shell is the form of the corresponding test command after removing test). The common operations are as follows:

//string comparison str1 = str2 //String equality str1 != str2 //String inequality //Digital comparison int1 -eq int2 // =(equal) int1 -ge int2 // >=(greater equal),In addition gt(greater than),le(less equal),lt(less than),ne(not equal) //File comparison file1 -ef file2 //file1 and file2 Have the same equipment and inode number file1 -nt file2 //file1 of mtime in comparison with file2 new file1 -ot file2 //file1 in comparison with file2 used //File characteristic judgment -d FILE //The file exists and is executable -w FILE //The file exists and is writable -e FILE //File exists //Multiple judgment -o //Any condition holds,-e file1 -o -e file2, file1 or file2 Return if any one exists true -a //Return if both conditions are true true ! //The result is reversed, e.g ! -e file1

The result returned by the above statements is true or false, which is often used in if conditional judgment statements and other locations.

In addition, it is recommended to use the above judgment in double brackets. For example, to judge whether a file exists, you can use [[$? - eq 0]] to judge whether the return result of the previous statement is 0. For the difference between "[]" and "[[]]", please refer to What is the difference between test, [ and [[ ?

Judgment statement

If judgment statement, in which the conditional judgment statement can be performed using the conditional judgment syntax described above. At the same time, in addition to using compound conditional judgment statements such as - o/-a, you can also use & & (and) and | (or) to connect conditional judgment of multiple brackets in the judgment statements of if statements. For example, the functions of [- e file1 -a -e file2] are the same as those of [- e file1] & & [- e File2].

if [Condition judgment 1]; then xxxxx elif [Condition judgment 2]; then //As long as there is conditional judgment, it needs to be connected then keyword xxxxx else xxxxx fi //End condition judgment

Use the case... In.. ESAC keyword to determine the match.

case $1 in // case And in Are keywords, $1 What to match for "1") //brackets ")" Before is the content for matching // statements ;; //Indicates the end of a program segment ×) //"*" Indicates the default matching content,It actually acts like a wildcard //statements ;; esac //case End of statement

Functions

The shell contains simple support for functions, and the definition of functions should be visible before being called. The basic definition of the function is as follows.

function funcName() { //statements }

When calling a function, you can call it directly in the form of function name + parameter. For example, funcName 1 calls a function with parameter 1. Inside the function, parameters are accessed in a similar way to shell script access parameters, where $1 represents the first parameter of the function call, not the first parameter of the shell script.

Circular statement

shell scripts contain loops of types while, util, and for.

While loop means to loop when the judgment condition is true, while util loop means to terminate the loop when the judgment condition is true.

while [Conditional judgment] //The loop continues when the condition is true do xxxx //statements done util [Conditional judgment] //If the condition is false, the loop continues do xxxx //statements done

In addition, the shell supports two syntax for statement loops, as shown below:

for var in con1 con2 con3 ... // $var Take successively con1 ... con3 Cycle, in Followed by iteratable objects do xxxx //statements done for (( Initial value; Limit value; Execution step )) do xxxx //statements done

Parameter use

In the shell script, the parameters passed to the script are accessed by default through the specified character combination. The more common accessors are as follows. For more examples of using special characters in the shell, please refer to   Advanced Bash-Scripting Guide - Chapter 3. Special Characters

$0 //file name $1-$9 //The 1st to 9th parameters of the script $@ //All parameters, independent all parameters $* //All parameters are separated by a separator character. The separator is a space by default, as shown in "$1 $2 $3" $# //Number of parameters $? //Previous command return value $$ //The process number of executing the current script !! //The common usage scenario of the last complete command is when the previous command fails due to permission problems sudo !! $_ //The last parameter of the previous command, in shell Can be used in Esc + . get

In addition, in the shell script, you can skip the first n parameters in the form of shift n, and the corresponding access form to the parameters ($X number in $x) will change.

Commands commonly used in shell scripts

read

The read command is used to read input from the keyboard. Useful parameters are as follows. Multiple parameters can be mixed. Read is bash's own command. You can view the command help through read --help.

read -t time val1 //wait for time Time, save the user input to the variable val1 read -p description val2 //output description And wait for user input and store it in val2 read -s //User input is not displayed on the terminal read var1 var2 <<< "$variable" //take varible The value in the variable was read var1 and var2 In, use $IFS The value in is used as a delimiter and can be modified by yourself

printf

For simple formatted output, the use method is similar to the formatted output in C/C + +.

printf "path name %s" $PATH //Available formatters include %s character string,%d Numbers,%f Floating point number, etc

declare

declare is used to set variables and modify variable properties.

declare [options] variable[=value] -a #Declare a variable as an array array -i #When a variable is declared as an integer, the type is then evaluated instead of being treated as a string -r #Declare a variable as a read-only type -x #Set the variable export as a global variable #Set the values of the above parameters "-" Replace with "+" You can close the corresponding properties

Other

Users can set the default value of variables in a variety of ways to facilitate output. That is, when the variable has not been set before, its default value is used.

echo $ //By: = set default echo $ //By = - set default

Reference:

1. man page

2. Linux Shell Scripting Tutorial - varible

29 November 2021, 22:56 | Views: 2479

Add new comment

For adding a comment, please log in
or create account

0 comments