The shell can use "[]" to judge data, and "[]" is basically consistent with test.
When using "[]" pay attention to three points (from Brother Bird):
1, in the brackets "[]" in each component needs to have the space bar to split.
2, in the brackets inside the variables, preferably in double quotes around.
3. Constants in brackets, preferably enclosed in single or double quotes
The structure of the IF Else statement is:
if [condition]; Then
#some statements
elif [condition]; Then
#some statements
Else
#some statements
Fi
Here is a small example, where ["$yn" = = "Y"] | | ["$yn" = = "Y"] is equivalent to ["$yn" = = "Y"-o "$yn" = = "Y"], "o" means "or", connecting two judgements.
#!/bin/bash
#read user ' s input and do something
read-p "please input (y/n):" Yn
if ["$yn" = = "Y"] | | ["$yn" = = "Y"]; Then
echo "OK, you say yes."
elif ["$yn" = = "N"] | | ["$yn" = = "n"]; Then
echo "NO? Goodbye. "
else
echo "I don ' t know what you say"
fi
#test whether a directory exist,if not,create it
if [! d] Keys Toneclient "]; Then
mkdir keystoneclient
fi
The basic structure of the while statement is:
While [condtion]
Todo
#some statements
Done
Here is a small example:
#!/bin/bash while
["$yn"!= "yes"-a "$yn"!= "yes"
does
read-p "please input Yse/Yes to stop this loo P. "Yn
do
echo" OK, you input the correct answer.
Adding a sleep statement in the while statement constructs a simple timer, and the sleep unit is seconds. For the following example, print the time every second.
#!/bin/bash while
[1]
does
date sleep
1
done