Shell 操作符
Summary
Operator
定義 | 說明 | 舉例 | 備註 |
---|---|---|---|
普通變量 | |||
$((運算式)) | result=$(((2+5)*5)) | ||
$[運算式] | result=$[(2+5)*5] | ||
`expr 運算式` | tmp=`expr 2 + 5` | 注意必須有空格 | |
result=`expr $tmp \* 5` | 注意*號要轉義 | ||
Other | |||
if [ condition ] | if [ -e ./test.sh ] | 注意condition兩側有空格 |
Practice
#!/bin/bash
result=$(((2+5)*5))
echo "result = $result"
result=$[(2+5)*5]
echo "result = $result"
tmp=`expr 2 + 5`
result=`expr $tmp \* 5`
echo "result = $result"
[root@propel tmp]# ./test.sh
result = 35
result = 35
result = 35
#!/bin/bash
if [ -e ./test.sh ]
then
echo "existing...."
fi
[root@propel tmp]# ./test.sh
existing…