[Consolidate shell Basics] shell Basics
Address of this Article
Sharing outline:
1. Functions in shell
2. array in shell
3. Variables in shell
4. Operators in shell
5. Linux commands
1. Functions in shell
1.1) [define a shell function (define function )]
[ function ] funname [()] { action; [return int;] }
Note:
- 1. It can be defined with function fun () or fun () without any parameters.
- 2. If the parameter is returned, the value "+: return" is displayed. If no value is added, the result of the last command is run as the return value. Return followed by value n (0-255
Instance (testfun1.sh ):
1 #! /Bin/sh 2 3 fSum 3 2; 4 function fSum () 5 {6 echo $1, $2; 7 return $($1 + $2 )); 8} 9 fSum 5 7; 10 total =$ (fSum 3 2); 11 echo $ total, $ ?; 12 13 sh testfun1.sh14 testfun1.sh: line 3: fSum: command not found15 5,716 3,217 118 5Testfun1.sh
From the example above, we can draw several conclusions:
- 1. You must declare the function before calling the function. The shell script runs on a line-by-line basis. It does not pre-compile like other languages. You must declare the function before using the function.
- 2. total = $ (fSum 3 2); through this call method, we know that in shell single brackets, it can be a command statement. Therefore, we can think of a function in shell as a new command, which is a command. Therefore, each input parameter is directly separated by a space. Once, the command can get the parameter method through: $0... $ N. $0 indicates the function itself.
- 3. The function return value can only be passed through $? The system variable is obtained directly through =, and the result is a null value. In fact, according to the above understanding, we know that a function is a command. To obtain the return value of a command in shell, we need to use $? .
1.2) [function scope, variable scope]
Let's first look at an instance (testfun2.sh ):
1 #! /Bin/sh 2 3 echo $ (uname); 4 declare num = 1000; 5 6 uname () 7 {8 echo "test! "; 9 (num ++); 10 return 100; 11} 12 testvar () 13 {14 local num = 10; 15 (num ++ )); 16 echo $ num; 17 18} 19 20 uname; 21 echo $? 22 echo $ num; 23 testvar; 24 echo $ num; 25 26 sh testfun2.sh27 Linux28 test! 29 10030 100131 1132 1001Testfun2.sh
Let's analyze the above example and draw the following conclusions:
- 1. The definition function can be the same as the system command. It indicates that when a shell SEARCH Command is executed, it will first be searched in the defined location of the current shell file.
- 2. You need to obtain the function value through $? Obtain
- 3. If other types of function values need to be transferred, you can define the variable before calling the function (this is the global variable ). You can directly modify the value within the function, and then read the modified value after executing the function.
- 4. If you need to define your own variables, you can define the local variable = value in the function. In this case, the variable is an internal variable. Its modification will not affect the value of the same variable outside the function.
2. array in shell
2.1) [array definition]
Two methods
Arr = (1 2 3 4 5) # note that it is separated by spaces, not commas !!
And
arrayarray[0]="a"array[1]="b"array[2]="c"
2.2) [array traversal]
for var in ${ arr[@] };do echo $vardone
2.3) [Other arrays]
-- [Read array]
Valuen =$ {array_name [n]} # obtain a single element echo $ {array_name [@]} # obtain all elements # obtain the number of array elements length =$ {# array_name [@]} # Or length =$ {# array_name [*]} # obtain the length of a single element in the array. lengthn =$ {# array_name [n]}
3. Variables in shell
3.1) Definition
Your_name = "runoob.com"
-- No space is allowed.
-- You can assign values using expressions.
For file in 'ls/etc'
3.2) [Use variables]
your_name="qinjx"echo $your_nameecho ${your_name}
-- You can add {} or not
-- [Read-Only variable]ReadonlyMyUrl
-- [Delete variable] unset variable_name
3.3) [Shell string]
-- Single or double quotation marks are supported. variables are parsed using double quotation marks.
-- Get the string length
String = "abcd" echo $ {# string} # output 4
-- Truncate a string
String = "runoob is a great site" echo $ {string: 1: 4} # output unoo
-- Search for strings
String = "runoob is a great company" echo 'expr index "$ string" is '# output 8
4. Operators in shell
4.1) [basic operators]
Native bash does not support simple mathematical operations, but can be implemented using other commands, such as awk and expr, which are most commonly used.
#! /Bin/bashval = 'expr 2 + 2' echo "the sum of the two values: $ val"
4.2) [Relational operators]
Lt: litter
Gt: grater
Operator |
Description |
Example |
-Eq |
Returns true if the two numbers are equal. |
[$ A-eq $ B] returns false. |
-Ne |
Checks whether two numbers are equal. If they are not equal, true is returned. |
[$ A-ne $ B] returns true. |
-Gt |
Checks whether the number on the left is greater than the number on the right. If yes, returns true. |
[$ A-gt $ B] returns false. |
-Lt |
Checks whether the number on the left is smaller than the number on the right. If yes, returns true. |
[$ A-lt $ B] returns true. |
-Ge |
Checks whether the number on the left is greater than or equal to the number on the right. If yes, returns true. |
[$ A-ge $ B] returns false. |
-Le |
Checks whether the number on the left is less than or equal to the number on the right. If yes, returns true. |
[$ A-le $ B] returns true. |
4.3) [String operator]
-Z |
Checks whether the string length is 0. If it is 0, true is returned. |
[-Z $ a] returns false. |
-N |
Checks whether the string length is 0. If it is not 0, true is returned. |
[-N $ a] returns true. |
Str |
Checks whether the string is null. If it is not null, true is returned. |
[$ A] returns true. |
4.4) [file test operator]
The file test operator is used to detect various properties of Unix files.
Attribute detection is described as follows:
Operator |
Description |
Example |
-B file |
Checks whether the file is a block device file. If yes, returns true. |
[-B $ file] returns false. |
-C file |
Checks whether the file is a character device file. If yes, returns true. |
[-C $ file] returns false. |
-D file |
Checks whether the file is a directory. If yes, returns true. |
[-D $ file] returns false. |
-F file |
Checks whether a file is a common file (neither a directory nor a device file). If yes, returns true. |
[-F $ file] returns true. |
-G file |
Checks whether the SGID bit is set for the file. If yes, true is returned. |
[-G $ file] returns false. |
-K file |
Check whether Sticky Bit is set in the file. If yes, true is returned. |
[-K $ file] returns false. |
-P file |
Checks whether the file is a famous pipe. If yes, returns true. |
[-P $ file] returns false. |
-U file |
Check whether the SUID bit is set for the file. If yes, true is returned. |
[-U $ file] returns false. |
-R file |
Checks whether the file is readable. If yes, true is returned. |
[-R $ file] returns true. |
-W file |
Checks whether the file is writable. If yes, true is returned. |
[-W $ file] returns true. |
-X file |
Checks whether the file is executable. If yes, true is returned. |
[-X $ file] returns true. |
-S file |
Check whether the file is empty (whether the file size is greater than 0). If it is not empty, true is returned. |
[-S $ file] returns true. |
-E file |
Checks whether a file (including directories) exists. If yes, returns true. |
[-E $ file] returns true. |
5. Linux commands
5.1) [Some uncommon commands]
-- View the shell: cat/etc/shells available for the current release.
5.2) [regular expression]
Basic Regular Expression (BRE)
5.3) [awk command]
Http://blog.csdn.net/shanyongxu/article/details/46563997
5.4) [xargs command]
Http://blog.csdn.net/shanyongxu/article/details/46859829