当前位置:网站首页>shell awk
shell awk
2022-07-04 10:35:00 【Zhuge Bo】
shell
What is? shell Script :
1. When the command is not executed on the command line , But when executing from a file , The file is shell Script . shell Scripts are plain text files , It consists of process control logic and commands . shell Scripts usually use .sh Suffixed name , But it's not necessary .
2.Shell It's an interpretive programming language , No compilation required , It is also executed by line .
3.Shell Scripts are interpreted and executed by the interpreter , Common interpreters are : bash dash ash ksh sh etc. Be careful , Each of these Shell The interpreter is right shell The grammatical interpretation of is not exactly the same (sh and bash The parser is right ll The explanation is different ), But basically the same .
4. A script is a stack of commands 5. Scripts can take parameters , There are variables, too , There can also be process control and even functions
Position parameter variable :
1. Positional parameters are a special set of built-in variables , Each string separated by a space following the script name $1 It means the first one 1 Parameter values ,……,$9 It means the first one 9 Parameter values ${10} It means the first one 10 Parameter values , ${11} It means the first one 11 Parameter values
2. Role of position parameters from shell command / Script The command line of accepts parameters Calling shell Pass parameters to function
Special variables :
1. Related to command line parameters
$* Treat all position parameters as a string ( In space ) .String str= "1 2 3 4"
[email protected] Treat each position parameter as a separate string ( In space ). "1" "2 " "3" "4"
“$*” Treat all position parameters as a string ( With $IFS interval ).
“[email protected]” Treat each position parameter as a separate string ( In space ) . $0 Entered on the command line Shell The program name .
$# Indicates the number of parameters on the command line .
2. Related to process status
$? Represents the return value of the last command
$$ The process number of the current process $! Displays the of the last job running in the background PID
$_ The last parameter of a command or script executed before Examples of location parameters and special variables , Execute the script
Improve the package backup function :
establish sh file touch pack.sh
#!/bin/bash Declaration parsing , Used to parse commands
# pack , And handle exceptions , When the backup package succeeds , Delete the original directory
# 2 Represents the execution of the previous name if there is an error && When the previous command is executed successfully , Execute the following command
tar -czvf $2 $1 2>error.log && rm -rf $1 || cat error
Then exit the editor , Modify user rights
chmod u+x pack.sh
Carry out orders :
./pack.sh Compressed files Compressed prefix name
ls Check whether to compress and delete
sign out / Return to status :
1.$?: Returns the status of the last statement or script execution 0: success 1-255: You don't succeed
2.exit The command is used to exit the script or the current Shelln It is a slave. 0 To 255 The integer of 0 Indicates a successful exit , Non zero means that some failure return value is encountered Saved in state variables $? in
Common return status codes :
0: Execute correctly
1: General error
126: Command or script does not have execution permission
127: The order was not found
2 : Parameter does not exist
Stream redirection :
The standard input : Code 0, Use < perhaps <<
standard output : Code 1, Use > perhaps >>
Error output : Code 2, Use > perhaps > >
> Express write in >> Indicates append
< Express Read in << Indicates end of read in
cat<b.log>c.log Read b.log Document content to c.log in
cat <<EOF >>c.log Read EOF The content between is appended to c.log
It is generally used for the combination of two files
Continuous execution of commands
&&: Execute the following command after success
||: Execute the following code after the previous code fails
if else if elseif for while
Greater than -gt (greater than)
Less than -lt (less than)
Greater than or equal to -ge (greater than or equal)
Less than or equal to -le (less than or equal)
It's not equal -ne (not equal)
Common commands awk
awk brief introduction :
1.awk Is a very good data processing program , It tends to divide a line into multiple ‘ Field ’ To deal with it . similar String[] strArray = "a,b,c,d".split(","); strArray[0] strArray[3]
2. Data can come from standard inputs 、 One or more files 、 Output of other processes
3.awk From 1 Line by line scanning to the last line , And perform matching operations
4.awk The default operation of is to output matching lines, that is, print matching lines to standard output
awk grammar :
1.awk [ Options ] 'BEGIN{ sentence 1; sentence 2;...} END{ sentence 1; sentence 2;...} Pattern 1{ sentence 1; sentence 2;...} Pattern 2{ sentence 1; sentence 2;...} Pattern n...' [ file name ]
awk 'BEGIN{print " File start ";} END{print " End of file ";} {print " Executive line " NR}' tomcat.log
practice :
Write a script , The functional requirements are as follows :
Script runtime , You can enter multiple parameters
1, When the script enters parameters , The first parameter is 1 when , The second parameter is the path to package , The third parameter is the packed file name , Realize the function of dynamically packaging and backing up any directory and deleting the original files
2, When the script enters parameters , The first parameter is 2 when , The second parameter is the directory path to traverse , Traverse all files under the directory of dynamic input and output the results
3, When the script enters parameters , The first parameter is 3 when , The second parameter is a positive integer , Achieve summation 1 To the function of the sum of the positive integer parameters
4, When the script enters parameters , When the first parameter is other , Input error of output parameters
#!/bin/bash
if [ $1 -eq 1 ] ; then
tar -czvf $3.tar.gz $2 2>error.log && rm -rf $2
elif [ $1 -eq 2 ] ; then
ls -l $2
elif [ $1 -eq 3 ] ; then
declare -i sum=0;
declare -i j=1;
while (( j<= 2 ) ) d o l e t s u m + = j ; l e t j + + ; d o n e e c h o " total and yes : 2 )) do let sum+=j; let j++; done echo " The sum is : 2))doletsum+=j;letj++;doneecho" total and yes :sum "
else
echo 'error ’
fi
边栏推荐
- 六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
- uniapp---初步使用websocket(长链接实现)
- DDL language of MySQL database: create, modify alter, delete drop of databases and tables
- 基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
- 2020-03-28
- Native div has editing ability
- uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
- Today's sleep quality record 78 points
- Dynamic memory management
- Two way process republication + routing policy
猜你喜欢
Idea SSH channel configuration
Knapsack problem and 0-1 knapsack problem
Rhcsa12
Network connection (II) three handshakes, four waves, socket essence, packaging of network packets, TCP header, IP header, ACK confirmation, sliding window, results of network packets, working mode of
leetcode1-3
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
What is an excellent architect in my heart?
leetcode842. Split the array into Fibonacci sequences
Static comprehensive experiment ---hcip1
Jianzhi offer 04 (implemented in C language)
随机推荐
Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
Es entry series - 6 document relevance and sorting
[Galaxy Kirin V10] [desktop] printer
Write a program that uses pointers to set all elements of an int array to 4.18: 0.
使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
Four characteristics and isolation levels of database transactions
Evolution from monomer architecture to microservice architecture
Work order management system OTRs
Ruby time format conversion strftime MS matching format
Sword finger offer 31 Stack push in and pop-up sequence
How to quickly parse XML documents through C (in fact, other languages also have corresponding interfaces or libraries to call)
[Galaxy Kirin V10] [server] grub default password
Rhcsa day 10 operation
Recursion and divide and conquer strategy
Vs201 solution to failure to open source file HPP (or link library file)
What is devsecops? Definitions, processes, frameworks and best practices for 2022
For programmers, if it hurts the most...
The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server
From programmers to large-scale distributed architects, where are you (2)