当前位置:网站首页>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
边栏推荐
- Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
- VLAN part of switching technology
- Recursion and divide and conquer strategy
- Sword finger offer 31 Stack push in and pop-up sequence
- MongoDB数据日期显示相差8小时 原因和解决方案
- Learning XML DOM -- a typical model for parsing XML documents
- [Galaxy Kirin V10] [server] grub default password
- system design
- Pod management
- Lavel document reading notes -how to use @auth and @guest directives in lavel
猜你喜欢

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

RHCE - day one

基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1

Huge number multiplication (C language)

The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago

Latex error: missing delimiter (. Inserted) {\xi \left( {p,{p_q}} \right)} \right|}}

uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示

Quick sort (C language)
![[Galaxy Kirin V10] [server] failed to start the network](/img/0f/6d2f321da85bd7437d2b86547bd8b4.jpg)
[Galaxy Kirin V10] [server] failed to start the network

Rhcsa day 10 operation
随机推荐
Introduction to extensible system architecture
Error C4996 ‘WSAAsyncSelect‘: Use WSAEventSelect() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARN
如果不知道這4種緩存模式,敢說懂緩存嗎?
PHP code audit 3 - system reload vulnerability
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
leetcode842. Split the array into Fibonacci sequences
Batch distribution of SSH keys and batch execution of ansible
Dichotomy search (C language)
BGP advanced experiment
leetcode1-3
[Galaxy Kirin V10] [server] grub default password
Differences among opencv versions
Rhcsa operation
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
uniapp---初步使用websocket(长链接实现)
Reprint: summation formula of proportional series and its derivation process
Latex learning insertion number - list of filled dots, bars, numbers
Architecture introduction
RHCE - day one
How to use diff and patch to update the source code