4.1.3 Shell Process Control Statements
If condition judgment Statement
If ( expression ) #if (Variable in Array)
Statement 1
Else
Statement 2
Fi
Case one, test if the directory exists, does not exist then new (note that space must be between the brackets)
#!/bin/sh
#judge direxist
if [! -d/data/20140515];then
mkdir-p/data/20140515
Else
echo "This DIR isexist,please exit ..."
Fi
Logical operator parsing:
- F determine if a file exists Eg:if [-f filename]
- D determine if a directory exists Eg:if [-D dir]
-eq equals applies To: integer comparison
-ne not equal to: integer comparison
-lt less than applied to: integer comparison
-GT greater than applied to: integer comparison
-le less than or equal to: integer comparison
-ge greater than or equal to: integer comparison
-A Both sides were established ( and ) Logical Expression – a Logical Expressions
- o Unilateral Establishment ( or ) Logical Expression – o Logical Expressions
- Z empty string
Case three, multiple condition test judgment
#!/bin/sh
scores=80;
if [[$scores-GT 85]]; Then
echo "Very good!";
elif [[$scores-GT 75]]; Then
echo "good!";
elif [[$scores-GT 60]]; Then
echo "pass!"; Elseecho "No pass!";
Fi
Loop statement for
for variables inch string
Do
Statement 1
Done
case one, print seq Multiple Numbers
#!/bin/sh
For i in ' seq 15 '
Do
echo "NUMis $i"
Done
case two, find the relevant Log , and then bulk pack
#!/bin/sh
For i in ' Find/var/log-name "*.log" '
Do
TAR–CZF 2014log.tgz $i
Done
Looping Statements while
while conditional Statements
Do
Statement 1
Done
Case One, while Conditional Judgment Number
#!/bin/sh
I=1;
while [[$i-lt]];d o
echo $i;
((i++));
Done
case Two, while read a file row by line
#!/bin/sh
While Read line
Do
Echo $line;
done</etc/hosts
Case SELECT statement
Case $arg in
PATTERN1)
Statement 1
;;
PATTERN2)
Statement 2
;;
*)
Statement 3
;;
Esac
Case one, create a selection parameter script
#!/bin/sh
Case $ in
Monitor_log)
Monitor_log
;;
Archive_log)
Archive_log
;;
* )
echo "Usage:{$0 Monitor_log | Archive_log |help} "
;;
Esac
This article from "Do not forget Beginner's mind" blog, reproduced please contact the author!
Shell Script Process Control