Shell operators and basic statements
1. Operators
Operator |
Describe |
Example |
File comparison Operators |
-e filename |
True if filename exists |
[-e/var/log/syslog] |
-D filename |
True if filename is a directory |
[-d/tmp/mydir] |
-F filename |
True if filename is a regular file |
[-f/usr/bin/grep] |
-L filename |
True if filename is a symbolic link |
[-l/usr/bin/grep] |
-R filename |
True if filename is readable |
[-r/var/log/syslog] |
-W filename |
True if filename is writable |
[-w/var/mytmp.txt] |
-X filename |
True if filename is executable |
[-l/usr/bin/grep] |
Filename1-nt filename2 |
If filename1 is newer than filename2, it is true |
[/tmp/install/etc/services-nt/etc/services] |
Filename1-ot filename2 |
If filename1 is older than filename2, it is true |
[/boot/bzimage-ot Arch/i386/boot/bzimage] |
String comparison operators (note the use of quotation marks, which is a good way to prevent whitespace from disturbing the code) |
-Z String |
True if string length is zero |
[-Z "$myvar"] |
-N String |
True if string length is nonzero |
[-N "$myvar"] |
string1 = string2 |
True if string1 is the same as string2 |
["$myvar" = "One of the three"] |
String1! = string2 |
True if string1 is different from string2 |
["$myvar"! = "one of the three"] |
Arithmetic comparison operators |
Num1-eq num2 |
Equals |
[3-eq $mynum] |
Num1-ne num2 |
Not equal to |
[3-ne $mynum] |
Num1-lt num2 |
Less than |
[3-lt $mynum] |
Num1-le num2 |
Less than or equal to |
[3-le $mynum] |
NUM1-GT num2 |
Greater than |
[3-gt $mynum] |
Num1-ge num2 |
Greater than or equal to |
[3-ge $mynum] |
Arithmetic operators
+-*/% indicates subtraction and take-rest operations
+ = = *=/= with C language meaning
Bitwise operators
> >>= means move one operation left and right
& &= | |= indicates bitwise AND, bit, or operation
~ ! Represents a non-operational
^ ^= represents XOR or manipulation
Relational operators
= = = = = Greater than, less than, greater than or equal, less than equals, equals, not equal to operation
&& | | Logical AND logical OR operational
Logical Expressions
Logical Non! The opposite of the conditional expression
if [! expression]
if [!-D $num] If there is no directory $num
Logic and –a Side- by-side of conditional expressions
If [expression 1–a-expression 2]
logical OR-o a conditional expression or
If [expression 1–o-expression 2]
2. Basic statements
Dellines=$[${filelines}-3] #使用 "" to the value of the variable filelines-3 to Dellines
1) for Loop
Example 1:
/bin/bash
For I in $ (seq) #将1-10 Number one assignment to variable i
Do echo $i #输出i
Done
Example 2:
#!/bin/bash
For ((i=1;i<=10;i++)) #在1-10, starting from 1, once plus 1
Do Echo $i
Done
Example 3:
#!/bin/bash
For i in a B c #指定循环内容
Do Echo $i
Done
Example 4:
#!/bin/bash
For i in ' ls ' #将ls命令查看到的文件名一次赋给i (under current directory)
Do Echo $i
Done
Example 5: Find out 1-100 numbers that can be divisible by 3
"1" uses the For
#!/bin/bash
Clear #清屏
For ((i=1;i<100;i++)) or for i in ' seq 100 ' or for I in {1..100}
Do if ((i%3==0)) #i除以3等于0
Then
Echo $i
Continue #继续下次循环
Fi
Done
"2" using while
#!/bin/bash
Clear
I=1
while (($i <100)) #条件i <100
Do if ((i%3==0)) #i除以3等于0
Then
Echo $i
Fi
i=$ (($i + 1)) #i加1
Done
2) While statement
While expression
Do
Command
Command
```
Done
Example 1:
#!/bin/bash
Clear
I=1
while (($i <=5)) #条件: i<=5
Do
Echo $i
Let "i++" #i加1
Done
3) If statement
If condition Then Command Else Command Fi |
If condition
Then
Command
Elif conditions
Command
Fi
Example 1:
echo "Input your choice:" Read ANS |
Note: "Read variable" reads the value of ANS from the keyboard
if [$ANS] Then Echo No empty Else Echo empth Fi |
Note: If [$ANS] determines that the string variable is non-null, equivalent to if [-N $ANS]
Grep-w
[email protected] mnt]# cat 1.txt
1
12
123
[[email protected] mnt]# grep 1 1.txt
1
12
123
[Email protected] mnt]# grep-w 1 1.txt
1
This article is from the "True Water No Fragrance" blog, please be sure to keep this source http://chengyanli.blog.51cto.com/11399167/1846776
Shell Basic Statement