当前位置:网站首页>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
边栏推荐
- Apt update and apt upgrade commands - what is the difference?
- [teacher Zhao Yuqiang] redis's slow query log
- [teacher Zhao Yuqiang] use the catalog database of Oracle
- Azure file synchronization of altaro: the end of traditional file servers?
- Exception when introducing redistemplate: noclassdeffounderror: com/fasterxml/jackson/core/jsonprocessingexception
- 2022.DAY592
- Es 2022 officially released! What are the new features?
- How does win7 solve the problem that telnet is not an internal or external command
- 2022.DAY592
- NG Textarea-auto-resize
猜你喜欢

配置xml文件的dtd

求质数的方法

一起上水碩系列】Day 9

"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
![[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation](/img/9b/a309607c037b0a18ff6b234a866f9f.jpg)
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation

Kubernetes resource object introduction and common commands (V) - (configmap)

Method of finding prime number

Latest version of source insight

pytorch 搭建神经网络最简版

Communication - how to be a good listener?
随机推荐
Final review (Day2)
Kubernetes resource object introduction and common commands (V) - (configmap)
Redis使用Lua脚本简介
[teacher Zhao Yuqiang] RDB persistence of redis
理解 期望(均值/估计值)和方差
[escape character] [full of dry goods] super detailed explanation + code illustration!
Qt读写Excel--QXlsx插入图表5
Deploy crawl detection network using tensorrt (I)
2022.7.2day594
[teacher Zhao Yuqiang] MySQL flashback
NG Textarea-auto-resize
ansible防火墙firewalld设置
How to use source insight
求质数的方法
【无标题】
Sophomore dilemma (resumption)
How to install and configure altaro VM backup for VMware vSphere
70 shell script interview questions and answers
[teacher Zhao Yuqiang] use the catalog database of Oracle
Life is a process of continuous learning