Control statements in linux

Control statements in linux

Control statement

Commonly listed three of the following. The lower-written string represents the control command, and the uppercase represents other meaning.
1. if TEST then COMMANDS elif TEST then COMMANDS fi
2. for VAR in LIST do COMMANDS done
3. while TEST do COMMANDS done

Commands, the collection of commands; var, refers to variables; Test is a test statement, several forms:

 1 [ -A file] If the File exists, it is true. 
2 [ -B file] If the file exists and is a special file, it is true.
3 [ -c file] If the file exists and is a word special file.
4 [ -D file] If the file exists and a directory is true.
5 [ -E File] If the File exists, it is true.
6 [ -f File] If the file exists and is a normal file.
7 [ -g file] If the file exists and the SGID has been set.
8 [ -h file] If the file exists and is a symbolic connection.
9 [ -K file] If the File exists and has been set, it is true.
10 [ -P file] If the file exists and is a name pipe (f, if o) is true.
11 [ -R file] If the file exists and readable is true.
12 [ -s File] If the file exists and the size is not 0, it is true.
13 [ -T fd] If the file descriptor FD is turned on and points to a terminal.
14 [ -u File] If File exists and sets the Suid (set User ID is true.
15 [ -w file] If File is true if the file exists and writable is true.
16 [ -X file] If the file exists and is executable.
17 [ -O File] If File exists and the valid user ID is true.
18 [ -G file] If the File exists and the valid user group is true.
19 [ -L File] If the file exists and is a symbolic connection.
20 [ -N file] If the file exists And Has Been MOD If IED Since It Was Last Read is true.
21 [ -S File] If File exists, it is true.
22 [ FILE1 -NT file2] If File1 Has Been Changed More Recently Than File2, or if File1File2 Does Not is true. EXISTS AND
23 [ FILE1 -OT file2] If File1 is older than file2, or file2 exists and File1 does not exist.
24 [ FILE1 -EF file2] If File1 and File2 points to the same device and node number, it is true.
25 [ -o OptionName] If the shell option "optionname" is turned on.
26 [ -Z string] The length of the String is true to zero.
27 [ -N string] or [string] String length is non-zero NON-ZERO is true.
28 [ STRING1 == String2] If 2 strings are the same. = may be used instead of == for Strict Posix Compliance is true.
29 [ STRING1 != String2] If the string is not equal, it is true.
30 [ STRING1 String2] If string1 sorts before string2 lexicographically in The Current Locale is true.
31 [ STRING1 > String2] If string1 sorts inster string2 lexicographically in The Current Locale is true.
32 [ ARG1 OP ARG2 ] OP is one of -eq, -ne, -lt, -le, -gt or -ge.
33
34 [ ! EXPR] If the expr is false, it is true.
35 [(EXPR)] Returns the value of EXPR. This can be used to ignore the normal operator priority.
36 [ EXPR1 -A expr2] If the expr1 and expens is true.
37 [ EXPR1 -o expr2] If EXPR1 or EXPR2 is true for true.
38
39 TEST can also be a command returned. E.g
40 if "hello world" | grep hello > /dev/null ; then echo "hello is here" ; fi
if [ $count -lt $times ] then count=`expr $count + 1` echo $count fi
for loop in `ls` do echo $loop done

Источник

Control statements in linux

Test statement 10 Shell’s unique functions, Shell provides a set of test operators, through these operators, the Shell program can determine whether a certain or several conditions are true.
In Shell, users can use test statements to test the true and false conditions of the specified conditional expression. When the specified condition is true, the return value of the condition test is 0; otherwise, the return value of the condition test is non-zero. There are two types of conditional test syntax, test command and [] command

1.1 test statement

Among them, the parameter expression represents the statement that needs to be tested, the entire test statement is true, test returns 0, if it is false, it returns non-zero

The author strongly recommends the test statement, the [] statement is very bad, the requirements are very strict, and various errors will occur, which is not conducive to learning by beginners.

1.2[] statement

Among them, the statement of the parameter expression is exactly the same as the syntax in the test command.There must be a space between the conditional expression and the left and right square brackets

1.3 File testing

File testing refers to judging the file attributes and types under the current path according to the given path.

Environment variable Description
-a If the file exists, the conditional test returns 0
-b If the file exists and is a block file, the conditional test returns 0
-c If the file exists and it is a character file, the condition test returns 0
-d If the file exists and is a directory file, the conditional test returns 0
-e If file, the condition test returns 0
-f If the file exists and is a regular file, the conditional test returns 0
-r If the file exists and is readable, the conditional test returns 0
-w If the file exists and is writable, the conditional test returns 0
-x If the file exists and is executable, the conditional test returns 0
-p If the file exists and is a FIFO file, the condition test returns 0
-s If the file exists and is not empty, the conditional test returns 0

1.4 String test

Operator Description
str Determine whether the specified string is empty
str1==str2 0 if the strings are equal
-n str Determine whether the string is a non-empty string, if it is an empty string, the test result is 0
-z str If it is an empty string, the test result is 0

1.5 Numerical test

Similar to strings, numerical tests also have two forms of syntax

Operator Description
n1 -eq n2 Compare whether n1 is equal to n2, if it is equal, the test result is 0
n1 -ne n2 If n1 is not equal to n2, the test result is 0
n1 -lt n2 Compare whether n1 is less than n2, if n1 is less than n2, the test result is 0
n1 -le n2 Compare whether n1 is less than or equal to n2, if n1 is less than or equal to n2, the test result is 0
n1 -gt n2 Compare whether n1 is greater than n2, if n1 is greater than n2, the test result is 0
n1 -ge n2 Compare whether n1 is greater than or equal to n2, if n1 is greater than or equal to n2, the test result is 0

1.5 logical operators

Logical operators are equivalent to &&, ||,! In c language.

Operator Description
!exp Logical negation, if the conditional expression is false, the result of the operator is true
exp1 -a exp2 Logical AND, when the values ​​of conditions exp1 and exp2 are both true, the entire expression is true
exp1 -o exp2 Logical OR, one of the values ​​of the conditional expressions exp1 and exp2 is true, and the entire expression is true
(exp) Parentheses, group expressions, get the result first

Источник

While statement

While can also implement loops, her syntax is as follows:

while condition do command done

In addition, we often use the while statement to achieve human-computer interaction (user input), for example:

#!/bin/bash 
echo 'Press to exit' echo -n 'Enter the title of your favorite book: ' while read book do echo "Yes! $book is a good book" done output: Press down drop out Enter the title of your favorite book: linux is good Yes! linux is good is a good book

Other keywords of the loop

In the following example, an infinite loop is shown, and the case condition is used to exit/terminate a loop operation.

#!/bin/bash while : #Open infinite loop do echo -n "Enter a number between 1 and 5:" read aNum case $aNum in 1|2|3|4|5) echo "The number you entered is $aNum!" ;; *) echo "The number you entered is not between 1 and 5! Game over" break # break means to exit the loop; if the continue keyword is used, the loop is ended and the content after the loop continues ;; esac done

Источник

Читайте также:  Linux mint установка приложений windows
Оцените статью
Adblock
detector