Shell Command and process control, Shell command Process Control

Source: Internet
Author: User

Shell Command and process control, Shell command Process Control

Shell commands 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 files, for example, search by file name
Find.-name filename-print
Tee: outputs data to the standard output device (screen) and files such as: somecommand | tee outfile
Head file: prints the first few lines of a text file.
Tail file: number of rows at the end of a text file
Tail-f Log File
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
# Querying processes with redis. sh
Ps-ef | grep redis. sh

####*
Sed: Sed is a basic search replacement program. You can read text from standard input (such as command pipeline) and output the results to 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/LinuxFocus/'> newtext. file
####*
Awk: awk is used to extract fields from text files. By default, the field delimiter is a space. You can use-F to specify other separators.
Awk-F, '{print $2}' test_awk.txt
Here we use it as a field delimiter and print the second field at the same time.
 
 
2) concept: pipelines and redirection
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.
-- YEAR = 'expr substr "$ {d1}" 1 4'
-- YYYYMM =$ {yyyymmdd: 0: 6}
-- Cat test_awk.txt | awk-F "=" '{print $2 }';
-- D2 = 'echo "$ YEAR $ MONTH $ DAY" | awk '{if (length ($2) = 1) $2 = 0 $2; if (length ($3) = 1) $3 = 0 $3; printf "% s", $1, $2, $3 }''
-- DAY = 'echo \ 'cal $ MONTH $ YEAR \ '| tail-n1 | awk' {print $ NF }''

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'
3) Process Control

#####
If the expression "if" is true, the part after then is executed:
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 "Check parameter 1"
Fi

In most cases, you can use test commands to test the conditions. For example, you can compare strings, determine whether a file exists, and whether the file is readable...

#####
Case expression
Case... in
...) Do ...;;
Esac

Case $1 in
1)
Echo "1"
;;
2)
Echo "2"
;;
*)
Echo $1
Esac

#####
For expression
For var in...; do
....
Done

For (variable = initial value; condition judgment; variable change); do
....
Done

--- Nohup sh 1.sh &
V_date = '2014 20150102 20150103 20150104'
For I in $ v_date
Do
Sh xx/ss. sh $ I
Done


V_date = '2014 20150102 20150103 20150104'
For I in $ v_date
Do
Case $ I in
20150102)
Echo & quot; 20150102 & quot"
;;
Op)
Echo "2"
;;
Esac
Done


I = 5
A = 10
For (I = 0; I <5; I ++ ));
Do
Echo $ I
Echo $
A = 'expr $ a-1'
Done

####
While expression
While ...;
Do
...
Done

I = 0
While [$ I-lt 10]; do
Echo $ I
Let I = I + 1
Done

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.