当前位置:网站首页>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
边栏推荐
- Use C to extract all text in PDF files (support.Net core)
- OSPF comprehensive experiment
- Two way process republication + routing policy
- Reprint: summation formula of proportional series and its derivation process
- Pod management
- Error C4996 ‘WSAAsyncSelect‘: Use WSAEventSelect() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARN
- 今日睡眠质量记录78分
- Const's constant member function after the function; Form, characteristics and use of inline function
- Network connection (III) functions and similarities and differences of hubs, switches and routers, routing tables and tables in switches, why do you need address translation and packet filtering?
- Leetcode48. Rotate image
猜你喜欢
How do microservices aggregate API documents? This wave of show~
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
Three schemes of ZK double machine room
Work order management system OTRs
【Day1】 deep-learning-basics
20 minutes to learn what XML is_ XML learning notes_ What is an XML file_ Basic grammatical rules_ How to parse
Number of relationship models
Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
Remove linked list elements
随机推荐
如果不知道這4種緩存模式,敢說懂緩存嗎?
PHP programming language (1) - operators
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
PHP代码审计3—系统重装漏洞
leetcode842. Split the array into Fibonacci sequences
Evolution from monomer architecture to microservice architecture
/*Write a function to open the file for input, read the contents of the file into the vector container of string class 8.9: type, and store each line as an element of the container object*/
Introduction to extensible system architecture
Reasons and solutions for the 8-hour difference in mongodb data date display
Write a program to define an array with 10 int elements, and take its position in the array as the initial value of each element.
Write a program to judge whether the two arrays are equal, and then write a similar program to compare the two vectors.
Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
Batch distribution of SSH keys and batch execution of ansible
Latex learning insertion number - list of filled dots, bars, numbers
When I forget how to write SQL, I
Uniapp--- initial use of websocket (long link implementation)
对于程序员来说,伤害力度最大的话。。。
Talk about scalability
[untitled]
OSPF comprehensive experiment