The basic course of Linux Shell script describes the beginning, comments, variables, and environment variables of the basic syntax. Here we will introduce the first part of the shell command and control process, three types of commands can be used in shell scripts, but the control process should be put in the next lecture.
1.1.5 shell command and Process Control
Three types of commands can be used in shell scripts:
1) Unix command:
Although any Unix command can be used in shell scripts, some more common commands are used. These commands are usually used for file and text operations.
Common command syntax and functions
Echo "some text": print the text on the screen
Ls: file list
WC-l filewc-W filewc-C file: calculate the number of file lines. Calculate the number of words in the file. Calculate the number of characters in the file.
CP sourcefile destfile: file copy
MV oldname newname: rename a file or move a file
RM file: delete an object
Grep 'pattern' file: searches for strings in a file, for example, grep 'searchstring' file.txt.
Cut-B colnum file: specify the content range of the file to be displayed, and output them to the standard output device, for example: output 5th to 9th characters in each line cut-b5-9 file.txt do not confuse with Cat command, this is two completely different commands
Cat file.txt: output file content to the standard output device (screen)
File somefile: get the file type
Read var: prompt the user to input and assign the input value to the variable.
Sort file.txt: sorts the rows in the file.txt file.
Uniq: Delete the columns in a text file, for example, sort file.txt | uniq
Expr: perform mathematical operations example: Add 2 and 3 expr 2 "+" 3
Find: search for a file. For example, search for find.-Name filename-print based on the file name.
Tee: outputs data to the standard output device (screen) and files such as: somecommand | tee OUTFILE
Basename file: returns a file name that does not contain a path, for example, basename/bin/tux.
Dirname file: the path of the returned file. For example, dirname/bin/tux will return/bin.
Head file: prints the first few lines of a text file.
Tail file: number of rows at the end of a text file
Sed: SED is a basic search replacement.Program. You can read text from standard input (such as command pipeline) and
The result is output to the standard output (screen ). This command uses a regular expression (see references) for search. Do not confuse with wildcards in shell. For example, replace linuxfocus with linuxfocus: CAT text. File | SED's/linuxfocus/'> newtext. fileawk: awk to extract fields from text files. By default, the field delimiter is a space. You can use-F to specify other separators.
Cat file.txt | awk-F, '{print $1 "," $3}', which is used here as a field delimiter and prints both the First and Third fields. If the file contains the following content: Adam Bor, 34, indiakerry Miller, 22, and USA, the output result is Adam Bor, indiakerry Miller, USA.
2) concept: pipelines, redirection, and backtick
These are not system commands, but they are really important.
The pipeline (|) uses the output of a command as the input of another command.
Grep "hello" file.txt | WC-l
Search for a row containing "hello" in file.txt and calculate the number of rows.
Here, the grep command output serves as the WC command input. Of course, you can use multiple commands.
Redirection: output the command result to a file instead of a standard output (screen ).
> Write the file and overwrite the old file
> Add it to the end of the file to retain the content of the old file.
Backlash
You can use a backslash to output a command as a command line parameter of another command.
Command:
Find.-mtime-1-type F-print
Used to search for files modified in the past 24 hours (-mtime-2 indicates the past 48 hours. If you want to pack all the searched files, you can use the following script:
#! /Bin/sh
# The ticks are backticks (') not normal quotes ('):
Tar-zcvf lastmod.tar.gz 'Find.-mtime-1-type F-print'
Next, let's talk about the basic learning of Linux shell scripts. We will continue to talk about the control process.