当前位置:网站首页>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
边栏推荐
- system design
- Use C to extract all text in PDF files (support.Net core)
- Es entry series - 6 document relevance and sorting
- Snake (C language)
- Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
- BGP ---- border gateway routing protocol ----- basic experiment
- Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
- [200 opencv routines] 218 Multi line italic text watermark
- Communication layer of csframework
- Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
猜你喜欢
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
Rhcsa12
Time complexity and space complexity
Learning XML DOM -- a typical model for parsing XML documents
Linked list operation can never change without its roots
How do microservices aggregate API documents? This wave of show~
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
Introduction to extensible system architecture
Occasional pit compiled by idea
VLAN part of switching technology
随机推荐
How to quickly parse XML documents through C (in fact, other languages also have corresponding interfaces or libraries to call)
Lavel document reading notes -how to use @auth and @guest directives in lavel
Talk about scalability
/*The rewriter outputs the contents of the IA array. It is required that the type defined by typedef cannot be used in the outer loop*/
【Day1】 deep-learning-basics
Rhcsa12
Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
[machine] [server] Taishan 200
/*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*/
原生div具有编辑能力
Latex learning insertion number - list of filled dots, bars, numbers
Map container
The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server
Dynamic memory management
Velodyne configuration command
PHP code audit 3 - system reload vulnerability
Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
Uniapp--- initial use of websocket (long link implementation)
Number of relationship models