当前位置:网站首页>Detailed explanation of shell condition judgment
Detailed explanation of shell condition judgment
2022-06-29 07:38:00 【TaotaoPlus】
test conditional
test command
1. Judgment of document type and existence
1.1 -e Determine whether a file or directory exists
[[email protected] ~]# test -e bbb.txt && echo ' file / directory already exists ' || touch bbb.txt
[[email protected] ~]# test -e bbb.txt && echo ' file / directory already exists ' || touch bbb.txt
file / directory already exists
[[email protected] ~]# test -e bbb && echo ' file / directory already exists ' || mkdir bbb
[[email protected] ~]# test -e bbb && echo ' file / directory already exists ' || mkdir bbb
file / directory already exists
1.2 -f Determine if it is a file
[[email protected] ~]# test -f bbb.txt && echo 'bbb.txt It's a document '
bbb.txt It's a document
[[email protected] ~]# test -f bbb || echo 'bbb It's not a document '
bbb It's not a document
1.3 -d Determine whether it is a directory
1.4 -b Judge whether it is block device device
1.5 -c Judge whether it is character device device
1.6 -S Judge whether it is socket file
1.7 -P Judge whether it is FIFO(pipe) file
1.8 -L Determine whether it is a connection
2. Judgment on file permissions
2.1 -r Determine whether Can be read
2.2 -w Determine whether Can write
2.2 -x Determine whether Executable
2.3 -u Judge whether there is SUID attribute
2.4 -g Judge whether there is SGID attribute
2.5 -k Judge whether there is sticky bit attribute
2.6 -s Judge whether it is Blank file
3. Comparison between two files , Such as test file1 -nt file2
3.1 -nt file1 Is it better than file2 new (new than)
3.2 -ot file1 Is it better than file2 used (old than)
3.3 -ef file1 Is it better than file2 Whether it is the same file
# Can be used for hard link The judgement of , As long as the meaning lies in determining whether both files point to the same inode
4. Two Integers The judgment of the
4.1 -eq equal
4.2 -ne It's not equal
4.3 -gt Greater than
4.4 -lt Less than
4.5 -ge Greater than or equal to
4.6 -le Less than or equal to
5. Judge string data
5.1 test -z str Determine whether it is an empty string ,str Empty return true
5.2 test -n str Determine whether the string is not empty ,str Not empty return true(PS;-n It can be omitted )
5.3 test str1 = str2 Judge whether it is equal
5.4 test str1 != str2 Judge whether it is not equal
6. Multiple condition judgment
6.1 -a Both are established at the same time (and)
test -r file -a -x file # The file also has r Authority and x jurisdiction
6.2 -o Either of the two is true (or)
6.3 ! Take the opposite
Bracket conditional judgment
The functions and test identical
Most used
The front and back must be separated by a space :[ -n $name ]
1. String judgment
[ -z "str" ] str The length of is zero , It is true , That is to judge whether it is empty , Empty is true ;
[ -n "str" ] str The length of is non-zero , It is true , That is, judge whether it is non empty , Not empty is true ;
[ "str1" = "str2" ] The two strings are the same , It is true ;
[ "str1" != "str2" ] The two strings are different , It is true ;
[ "str1" ] String is not empty , It is true , And -n similar .
2. File to judge
[ -e file ] file There is , It is true .
[ -d DIR ] file There is , And it's a directory , It is true .
[ -f file ] file There is , And it's an ordinary file , It is true .
[ -r file ] file Exist and readable , It is true .
[ -w file ] file Exists and is writable , It is true .
[ -x file ] file Existing and executable , It is true .
[ -b file ] file There is , And is a block special file , It is true .
[ -c file ] file There is , And it's a word special file , It is true .
[ -g file ] file Exists and has been set SGID, It is true .
[ -k file ] file The adhesive bit exists and has been set , It is true .
[ -p file ] file There is and is a name pipeline (F If O), It is true .
[ -s file ] file Exist and not of size 0, It is true .
[ -t FD ] File descriptor FD Open and point to a terminal , It is true .
[ -u file ] file Exist and set SUID (set user ID), It is true .
[ -O file ] file Exists and is a valid user ID, It is true .
[ -G file ] file Exists and belongs to a valid user group , It is true .
[ -L file ] file Exists and is a symbolic connection , It is true .
[ -N file ] file There is and has been mod, If ied since it was last read It is true .
[ -S file ] file Exists and is a socket , It is true .
[ file1 -ot file2 ] file1 Than file2 To the old , perhaps file2 Exist and file1 Do not save , It is true in .
[ file1 -ef file2 ] file1 and file2 Point to the same device and node number , It is true .
[ $a -gt $b -a $a -lt $c ] -a And
[ $a -gt $b -o $a -lt $c ] -o or
[ ! -z ${name} ] ! Take the opposite
Double bracket conditional judgment
[[ ]] Built in shell One of the orders in , It's better than just said [ ] Much more powerful . Support pattern matching in strings .[ ] Out of commission && and ||, Only use -a and -o
[[email protected] ~]# [ -r "aaa.txt" && -w "aaa.txt" ] && echo "aaa.txt Have read and write permission "
-bash: [: The lack of `]'
[[email protected] ~]# [[ -r "aaa.txt" && -w "aaa.txt" ]] && echo "aaa.txt Have read and write permission "
aaa.txt Have read and write permission
[[email protected] ~]# [ -r "aaa.txt" -a -w "aaa.txt" ] && echo "aaa.txt Have read and write permission "
aaa.txt Have read and write permission
[[email protected] ~]#
When comparing strings, you can use the one on the right as a pattern ( This is the string on the right without double quotation marks . If the string on the right is enclosed in double quotes , It is considered to be a text string .), Not just a string , such as [[ hello == hell? ]], The result is true .
Double bracket conditional judgment
let conditional
边栏推荐
- [translation] swarmed out. Design methods for building modern applications
- Schnuka: 3D visual inspection scheme 3D visual inspection application industry
- excel高级绘图技巧100讲(六)-甘特图在项目进度上的实战应用案例
- 关于KingbaseES临时文件过大问题
- [popular science materials] materials from scientific spirit to scientific knowledge
- ES中配置ext.dic文件不生效的原因
- 道闸控制器通讯协议
- Use of LSTM neural network and general neural network
- cv2.cvtColor
- Mmclassification installation and debugging
猜你喜欢

【工控老马】基于PLC的花样喷泉设计原理详解

Matlab Simulink simulation and analysis of power grid sweep frequency

matlab 多普勒效应产生振动信号和处理

Appium自动化测试基础 — ADB常用命令(二)
![[popular science materials] materials from scientific spirit to scientific knowledge](/img/fc/647805056c8ff2b30e894a6e6c4a36.jpg)
[popular science materials] materials from scientific spirit to scientific knowledge

ES中配置ext.dic文件不生效的原因

Markdown skill tree (5): picture

ShapeShifter: Robust Physical Adversarial Attack on Faster R-CNN Object Detector

Simulation analysis of sailing dynamics

阿里云访问资源:NoSuchKey
随机推荐
Imx6dl4.1.15 supports EIM bus (upper) - actual operation and modification.
matlab 多普勒效应产生振动信号和处理
[translation] E-Cloud. Large scale CDN using kubeedge
Imx6dl4.1.15 supports EIM bus (Part 2) - configuration principle analysis.
节流的两种写法-最近看到的这种写法
Mmclassification installation and debugging
Markdown skill tree (8): code blocks
TF. Repeat and stack operations of slim
SAP UI5 初学 ( 一 )、简介
Listen to textarea input through Keyup to change button style
1183: patient queue
【工控老马】基于西门子S7-200PLC的跑马灯控制系统的设计方案详解
KingbbaseES V8R6集群维护案例之---集群之间数据迁移
ShapeShifter: Robust Physical Adversarial Attack on Faster R-CNN Object Detector
What is a Test Architect
Deploy Prometheus server service system management
Markdown skill tree (6): List
【科普资料】从科学精神到科学知识的材料
什么是测试架构师
E-commerce is popular, how to improve the store conversion rate?