14th class hour Functions
14.1 create and use functions
Format: Name () {list ;}
An important feature of Shell functions is that they can be used to replace binary executable files or shell built-in commands with the same name.
For example:
CD () {chdir $ {1:-$ home}; PSI = "'pwd' $"; export PSI ;}
This statement uses a function instead of the CD command, which changes the Directory and sets the main shell prompt $ PSI to include the current directory.
Function variables in shell scripts do not work with other scripting languages. For example, in PHP, when the script execution exceeds the function range, the internal variables
Will disappear, so there is no problem if you do not delete it.
In Shell, variables are identified as global variables, so they must be managed with caution.
For example:
$ Test () {test = 123 ;}
$ Test
$ Echo $ test # output 123
15th Lesson text filter
Shell scripts are often called to operate and format the output of the executed command.
1) Head
2) tail
3) grep
4) sort
5) uniq
6) tr
15.1.1 head and tail commands
Format: head [-n lines] files
If no-n lines exist, the first 10 lines of the standard are displayed.
15.1.2 tail command
Format: tail [-n lines] files
If no-n lines exist, the last 10 lines of the standard are displayed.
-F option, used to view the file that is being written.
15.2 use grep
The grep command allows you to find a line containing a special word or phrase in the file.
15.2.1 search for words
Format:
Grep Word file
Here, the file specifies the files in which the word is to be searched. The grep command displays each line of the word in the file.
When multiple files are specified, the grep command adds the file name containing the row before the output line.
-I option, case insensitive.
15.2.2 read from stdin (standard input)
When no file is specified, grep looks for a match from the stdin input line, which makes it well connected to the pipeline.
Example: $ who | grep Root
-V option to list rows that do not match the condition
15.2.3 row number
Option-n. The result list returns the row number.
$ Grep-N pipe ch15.doc
Return format: file: line number: Line
15.2.4 only list file names
Sometimes, you only need to return a file name containing a word.
You can use the-L option.
Example: $ grep-l PHP *
Return Value: index. php