Linux Shell programming knowledge point summary (to be continued)

Source: Internet
Author: User

2011-04-23 wcdj

 

The method for learning shell program design is: Hands-on + man command

The following scripts are all tested in cygwin.
$/Bin/Bash -- version
GNU bash, version 2.05b.0 (1)-release (i686-pc-cygwin)
Copyright (c) 2002 Free Software Foundation, Inc.

The shell script programTwo methods
:
[1] You can enter a series of commands for shell to execute them interactively.
[2] Save these commands to a file and call the file as a program.

Common bash commands
Directory
:
(1) read command -- the variable content is determined by the user
(2) Redirect output and input
(3) pipelines |
(4) find command -- search for files in the system
(5) grep command -- search for strings in the file
(6)-exec and-OK commands
(7) arithmetic Extension
(8) parameter Extension
(9) script tracking and debugging

 

 

 

(1) read command -- the variable content is determined by the user


Example
:
#! /Bin/bash
# Program:
# This program is used to show the usage of [read] command.
# History:
#2011/04/23 wcdj first release
Echo-e "this program is used to show the usage of [read] command ."
Read-P "Please input your first name:" firstname
Read-P "Please input your last name:" lastname
Echo-e "/Nyour full name is: $ firstname $ lastname"

(2) Redirect output and input


Redirect output> and>
File descriptor 0: represents the standard input of a program
File descriptor 1: represents the standard output of a program
File descriptor 2: indicates the standard error output of a program.
Redirect input <

(3) pipelines |


You can use the pipeline operator | to connect processes. Linux, unlike MS-DOS, processes connected through pipelines in Linux can run simultaneously and automatically coordinate as data streams pass between them.
Example 1
: Use the sort command to sort the output of the ps command.
PS | sort> pssort. Out # Method 1: Save the result to the pssort. Out file.
PS | sort | more # Method 2: display the output result by page on the screen

Example 2
: View the names of all processes running in the system, excluding the shell itself.
PS-XO comm | sort | uniq | grep-V sh | more
In cygwin, PS does not have the-XO option.

Note:
:
(1) There is no limit on the number of processes allowed to connect.
(2) If a series of commands need to be executed, the corresponding output files are created or written at the same time as the commands are created, therefore, you must never reuse the same file name in the Command stream. For example, the following command:
Cat mydata.txt | sort | uniq> mydata.txt
Error: An empty file is generated at the end, because the file content is overwritten before reading the file mydata.txt.

(4) find command -- search for files in the system


The complete syntax format of the find command is as follows:
Find [path] [Options] [tests] [actions]

Example 1
: Find the file named test on the local machine. To ensure that you have the permission to search the entire machine, run this command as a root user.
# Find/home/wcdj-name test-print
Output result:
/Home/wcdj/Linux/openssl-SNAP-20091203/demos/tunala/test. Sh
/Home/wcdj/bash/test. Sh
After running the command, the hard disk will rotate wildly, which takes a long time.

Example 2
: Search for a new file in the current directory. If the file passes the test, check whether the file is a normal file. Finally, check the file.
Find./(-name "*"-or-newer file/)-type F-print
Note:
:
(1) use parentheses to force testing and operator priority. Since parentheses have special meanings for shell, they must also be referenced using backlash.
(2) The-name pattern file name (excluding the path name) matches the pattern provided. To ensure that pattern is passed to the find command rather than the shell, pattern must always be enclosed in quotation marks.

(5) grep command -- search for strings in the file


The complete syntax format of the grep command is as follows:
Grep [Options] pattern [files]

Example 1
: Search for files containing the POSIX string from a large number of C language source files and output file names that meet the criteria.
$ More 'grep-l POSIX * '# Method 1: Use backquotes
$ More $ (grep-l POSIX *) # Method 2: use $ (...) to get the output of the sub-process
$ Grep-l POSIX * | more # method 3: Use pipelines |
$ Grep-l POSIX *-exec more {}/; # Method 4: Use-exec (it can be displayed but some errors will be output)

Example 2
:
Grep wcdj words.txt # search for the string wcdj in the words.txt file, and then output matched rows.
Grep-C wcdj words.txt words2.txt # calculate the number of matched rows in two different files. In this case, the file name is output
Grep-C-V wcdj words.txt words2.txt # use the-V option to reverse the search and calculate the number of unmatched rows in the two files

Example 3
: Use regular expressions for more complex matching
# [1] search for rows ending with the letter E
Grep e $ words.txt
# [2] search for words ending with Letter
Grep A [[: blank:] words.txt
# [3] search for words with three letters starting with th
Grep th. [[: blank:] words.txt
# [4] use the extended grep mode to search for words with only 10 characters long, all consisting of lowercase letters
Grep-E [A-Z]/{10/} words.txt

(6)-exec and-OK commands


The-exec and-OK commands use subsequent parameters on the command line as part of their parameters until they are terminated by the/; sequence. In fact, the-exec and-OK commands execute an embedded command, so the embedded command must end with an escape semicolon, this allows the find command to determine when it can continue searching for its own command line options. The magic string {} is a special type parameter of the-exec or-OK command. It will be replaced by the complete path of the current file.
Example
:
Find.-Name file-type F-exec LS-l {}/;
Find.-newer file-type F-exec LS-l {}/;

(7) arithmetic Extension


The expr command can process some simple arithmetic commands, but it is quite slow to execute because it needs to call a new shell to process the expr command.
A better way to update is to use $ (...) to extend the value of the calculated expression.
Note:
:
$ (...) Is different from $ (...). Two pairs of parentheses are used for arithmetic replacement, and a pair of parentheses are used for command execution and output.
Example
: Print the output 0 ~ 9
#! /Bin/bash
# Program:
# $ ((...))
# History:
#2011/04/23 wcdj first release
X = 0;
While ["$ X"-ne 10]
Do
Echo $ x
X = $ ($ x + 1) # x = $ (expr $ x + 1)
Done
Exit 0

(8) parameter Extension


Put the variable in braces {} to replace the parameter value with a string.
Example 1
:
#! /Bin/bash
# Program:
# Parameter Extension
# History:
#2011/04/23 wcdj first release
For I in 1 2
Do
Echo $ {I} _ TMP
Done

Example 2
: Use the cjpeg program to create a corresponding jpge file for each GIF file in the current directory
#! /Bin/bash
# Program:
# Parameter Extension
# History:
#2011/04/23 wcdj first release
For Image in *. gif
Do
Cjpeg $ image >$ (image % GIF) JPG
Done

Note:
:
There are many ways to use parameter extension. For more information, see p.60 in [2.

(9) script tracking and debugging

Sh-[nvx] scripts. Sh
Parameter meaning:
-N: do not execute the script. Only query syntax problems. If there is no syntax problem, no information is displayed.
-V: Before executing the script, output the script content to the screen.
-X: displays the script content on the screen. This is a useful parameter. Using-X is a good method for tracking scripts. It can list all program segments that are executed before execution. If it is a program section, A + symbol is added at the beginning of the output, indicates that it is a program code, and the actual output is related to the standard output.

Example
: The name of the script file below is test. Sh.
#! /Bin/bash
# Program:
# Parameter Extension
# History:
#2011/04/23 wcdj first release
For I in 1 2
Do
Echo $ {I} _ TMP
Done

Debug this script:
Sh-X test. Sh
The following content is displayed:
+ Echo upload TMP
1_tmp
+ Echo 2_tmp
2_tmp

To be continued ......

 

Reference
:
[1] Linux laruence's basic learning of Private food (version 2)
[2] Linux programming (version 4th)

 

 

Related Article

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.