当前位置:网站首页>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
边栏推荐
- cv::Mat与Base64转换(含图片压缩解压等流程)
- tf.count_nonzero
- Schnuka: visual positioning system manufacturer what is a visual positioning system
- [translation] how Bink drives the digital loyalty transactions of some of the largest banks in the UK
- Suggestions on working methods and efficient work
- Concurrent idempotent anti shake
- Markdown skill tree (1): introduction to markdown
- SQL 注入绕过(六)
- 施努卡:视觉定位系统厂家 什么是视觉定位系统
- Blue Bridge Cup - minimum frame
猜你喜欢

Appium自动化测试基础 — ADB常用命令(三)

tf.count_nonzero

Explain canfd message and format in AUTOSAR arxml in detail

Schnuka: 3D visual inspection scheme 3D visual inspection application industry

蓝桥杯——最小框架

解题-->在线OJ(十三)

Kyushu cloud helps Inner Mongolia's "counting from the east to the west" project to drive the smart new ecology of the surveying and mapping industry
What tools do testers need to know

Autosar SWC在Simulink中Parameter的使用
![[FreeRTOS] interrupt mechanism](/img/ab/9b1d07048b4631d7cc95db99ed529a.png)
[FreeRTOS] interrupt mechanism
随机推荐
[translation] swarmed out. Design methods for building modern applications
Final summary spark
服裝行業的CRM品牌供應商如何選型?
Blue Bridge Cup -- Analysis of the second batch of test questions of the 13th session
TF.Slim的repeat和stack操作
TF. Repeat and stack operations of slim
项目中 if else 的代替写法
Imx6dl4.1.15 supports EIM bus (Part 2) - configuration principle analysis.
详解Autosar Arxml中的CANFD报文及格式
Unexpected exception ... code: Badrequest when downloading Xilinx 2018.2
SAP ui5 Beginner (I) Introduction
101. 对称二叉树(递归与迭代方法)
719. 找出第 K 小的数对距离(二分)
KingbaseES应对表年龄增长过快导致事务回卷
测试人员需要了解的工具有哪些
节流的两种写法-最近看到的这种写法
Schnuka: 3D machine vision inspection system 3D vision inspection application industry
Schnuka: what is visual positioning system? How visual positioning system works
Markdown skill tree (7): separator and reference
Markdown skill tree (2): paragraph and emphasis