Shell command and Process Control
There are three types of commands that can be used in shell scripts:
1) Unix command:
Although you can use any Unix command in a shell script, there are some relatively common commands. These commands are usually used for file and text operations.
Common command syntax and functions
echo "Some text": Print text content on the screen
LS: File list
Wc–l filewc-w filewc-c File: Count the number of words in the file count the number of characters in the file
CP sourcefile destfile: File copy
MV oldname newname: Renaming files or moving files
RM file: Deleting files
grep ' pattern ' file: Search for strings within a file such as: grep ' searchstring ' file.txt
Cut-b colnum File: Specifies the range of files to display and outputs them to a standard output device such as: Output from 5th to 9th characters per line cut-b5-9 file.txt never be confused with cat commands, this is two completely different commands
Cat file.txt: Output file contents to standard output device (screen)
File somefile: Get the files type
Read Var: Prompts the user for input and assigns the input to the variable
Sort file.txt: Rows in the File.txt file are
Uniq: Delete columns that appear in a text file such as: sort File.txt | Uniq
Expr: Perform mathematical operations Example:add 2 and 3expr 2 "+" 3
Find: Search for files such as: Search by file name
Find. -name Filename-print
Tee: Output data to standard output devices (screens) and files such as: Somecommand | Tee outfile
Head file: Print text file at the beginning of a few lines
Tail File: Print text file at the end of a few lines
Tail-f log files
Enconv conversion file encoding for example, to convert a GBK encoded file into UTF-8 encoding, the operation is as follows
Enconv-l zh_cn-x UTF-8 filename
# Query for processes with redis.sh
Ps-ef|grep redis.sh
#### *
Sed:sed is a basic find-and-replace program. You can read text from standard input, such as a command pipeline, and output the results to a standard output (screen).
the command is searched using a regular expression (see Reference). Do not confuse with wildcard characters in the shell. For example: Replace Linuxfocus with Linuxfocus:cat text.file | Sed ' s/linuxfocus/linuxfocus/' > Newtext.file
#### *
The Awk:awk is used to extract fields from a text file. By default, the field separator is a space, and you can use-f to specify additional delimiters.
Awk-f, ' {print $} ' Test_awk.txt
Here we use, as a field separator, to print the second field at the same time.
2) Concept: Pipeline, redirect
These are not system commands, but they are really important.
Pipe (|) The output of one command as input to another command.
####*
grep "Hello" file.txt | Wc-l
Searches for a row containing "Hello" in File.txt and calculates its number of rows.
Here the output of the grep command is used as input to the WC command. Of course you can use multiple commands.
Redirect: Outputs the result of the command to a file instead of the standard output (screen).
> Write files and overwrite old files
>> add to the end of the file, preserving the contents of the old file.
Anti-Short Slash
Use a backslash to make the output of one command a command-line argument for another command.
--year= ' expr substr ' ${d1} ' 1 4 '
--Yyyymm=${yyyymmdd:0:6}
--Cat Test_awk.txt |awk-f "=" ' {print $} ';
--d2= ' echo ' $YEAR $MONTH $DAY "|awk" {if (length ($) ==1) $2=0$2;if (Length ($) ==1) $3=0$3;printf "%s%s%s", $1,$2,$3} "
--day= ' Echo ' cal $MONTH $YEAR \ ' |tail-n1|awk ' {print $NF} '
Command:
Find. -mtime-1-type F-print
Used to find files that have been modified in the last 24 hours (-mtime–2 represents the last 48 hours). If you want to hit a package with all the files you find, you can use the following script:
#!/bin/sh
# The Ticks is Backticks (') not normal quotes ('):
TAR-ZCVF lastmod.tar.gz ' Find. -mtime-1-type F-print '
3) Process Control
#####
The "If" expression executes the following part if the condition is true:
If ....; Then
....
Elif ...; Then
....
Else
....
Fi
If [$1-eq 0];then
echo Parameter 1 "= 0"
elif [$1-lt 5];then
echo Parameter 1 "< 5"
elif [$1-ge 5];then
echo Parameter 1 ">= 5"
Else
echo "Please check parameter 1"
Fi
In most cases, you can use test commands to test the condition. For example, you can compare strings, determine whether the file exists and whether it is readable, etc...
#####
Case expression
Case ... in
...) Do ...;
Esac
Case $ in
1)
echo "1"
;;
2)
echo "2"
;;
*)
echo $
Esac
#####
for-expression
for Var in ...; Do
....
Done
for (variable = initial value; condition judgment; variable change);d o
....
Done
---nohup sh 1.sh &
V_date= ' 20150102 20150103 20150104 '
For I in $v _date
Do
SH xx/ss.sh $i
Done
V_date= ' 20150102 20150103 20150104 '
For I in $v _date
Do
Case $i in
20150102)
echo "20150102"
;;
Op
echo "2"
;;
Esac
Done
I=5
a=10
For ((i=0;i<5;i++));
Do
Echo $i
Echo $a
A= ' expr $a-1 '
Done
####
While expression
While ...;
Do
...
Done
I=0
While [$i-lt 10]; Do
Echo $i
Let I=i+1
Done
Shell command and Process Control