当前位置:网站首页>It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
2022-07-03 05:47:00 【Tell a joke】
List of articles
Preface
Shell It's a use. C A program written in a language , It is used by users Linux The bridge .Shell It's a command language , Another programming language .
Shell It's an application , This application provides an interface , Users access the services of the operating system kernel through this interface .
Ken Thompson Of sh It's the first one Unix Shell,Windows Explorer It's a typical graphic interface Shell.
One 、Shell Variable
The rules :
· Only English letters can be used for naming 、 Numbers and underscores , The first character cannot begin with a number .
· No spaces in between , You can use underscores (_)
· You can't use punctuation
· Out of commission bash Keywords in ( You can use help Command to view reserved keywords )
1、 Custom variable
Define the format of the variable :
Variable name = A variable's value
Unformatted variable :
unset Variable name
matters needing attention :
√ = There must be no spaces on either side , Do not use keywords as variable names , Such as :ls 、 cd etc. ;
√ If the variable name already exists, overwrite the previous variable value
√ Variable names are : Letter / Numbers / Underline , Don't start with numbers
2、 System preset variables
① environment variable ( Variable names are usually capitalized , Operating system maintenance )
Stored in /etc/profile or ~/.bash_profile
Common environment variables are :PATH \ PWD \ USER \ UID \ HOME \ SHELL
② Positional variable (bash Built-in variables , Store parameters during script execution )
Store parameters during script execution
Use $n Express ,n Is a numeric serial number
Create a system account through the location variable , Configure a password
[[email protected] ~]# vim /root/user.sh
#!/bin/bash
useradd "$1" // Create user
echo "$2" | passwd --stdin "$1" // Set the password for the user
[[email protected] ~]# sh /root/user.sh abc 123
③ Predefined variables (bash Built-in variables , Can be called but cannot be assigned or modified )
It is used to save the execution information of the script program
Use these variables directly , You can't assign values to these variables directly
Variable name | meaning |
---|---|
$0 | Name of the current process or script |
$$ | Of the currently running process PID Number |
$? | Return status after command execution ,0 Is normal ,1 Or other values indicate an exception |
$# | Number of location variables loaded |
$* | The value of all position variables |
3、 The difference between quotation marks in variables
① The difference between multiple quotation marks
quotes | difference |
---|---|
Double quotes “” | Allow extension , With $ Reference other variables |
Single quotation marks ‘’ | Disable extensions , Even if $ It is also regarded as a common symbol |
The quotation marks `` | Take the execution output of the command as the variable value ,$() Equivalent to backquotes |
② Global variables and local variables
local variable : The newly defined variable defaults only to the current variable shell Effective in the environment , Can't be in a child shell Use in the environment Global variables : Any in the system shell Effective in all environmentsTwo 、Shell sentence
1、 Conditions for testing
① Introduction to comparison operators
The operator | meaning |
---|---|
-eq | be equal to (equal) |
-ne | It's not equal to (not equal) |
-ge | Greater than or equal to (greater or equal) |
-le | Less than or equal to (less or equal) |
-gt | Greater than (greater than) |
-lt | Less than (less than) |
② Introduction to file status operator
The operator | meaning |
---|---|
-e | Determines whether an object exists (exist), If it exists, the result is true |
-d | Determine whether the object is a directory (directory), Yes, it is true |
-f | Determine whether the object exists as a general file (file), Yes, it is true |
-r | Determine whether the object has read permission (read), Yes, it is true |
-w | Determine whether the object has writable permissions (write), Yes, it is true |
-x | Determine whether the object has executable permissions (excute), Yes, it is true |
③ Introduction to control operators
The operator | meaning |
---|---|
; | Separate multiple commands . Do not interfere with each other during execution |
&& | Both the previous and subsequent commands were executed successfully , The whole command is right , Otherwise it's a mistake |
II | Two commands before and after , Any one is executed successfully , The overall command is correct |
2、if Judgment statement
①if Single branch statements
Grammar format :
if Conditions ;then
command
fi
example :
[[email protected] ~]# vim /root/shell/user.sh
#!/bin/bash
read -p " Please enter a user name :" user
read -s -p " Please input a password :" pass
if [ ! -z "$user"] && [ ! -z "$pass"];then
useradd "$user"
echo "$pass" | passwd --stdin "$user"
fi
②if Two branch statements
if Conditions ;then
command 1
else
command 2
fi
③if Multi branch statement
Grammar format :
if Conditions 1;then
command 1
elif Conditions 2;then
command 2
else
command 3
fi
example :
[[email protected] ~]# vim /root/shell/number.sh
#!/bin/bash
clear
num=$[RANDOM%10+1]
read -p " Please enter 10 A random integer within :"guess
if [ $guess -gt $num ];then
echo "Oops, Guess the "
elif [ $guess -lt $num ];then
echo "Oops, Guess a little "
else
echo " Congratulations , Guessed it , Namely :$num"
fi
3、while loop
√ Test conditions repeatedly , As long as it is established, execute the order
Grammar format :
while Conditions
do
command
done
example :
[[email protected] ~]# vim /root/while.sh
#!/bin/bash
i=1
while [ $i -le 5 ]
do
echo $i
done
4、for loop
effect : Reduce code redundancy and duplication
Grammar format 1:
for Variable in List of values
do
command
done
Grammar format 2:
for ( initial value ; Conditions ; step )
do
Command sequence
done
example :
[[email protected] ~]# vim /root/for.sh
for i in 1 2 3 4 5
do
echo "this is a number $i"
done
边栏推荐
- Download the corresponding version of chromedriver
- 2022.DAY592
- [explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
- MySQL 5.7.32-winx64 installation tutorial (support installing multiple MySQL services on one host)
- [teacher Zhao Yuqiang] use Oracle's tracking file
- Source insight operation manual installation trial
- Apt update and apt upgrade commands - what is the difference?
- ROS Compilation Principle
- Niuke JS separator
- Final review (Day2)
猜你喜欢
2022.DAY592
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation
一起上水硕系列】Day 9
pytorch DataLoader实现miniBatch(未完成)
[teacher Zhao Yuqiang] calculate aggregation using MapReduce in mongodb
Final review (Day5)
How to install and configure altaro VM backup for VMware vSphere
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
【一起上水硕系列】Day 10
Qt读写Excel--QXlsx插入图表5
随机推荐
pytorch 多分类中的损失函数
Introduction to redis using Lua script
mysql启动报错:The server quit without updating PID file几种解决办法
[advanced pointer (1)] | detailed explanation of character pointer, pointer array, array pointer
Calculation method of AUC
Solve the problem of automatic disconnection of SecureCRT timeout connection
Strategy pattern: encapsulate changes and respond flexibly to changes in requirements
[set theory] relational closure (reflexive closure | symmetric closure | transitive closure)
Why is the website slow to open?
Shanghai daoning, together with American /n software, will provide you with more powerful Internet enterprise communication and security component services
Ext4 vs XFS -- which file system should you use
How to use source insight
How to set up altaro offsite server for replication
[teacher Zhao Yuqiang] MySQL high availability architecture: MHA
2022.7.2 模拟赛
【无标题】
mapbox尝鲜值之云图动画
[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
C 语言文件操作函数大全 (超详细)
【无标题】