This is the 23rd day of my participation in the August More Text Challenge
A lifelong learner, practitioner and sharer committed to the path of technology, a busy and occasionally lazy original blogger, an occasionally boring and occasionally humorous teenager.
Welcome to dig friends wechat search “Jie Ge’s IT journey” attention!
The original link: Shell programming – condition test | base paper
What is a Shell
A Shell is a command interpreter that interprets and executes commands typed at a command line prompt. In addition, the Shell has another function. If you want to execute more than one command, it can store the set of commands in a file, which can then be executed just like any other program provided by the Linux system. This command file is called a Shell program or Shell script. When the file is run, it executes the commands in the same order as if they were typed on the command line.
Shell scripts support variables, command-line arguments, interactive input, function modules, various control statements, and other features of high-level programming languages, such as if, case, while, and for. Shell programming language can be used to write scripts with very complex functions, to automate a large number of tasks, especially those requiring a large number of commands in the execution of the system management tasks without much intervention with users.
In order for the Shell to read and execute Shell programs, the file permissions of Shell scripts must be set to be readable and executable. To allow the Shell to find the program, you can either enter the full pathname, or place the script’s PATH in the list of paths specified by the PATH environment variable. Shell scripts are not complex programs that are interpreted line by line from top to bottom. The first line of the script always begins with “#! <Shell interpreter file path > is used to specify which Shell the script is using for interpretation execution.
Write Shell scripts
1) Use VI or any other text editing tool to create a file called Helloworld.sh and add the following content to it:
[root@localhost ~]# vi HelloWorld.sh #! /bin/bash #The first Shell script echo "Hello World!"Copy the code
2) Add execute permission to helloworld.sh;
[root@localhost ~]# chmod +x HelloWorld.sh
Copy the code
3) Run the helloworld. sh script to view the result.
[root@localhost ~]# ./HelloWorld.sh
Hello World!
Copy the code
Three, condition test | numerical test
When writing a Shell script, you sometimes test for consistent strings, equal numbers, or file status, and then proceed based on the results of those tests. Conditional tests for Shell scripts can be used to test strings, file state, and numbers.
When the test is complete, you can pass the “$? Gets the result of the test, where 0 is correct and 1 is wrong.
The value test compares two values and obtains the judgment result, including: equal to, not equal to, greater than, greater than or equal to, less than or equal to.
Numerical judgment format: [numeric 1 relational operator numeric 2]
There must be a space between square brackets and conditions.
Test whether two values are equal;
[root@localhost ~]# [1024-eq 1024] // Test whether 1024 is equal to 1024 [root@localhost ~]# echo $? 0 // The two values are equal. Change the first value to 1124 and test again. [root@localhost ~]# [ 1124 -eq 1024 ] [root@localhost ~]# echo $? 1 // The two values are not equal [root@localhost ~]# number1=500 // Number1 is 500 [root@localhost ~]# number2=254 //number2 is 254 [root@localhost ~]# [ $number1 -gt $number2 ] [root@localhost ~]# echo $? 0 //number1 is greater than number2 [root@localhost ~]#Copy the code
Four, condition test | string tests
String tests can compare the values of two strings and test whether the value of a single string is empty or non-empty.
String test format: [relational operator string] or [string 1 relational operator string 2]
To test whether two strings are equal [root@localhost ~]# [" ABC "=" ABC "] [root@localhost ~]# echo $? [root@localhost ~]# ["bac" = "ABC"] [root@localhost ~]# echo $? 1 // Two strings are not equal if the operator is changed to "! = "/ root @ localhost ~ # [" bac"! = "ABC"] / root @ localhost ~ # echo $? 0 Check whether the environment variable is empty or not empty [root@localhost ~]# [-z $python1] [root@localhost ~]# echo $? 0 [root@localhost ~]# [ -n $python1 ] [root@localhost ~]# echo $? 0 // PythON1 variable is empty [root@localhost ~]# python1="test" // Assign the python1 variable [root@localhost ~]# [-z $python1] [root@localhost ~]# echo $? 1 //python1 variable is not empty [root@localhost ~]#Copy the code
Five, the test conditions | file status test
The Shell script of Linux supports file status detection, including file type, file permission, and file length.
File state test format: [relational operator string]
1 root root 56 6月 4 07:55 helloworld.sh [root@localhost 20190105]# [-r HelloWorld.sh ] [root@localhost 20190105]# echo $? 0 //HelloWorld.sh file readable [root@localhost 20190105]# [-w helloworld.sh] [root@localhost 20190105]# echo $? 0 //HelloWorld.sh file can be written to [root@localhost 20190105]# [-x helloWorld.sh] [root@localhost 20190105]# echo $? 0 // Helloworld.sh file is executable [root@localhost 20190105]#Copy the code
Logical operators for conditional tests
There are three types of logical operators:
- -a: logical and. The result is true only if conditions on both sides of the operator are true. Otherwise, the result is false.
- -o: the result of the logical or operator is true as long as one of the conditions on both sides is true. The result is false only if all the conditions on both sides are false.
- ! : Logical no. The condition is false and the result is true.
[root@localhost 20190105]# ll test1 test2-rw-rw-r -- 1 root root 0 6月 4 09:25 test1-rw-rw-r -- 1 root Root 0 6月 4 09:25 test2 [root@localhost 20190105]# [-r test1-a -r test2] // Test whether files test1 and test2 are readable [root@localhost 20190105]# echo $? 0 [root@localhost 20190105]# [-x test1-o-x test2] // Is at least one of the test files test1 and test2 executable [root@localhost 20190105]# echo $? 1 To test two numerical variables [root@localhost 20190105]# number1=10 [root@localhost 20190105]# number2=20 [root@localhost 20190105]# [ $number1 -eq 10 -a $number2 -gt 20] $number1 > 10 and number2 > 20 [root@localhost 20190105]# echo $? 1 To test whether file test1 is unreadable [root@localhost 20190105]# ls -l test1-rw-rw-r --. 1 root root 0 6月 4 09:25 test1 [root@localhost 20190105]# [! -r test1] // Is test1 unreadable [root@localhost 20190105]# echo $? 1 [root@localhost 20190105]#Copy the code
Recommended reading
Super hardcore! 11 very useful Python and Shell scripts to use examples!
7 very useful Shell take to use script examples!
In this paper, to the end.
Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!
By the way, please give me some free attention! In case you get lost and don’t find me next time.
See you next time!