当前位置:网站首页>Basic series of SHEL script (II) syntax + operation + judgment
Basic series of SHEL script (II) syntax + operation + judgment
2022-07-05 07:20:00 【jiankang66】
One 、 background
Although I am a java The programmer , Do the back end , But recently, I often need to read scripts written by others to run projects , So as a back-end programmer , We also need to know shell grammar , Can read some basic shell Script .
Two 、 Introduction to script definition and execution
1、bash The script header defines the executable environment , Define the interpreter , The program specified by the following path is the program that interprets this script file Shell Program .
#!/bin/bash2、 Give script permission
chmod +x a.sh3、 perform bash Script
./a.sh4、 Check execution
bash -x zhishu.shAfter execution , Will be displayed bash The execution of the script , as follows , It is a prime number script execution process .
+ read -p Please enter a number : number
Please enter a number :33
+ '[' 33 -eq 1 ']'
+ '[' 33 -eq 2 ']'
++ seq 2 32
+ for i in '`seq 2 $[$number-1]`'
zhishu.sh: line 12: %2: syntax error: operand expected (error token is "%2")
+ echo 33 Prime number
33 Prime number 5、 Shortcut key
(1) Not insert In mode , Do the following , Copy the current row .
yyp(2) Not insert In mode , Do the following , Delete several lines below the cursor .
10d6、 Escape character ,
(1) Add... To the front \, Example , Defines a variable $0.
echo "\$0 = $0"7、 Execute commands in the script without output
ping -c1 $ip &>/dev/null3、 ... and 、read Command syntax
1、、 Prompt the user to enter
(1) It will show input your name;, The user will assign the input content to name Variable .
read -p "input your name:" name
# Print user input
echo ${$name}(2) User input is hidden ,-p The parameters entered by the user will be hidden , Console not displayed .
read -s -p "input your name:" nameFour 、 arithmetic
1、 common 4 There are two operation modes , Remember that syntax can write complex operations in scripts .
(1) The first one is
$((1+1)) (2) The second kind
$[84*1] (3) The third kind of , Space required
expr 1 + 1 # Note blank space
expr 10 \* 2 #\ For escape characters 4、 let Calculate variables
n=1;let n=n+2;echo $n # The result is 3
ln=1;let n=n**3;;echo $n # The result is 85、 ... and 、 conditional
1、 The first way ,
(1) Be careful : There should be spaces between brackets and calculated fields , Otherwise, an error will be reported
[[ ]](2) Example :
[[ ! -d ./dir ]];echo $? # Determine whether it is a directory , If yes, go back to 02、 The second way ,
(1) There should be spaces between brackets and calculated fields , Otherwise, an error will be reported
[](2) Example
[ -L ./va.sh ];echo $? # Determine whether it is a linked file , In this way, spaces are required in brackets 3、 The third way
(1) Use test command
test(2) Example :
test test -f varable.sh # Determine if it's a document
test -d dir2;echo $? # Determine if it's a directory
test varable.sh -nt a.sh;echo $? # Judge whether the previous file is newer than the following file
test a.sh -ef b.sh ;echo $? # Compare whether the two files are the same
test -z "hello world";echo $? # Determines if the string is empty 1 Not empty
test -n "hello world";echo $? # Determine whether the string is non empty 1 Not empty
test "hello"="word" ;echo $? # Determine whether two strings are equal
test 1 -eq 2;echo $? # Judge 1 and 2 Whether it is equal or not 1 Is not equal 4、 Options for judging numbers
| -eq | be equal to |
| -ne | It's not equal to |
| -gt | Greater than |
| -lt | Less than |
| -ge | Greater than or equal to |
| -le | Less than or equal to |
5、 Determine file options
| -d | Whether it is a directory |
| -f | Is it a document |
| -e | Whether there is |
| -ef | Are the two files equal |
| -w | Whether a file can be written |
| -L | Is there a linked file |
| -s | Determine whether a file is not empty |
| ! -s | Judge whether the file content is empty |
6、 String judgment options
| -z | Determines if the string is empty |
| -n | Determine whether the string is non empty |
7、 Other orders passed man test see , Yes string Comparison , Document judgment, etc
6、 ... and 、 Multi condition judgment
1、 grammar
(1) Logic and
-a perhaps &&(2) Logic or
-o perhaps ||(3) Example
# Determine whether the user is an administrator , Print the following content if the conditions are met
[ $(id -u) -eq 0 ]&& echo “ It's the administrator ” 2、if structure
(1) Grammar 1
if [ condition ];then
command
fi(2) Grammar II
if test condition ;then
command
else
command
fi(3) Grammar 3
if test condition ;then
command
elif test condition ;then
command
else test condition ;then
command
fi3、 The application case
(1) Determine whether the host is ping through
#!/bin/bash
read -p 'ping Which one? ip:' ip
# Execute commands in the script without output
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo Through the
else
echo no
fi(2) Determine whether a process exists , 0 For existence , Use ps command
#grep -v 'grep' The result does not include the current command process
ps -ef|grep clickhouse|grep -v 'grep';echo $?(3) Determine whether a process exists , 0 For existence , Use pgrep command
# Determine if a process exists
pgrep clickhouse &>/dev/null
test $? -eq 0 && echo " At present clickhouse The process exists " || echo " The current process does not exist "(4) Determine whether a user exists
read -p " Please enter a user :" username
id $username &>/dev/null
if test $? -eq 0;then
echo "$username Users exist "
else
echo "$username The user doesn't exist "
fi7、 ... and 、 summary
The above is about shell Grammar variables , operation , Judge all relevant contents , Hopefully that helped , You can refer to it , If you think it's good , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .
边栏推荐
- Rough notes of C language (2) -- constants
- Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
- Don't confuse the use difference between series / and / *
- 【软件测试】06 -- 软件测试的基本流程
- list. files: List the Files in a Directory/Folder
- Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
- Ros2 - workspace (V)
- What if the DataGrid cannot see the table after connecting to the database
- golang定时器使用踩的坑:定时器每天执行一次
- 现在有html文件,和用vs制作的mvc(连接了数据库),怎么两个相连?
猜你喜欢

PHY drive commissioning - phy controller drive (II)
![[idea] efficient plug-in save actions to improve your work efficiency](/img/6e/49037333964865d9900ddf5698f7e6.jpg)
[idea] efficient plug-in save actions to improve your work efficiency

Hdu1232 unimpeded project (and collection)

剑指 Offer 56 数组中数字出现的次数(异或)

一文揭开,测试外包公司的真实情况

Netease to B, soft outside, hard in

IPage能正常显示数据,但是total一直等于0

【无标题】

Ros2 - node (VII)

【Node】nvm 版本管理工具
随机推荐
Ros2 - common command line (IV)
PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
Target detection series - detailed explanation of the principle of fast r-cnn
I can't stand the common annotations of idea anymore
Concurrent programming - how to interrupt / stop a running thread?
Ugnx12.0 initialization crash, initialization error (-15)
Matrix and TMB package version issues in R
基于Cortex-M3、M4的GPIO口位带操作宏定义(可总线输入输出,可用于STM32、ADuCM4050等)
C#学习笔记
Install deeptools in CONDA mode
Word import literature -mendeley
Chapter 2: try to implement a simple bean container
Implementation of one-dimensional convolutional neural network CNN based on FPGA (VIII) implementation of activation layer
Application of MATLAB in Linear Algebra (4): similar matrix and quadratic form
2022 PMP project management examination agile knowledge points (7)
Altimeter data knowledge point 2
The SQL implementation has multiple records with the same ID, and the latest one is taken
2022.06.27_ One question per day
CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)
并查集理论讲解和代码实现