The testing Test command in shell programming can basically be divided into 3 data types, each of which is different. Individuals are more inclined to understand the rules of writing conditional statements, that is, test plus conditional plus judgment statement.
1 Numeric types
Basic can be divided into 6 judgments:-eq equals,-ne not equal to,-gt greater than,-lt less than,-ge greater than equals,-le less than equals
Example: a=1;b=2
if test $[a]-eq $[b] #如果a等于b, then ...,-NE,-GT,, and so on can, in addition, if not a formula, you can not add square brackets, $a + $b
Then
echo "A=b" #缩进4个4个都行, habitually indented 4
Else
echo "A!=b"
PS: Formulas are written in two ways: $[a + b] and ' expr $a + $b '
2 string
There are 4 judgments: =,! =,-Z (length 0) and-n (length not 0)
Example: astr= ' hehehehe '; Bstr= ' Hahahaha '
if test $astr = $bstr
Then
echo "ASTR = BSTR"
Else
echo "Astr!=bstr"
Fi
3 File Test
Basically there are 9 commonly used parameters, the basic format is Test + parameter + filename, the parameters are:
-e:exists, there is such a file;
-r:readable, the document is readable;
-w:writable, a document that can be written;
-X: The file is executable,
-S: exists and is not empty
-d:directory, Existence directory
-f:file, exists and is an ordinary file,
C: File exists and is a special file of character #这个地方怎么理解呢, can be associated with the first character of the LS command : '-' represents a file. D is the directory,l represents the connection file,b represents the storage interface device in the device file,C represents the serial port settings inside the device file. Https://zhidao.baidu.com/question/1820545103813500748.htm
Instance:
Cd/etc
If test-f/etc/passwd #此处可以结合与或非逻辑语句: Shaped like if test - e ./notfile -o -e ./ bash indicates presence./notfile or./bash
Then
echo "Yes"
else
echo "No"
Fi
Shell Programming Test Command