当前位置:网站首页>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
边栏推荐
- The bamboo shadow sweeps the steps, the dust does not move, and the moon passes through the marsh without trace -- in-depth understanding of the pointer
- Online troubleshooting
- Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
- DCL statement of MySQL Foundation
- Container cloud notes
- [Galaxy Kirin V10] [server] NUMA Technology
- 按键精灵跑商学习-商品数量、价格提醒、判断背包
- uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
- Write a program to judge whether the two arrays are equal, and then write a similar program to compare the two vectors.
- 183 sets of free resume templates to help everyone find a good job
猜你喜欢
system design
20 minutes to learn what XML is_ XML learning notes_ What is an XML file_ Basic grammatical rules_ How to parse
If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display
Summary of several job scheduling problems
[machine] [server] Taishan 200
Seven examples to understand the storage rules of shaped data on each bit
Evolution from monomer architecture to microservice architecture
Two way process republication + routing policy
Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
[Galaxy Kirin V10] [desktop and server] FRP intranet penetration
随机推荐
[Galaxy Kirin V10] [server] soft RAID configuration
Latex error: missing delimiter (. Inserted) {\xi \left( {p,{p_q}} \right)} \right|}}
What is an excellent architect in my heart?
Reprint: summation formula of proportional series and its derivation process
leetcode1229. Schedule the meeting
system design
/*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*/
Collection of practical string functions
Lavel document reading notes -how to use @auth and @guest directives in lavel
Add t more space to your computer (no need to add hard disk)
Talk about scalability
原生div具有编辑能力
Hlk-w801wifi connection
Leetcode48. Rotate image
Idea SSH channel configuration
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
Latex learning insertion number - list of filled dots, bars, numbers
【Day1】 deep-learning-basics
RHCE day 3
MongoDB数据日期显示相差8小时 原因和解决方案