Test: [test conditions test content] more widely used test file type test-e file name test file exists, exist true [-e file name] Note: the left and right sides of [] must be blank & amp; 26684;
Conditional test
Test conditions test content
[Test conditions] # more widely used
1. test file type
Test-e file name test file exists, true
[-E file name] Note: spaces must be added on both sides!
Appendix: test results are not displayed. echo $? is required? To display the return value of test.
The return value is 0. if the return value is not 0, the return value is incorrect.
Extended: test-e test.txt & echo Yes | echo No
Test-f file name determines whether it is a common file # if it is a file, Yes is returned, if it is a directory, non-No is returned
Test-d file name determines whether the file is a directory
Test-B file name determines whether it is a block device file
Test-c file name character device file
2. test file permissions
Test-r file name to determine whether the file has the read permission
Test-w file name writable
Test-x file name execution
Test-s file name determines whether it is non-blank, and the content is true
3 Comparison of two documents
[File1-nt file2] is file1 newer than file2?
[File1-ot file2] is file1 older than file2?
[File1-ef file2] whether file1 and file2 are linked files, shortcuts, and soft links
4. judge between two values
[N1-eq n2] whether n1 and n2 are equal # Use the = symbol below to determine whether n1 and n2 are equal #
Test: aa = 123; bb = 123
[$ Aa-eq $ bb] & echo Yes | echo no
[N1-ne n2] does n1 and n2 differ?
[N1-gt n2] n1 is greater than n2
[N1-lt n2] n1 less than n2
[N1-ge n2] n1 is greater than or equal to n2
[N1-le n2] n1 is less than or equal to n2
5. judge the string
[-Z string] determines whether the string is null. if the variable value has no content or is empty, it is true.
[String 1 = String 2] determine if String 1 is equal to string 2 # Use string type
[$ Aa = $ bb] & echo 1 | echo 2
Appendix: aa = abc
["$ Aa" = "abc"] & echo Yes | echo No # Why did the two "" be removed from the test?
[String 1! = String 2] determine whether the string is not equal
6 Multiple judgments
-A logic and
[-Z $ file-a-e $ file]
-O logic or
! Non-logical
Example 7: determine the input file type and file permissions. note: exercise.
#! /Bin/bash
Echo-e "nide wenjian shifou cunzai? Wenjian quanxian shi shenme? \ N"
Read-p "please input filename:"-t 30 filename
Test-z $ filename & echo "please input filename !! "& Exit 1
# Determine whether the string is null. if it is null, print the error message and exit the program!
Test! -E $ filename & echo "wenjian bucunzai! "& Exit 2
#-E variable determines whether a file exists. it is true! Non-logical
Test-f $ filename & filetype = putong
#-F is a common file
Test-d $ filename & filetype = mulu
#-D is a directory
Test-r $ filename & perm = "read"
#-R has the read permission
Test-w $ filename & perm = "$ perm write"
#-W is writable
Test-x $ filename & perm = "$ perm executable"
#-X executable
Echo-e "the filename is: $ filename \ n"
# Print the file name
Echo-e "filetype is: $ filetype \ n"
# Print file type
Echo-e "permision is: $ perm \ n"
# Print File permissions
Multi-command sequential execution
1) Command 1, Command 2, and command 3 Command 123 are executed sequentially. There is no relationship between them, such as: cd; ls; date # date is used to print the current system time
2) Command 1 & Command 2 Command 1Correct executionCommand 2 will run
3) Command 1 | Command 2 Command 1 is not correctly executed. Command 2 is executed. that is to say, if the first command is correctly executed, the second command will not be executed!
Test:
A) ls & echo "Yes" | echo "No"
B) ls skdjfkdj & echo "Yes" | echo "No"
Lsaa & echo "cunzai" | echo "bu cunzai! "Execute ls aa to determine if it is correct and the output" exists ". If it does not exist, the output "does not exist"
7. pipeline operator: there is a connection between the two commands,
Command 1 | Command 2 Command 1 execution result, as the execution condition of Command 2
Note: The first command must have output.