Basic Shell Programming __ programming

Source: Internet
Author: User
if else and greater than, less than, equal to logical expressionIf ...; Then
....
Elif ...; Then
....
Else
....
fi [F "somefile"]: judge whether it is a file
[-X "/bin/ls"]: Determine if/bin/ls exists and has executable permissions
[-N ' $var]: Determine if the $var variable has a value
["$a" = "$b"]: Determine if $a and $b are equal
-r file user readable as True
-W file user can write as true
-X file user can execute as true
-F file is true for regular files
-D file is true for directory
-C file is true for character special files
-B file is a block special file is True
-S file file non-0 o'clock is true
-T file True when the device specified by the file descriptor (default is 1) is a terminal
#########################################################
Integer comparisons
-eq equals, such as: if ["$a"-eq "$b"]
-ne is not equal to, such as: if ["$a"-ne "$b"]
-GT is greater than, such as: if ["$a"-gt "$b"]
-ge is greater than or equal to, such as: if ["$a"-ge "$b"]
-lt is less than, such as: if ["$a"-lt "$b"]
-le is less than or equal, such as: if ["$a"-le "$b"]

< less than (requires double brackets), such as: (("$a" < "$b")
<= is less than or equal (requires double parenthesis), such as: (("$a" <= "$b")
> Greater than (requires double brackets), such as: (("$a" > "$b")
>= is greater than or equal (requires double parenthesis), such as: (("$a" >= "$b")

String comparisons

= equals, such as: if ["$a" = "$b"]
= = equals, such as: if ["$a" = = "$b"], and = equivalent

Note: the behavior in [[]] and [] is different in some cases:

[[$a = = z*]] # If $a starts with "Z" (pattern match) then it will be true
[[$a = = "z*"]] # if $a equals z* (character match), then the result is true

[$a = = z*] # File globbing and word splitting will occur
["$a" = "z*"] # if $a equals z* (character match), then the result is true



For i in {1..10}
Todo
Echo $i
Done

#详细列出 (not many characters and few items)
For File in 1 2 3 4 5
Todo
Echo $File
Done

#对存在的文件进行循环
For shname in ' LS *.sh '
Todo
Name= ' echo ' $shname | Awk-f. ' {print '} '
Echo $name
Done

#查找循环 (LS data is too large to use this method)
For Shname in ' find. -type f-name "*.sh"
Todo
Name= ' echo ' $shname | awk-f/' {print $} '
Echo $name
Done

# (Grammar loop--a bit like C syntax, but remember double bracket
For ((i=1;i<100;i++))
Todo
if ((i%3==0))
Then
Echo $i
Continue
Fi
Done

#seq形式 starting from 1
For i in ' SEQ 100 '
Todo
if ((i%3==0))
Then
Echo $i
Continue
Fi
Done

#while循环注意为方括号 [], and note that spaces
Min=1
max=100
While [$min-le $max]
Todo
Echo $min
min= ' expr $min + 1 '
Done

#双括号形式, the internal structure is somewhat like the syntax of C, note assignment: i=$ (($i + 1))
I=1
while (($i <100))
Todo
if (($i%4==0))
Then
Echo $i
Fi
i=$ (($i + 1))
Done


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.