1. 数值运算
var=$((expression))
total=$(($firstnu*secnu))
echo $total;
2. test 命令可用于判断某文件是否存在或其属性(如是否是文件或目录)
fredchen@fredchen:~$ test -e dd || echo "this file does not exsist"
this file does not exsist
fredchen@fredchen:/dev$ test -b ttyze
fredchen@fredchen:/dev$ test -b ttyze || echo "not a block dev"
not a block dev
fredchen@fredchen:/dev$ test -b sda && echo "it is a block dev"
it is a block dev
test ! x file //! 用于条件求反,当file不具x属性时,返回true
3. [] 同样可以用于条件判断
read -p "please make your choice: " choice
[ "$choice" == "y" ] && echo "you accept it!" && exit 0
[ "$choice" == "n" ] && echo "you reject it!" && exit 0
echo "we can not determine your choice!" && exit 0
total=$(($firstnu*secnu))
echo $total;
2. test 命令可用于判断某文件是否存在或其属性(如是否是文件或目录)
fredchen@fredchen:~$ test -e dd || echo "this file does not exsist"
this file does not exsist
fredchen@fredchen:/dev$ test -b ttyze
fredchen@fredchen:/dev$ test -b ttyze || echo "not a block dev"
not a block dev
fredchen@fredchen:/dev$ test -b sda && echo "it is a block dev"
it is a block dev
test ! x file //! 用于条件求反,当file不具x属性时,返回true
3. [] 同样可以用于条件判断
read -p "please make your choice: " choice
[ "$choice" == "y" ] && echo "you accept it!" && exit 0
[ "$choice" == "n" ] && echo "you reject it!" && exit 0
echo "we can not determine your choice!" && exit 0
4. shell script的默认变量 $0 $1 $2 $3 ...
$0 = script name
$1 = para1
$2 = para2
5. if判断
if [ expression1 ] || [ expression2 ]; then //semicolon and then
statement1 //no semiconlon
statement2 //no semiconlon
fi
if [ expression1 ] && [ expression2 ]; then //semicolon and then
statement1 //no semiconlon
statement2 //no semiconlon
else [ expression1 ] || [ expression2 ]; then //semicolon and then
statement3 //no semiconlon
statement4 //no semiconlon
fi
if [ expression1 ] && [ expression2 ]; then //semicolon and then
statement1 //no semiconlon
statement2 //no semiconlon
elif [ expression1 ] || [ expression2 ]; then //semicolon and then
statement3 //no semiconlon
statement4 //no semiconlon
else
statement3 //no semiconlon
statement4 //no semiconlon
fi
$1 = para1
$2 = para2
5. if判断
if [ expression1 ] || [ expression2 ]; then //semicolon and then
statement1 //no semiconlon
statement2 //no semiconlon
fi
if [ expression1 ] && [ expression2 ]; then //semicolon and then
statement1 //no semiconlon
statement2 //no semiconlon
else [ expression1 ] || [ expression2 ]; then //semicolon and then
statement3 //no semiconlon
statement4 //no semiconlon
fi
if [ expression1 ] && [ expression2 ]; then //semicolon and then
statement1 //no semiconlon
statement2 //no semiconlon
elif [ expression1 ] || [ expression2 ]; then //semicolon and then
statement3 //no semiconlon
statement4 //no semiconlon
else
statement3 //no semiconlon
statement4 //no semiconlon
fi