Document directory
- 1. Use of shell Array
- 2. awk Array Operations
- 3. shell parameter problems
- 4. How to output single quotes in awk
- 5. Methods for connecting awk and shell strings
1. Use of shell Array
Value assignment: All = (6 5 4 3 1 8 9 10) # An array is represented by a pair of parentheses. The array elements are separated by spaces.
Length: num =$ {# A [*] }#$ {# A [* or @]}
Element: Member =$ {A [2]} # use $ {array name [subscript]} To subscript from 0: * or @ to get the entire array content
Print all elements: Echo $ {A [*]} #
Delete an element: unset A [3] # Delete the fourth element
Replace an element: a = ($ {A [*]/3/125}) # locate the element with a value of 3 and replace it with 125.
Split: Echo $ {A [@]: 3: 2} # print the two elements starting from the fourth element
2. awk Array Operations
<1> Create an array
Array [Index] = value: array name array, subscript index, and corresponding value.
<2> Read array values
{For (item in array) print array [item]} # The output order is random.
{For (I = 1; I <= Len; I ++) print array [I]} # Len is the length of the array
<4> delete an array or array element: Use the delete function.
Delete array # Delete the entire array
Delete array [item] # delete an array element (item)
<5> multidimensional array
???
3. shell parameter problems
$ # Number of parameters
$ * Or $ @ All Parameters
$1 $2: represents the first and second parameters (the number of parameters starting from 1)
Get the last parameter: $ {! #}
Obtain a parameter through nesting:
A = "cd"
Cd = "hellofroma"
Echo $ {! A} # "hellofroma" will be printed"
4. How to output single quotes in awk
Recently, when I used awk to spell an SQL statement, I encountered the problem that single quotes cannot be output.
It's confusing!
How does one output single quotes in awk? How does one print out single quotes in awk? How does Shell Process single quotes?
That is, the single quotation marks in shell processing.
Solution
Awk '{print "'\''"}'
Enclose the escaped single quotes with two single quotes so that they are not executed
The reason is: the three special characters enclosed in double quotation marks are not ignored: $, \, ', that is, double quotation marks will explain the special meaning of the string, while single quotation marks directly use string output.
So how does shell handle special characters?
1. Single quotes ('')
If we want to search for Susan Goldberg, we cannot directly use the grep Susan Goldberg phonebook command. grep regards Goldberg and phonebook as files to be searched.
Howard @ 0 [script] $ grep 'Susan gold' phonebook
Susan Goldberg 403-212-4921
When the shell encounters the first single quotation mark, it ignores all the special characters that follow it until the right quotation mark.
2. Double quotation marks ("")
Double quotation marks are similar to single quotes. The difference is that they are not so strict. Single quotes tell shell to ignore all special characters, while double quotes only need to ignore the majority. Specifically, three special characters enclosed in double quotes are not ignored: $ ,\,', double quotation marks explain the special meaning of strings, while single quotation marks directly use strings. if you use double quotation marks to assign a string to a variable and feed it back, it is actually no different from the direct feedback variable. Double quotation marks are often used to query strings containing spaces.
Howard @ 0 [script] $ x = *
Howard @ 0 [script] $ echo $ x
Hello. Sh menus. Sh Misc. Sh phonebook tshift. Sh
Howard @ 0 [script] $ echo '$ x'
$ X
Howard @ 0 [script] $ echo "$ X"
*
In this example, we can see the difference between no quotation marks, single quotation marks, and double quotation marks. In the last case, double quotation marks Tell Shell to replace the variable name in the quotation marks, so shell replaces $ X with * because the double quotation marks do not replace the file name, therefore, * is passed to echo as the value to be displayed.
For the first case, it should be further explained that shell does not replace the file name when assigning values to variables (this can also be seen in the third case). The exact sequence of each step is as follows:
Shell scans the command line and sets the value of X to asterisk *;
Shell scans the command line again, encounters the asterisk *, replaces it with the file list in the current directory;
Shell starts to execute the echo command and passes the file list as a parameter to Echo.
The order of value assignment is very important: Shell replaces the variable first, then the file name, and finally processes this line as a parameter.
3. Reverse quotation marks ('')
Command replacement means that shell can insert the standard output of a command to any location in a command line. There are two methods in shell to replace the command: Use reverse quotation marks or $ (...) for shell commands (...) structure, where $ (...) the format is supported by the POSIX standard and also facilitates nesting.
Howard @ 0 [script] $ echo the date and time is 'date'
The date and time is three June 15 06:10:35 CST 2005
Howard @ 0 [script] $ echo your current working directory is $ (PWD)
Your current working directory is/home/Howard/script.
4. backslash-escaped (\)
The backslash is generally used as an escape character or escape character. In Linux, if ECHO is used to enable the escape character, the-e option must be used and double quotation marks must be used for escape characters.
Echo-e "\ n"
Another function of the backslash is that when the backslash is used for the last character of a line, shell uses the backslash at the end of the line as the continuation line, this structure is often used when several lines of long commands are input.
Transfer: http://www.cnblogs.com/rootq/articles/1417138.html
5. Methods for connecting awk and shell strings
Awk:
Http://www.cnblogs.com/chengmo/archive/2010/10/09/1846639.html
Shell:
Http://www.cnblogs.com/wangkangluo1/archive/2012/06/13/2548636.html