Shell script ( Namely Shell Script),Shell The script is similar to Windows/Dos
Next batch , That is to put all kinds of commands into a file in advance , A program file convenient for one-time execution , Mainly used in Linux
System operation and maintenance , Convenient for administrators to set or manage . But it's better than Windows More powerful batch processing , More efficient than programs written in other programming languages . Because it uses Linux/Unix
Order under , So I want to write the Shell script , Need to understand Linux/Unix command , as grep,sed,awk wait .

Shell First line of script

   generally speaking ,Shell The first line of the script specifies the interpreter to execute the script , as :
“#!” Sign reading “Sha-bang”, yes Shell Start of script , The path immediately following the interpreter , Here is /bin/bash, In addition, we may see
/bin/sh,/bin/python wait .

Shell Script variables

   We are Shell Often encountered in scripts $#,$*,$$,$@,$0,$1,$2,$?
Symbol of equal form , If you don't know what it means , It's easy to get confused , So it's important to remember these rules .

$# —— Indicates the number of parameters passed in by executing script

$* —— Represents the list of parameters passed in by the execution script

$$ —— Represents a process id

$@ —— Indicates that all parameters passed in by executing script

$0 —— Indicates the execution script name

$1 —— Represents the first parameter

$2 —— Represents the second parameter

$? —— Indicates script execution status 0 normal , Other means there is an error

Shell Delay can be added to the script

   stay Shell Some delay processing is often needed in scripts , So the delay needs to use the system's sleep or usleep.

sleep : Default in seconds (s) In units
usleep : Default in microseconds (us) In units

【 1s = 1000ms = 1000000us 】

in addition ,sleep Not only in seconds , You can also specify the unit of delay , for example :

sleep 1s Indicates one second delay
sleep 1m Indicates one minute delay
sleep 1h Indicates an hour delay
sleep 1d Indicates a day delay

   It is worth mentioning that , use sleep or usleep The delay will be blocked , If we were in a Shell script ( Let's call it the parent script ) Called another Shell
script ( Subscript ), And the subscript just uses latency , The parent script will also block . If that's not what you want , Please look down ( Reasonable use &).

Shell Script execution process

  Shell It is executed in single thread sequence , To achieve “ Multithreading ” It can be realized by running multiple processes in the background . stay bash
in , Use background tasks to implement the “ Multiprocessing ”. In uncontrolled mode , No matter how many tasks , All in the background . in other words , under these circumstances , As many tasks as there are “ process ” At the same time .
   Take a chestnut :
# !/bin/bash for ((i = 0 ;i < 5 ;i ++ )); do { sleep 3 ;echo 1 >> aa && echo "
done! " } done wait cat aa | wc - l rm aa
   In this case , Program sequence execution , Each cycle 3s, Common needs 15s about . The output is as follows :
$ time bash test . sh done ! done ! done ! done ! done ! 5 real 0m15 . 030s
user0m0 . 002s sys 0m0 . 003s
conduct “ Multiprocessing ” reform :
# !/bin/bash for ((i = 0 ;i < 5 ;i ++ )); do { sleep 3 ;echo 1 >> aa && echo "
done! " } & done wait cat aa | wc - l rm aa
   In fact, this transformation is based on the above, and an additional background is added for implementation & Symbol , It should be 5 Concurrent execution of circular tasks , Last need 3s About time . The output is as follows :
$ time bash test . sh done ! done ! done ! done ! done ! 5 real 0m3 . 011s user
0m0 . 002s sys 0m0 . 004s
   By the way wait The role of .wait
Wait for all the previous background tasks to be completed before proceeding , Otherwise, the program itself will not wait , In this way, errors may occur to the commands that depend on the results of the previous tasks . Like above wc -l The order of : non-existent
aa This document .

case :Shell The script finds a process and places it kill

#!/bin/bash NAME=$1 PID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep"
| awk'{print $1}'` echo $PID for id in $PID do kill -9 $id echo "killed $id"
test : For this Shell Script set execution permission ,chmod a+x, implement ./ Process name that will do .

©2019-2020 Toolsou All rights reserved,
" Cephalosporin wine Say go and go "? DANGER ! Don't drink alcohol when taking these drugs Programmer Tanabata Valentine's Day confession code China Lunar Rover “ Moon rabbit No.2 ” A strange rock was found on the moon use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor ) Software testing BUG describe Random forest R Language implementation TP6 Application examples of verifier and correct verification data Google says home office affects work efficiency !2021 Return to offline office in 2010 Message quality platform series | Full link troubleshooting Free download of documents : To introduce you to a few useful free download URL