Shell 操作符

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…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章