-
#! bin/bash
read -t 30 -p 'please input num1:' num1
read -t 30 -p 'please input num2:' num2
read -t 30 -p 'please input ope:' ope
# $num1為空
if [-z $num1]
then?
echo 'the num1 is null'
exit 1
fi
if [-z $num2]
then?
echo 'the num2 is null'
exit 2
fi
if [-z $ope]
then?
echo 'the ope is null'
exit 3
fi
#把輸出的num1中的數字全部替換成空
test_num1=$(echo $num1 | sed 's/[0-9]//g')
test_num2=$(echo $num2 | sed 's/[0-9]//g')
if [-n $test_num1]
then
echo 'the num1 is not number'
exit 4
fi
if [-n $test_num2]
then?
echo 'the num2 is not number'
exit 5
fi
if ["$ope" == '+']
then
result = $(($num1+$num2))
elif["$ope" == '-']
then
result=$(($num1-$num2))
elif["$ope" == '*']
then
result=$(($num1*$num2))
elif["$ope"=='/']
then result=$(($num1/$num2))
else
echo 'the ope is not reconize'
exit 10
fi
echo "$num1$ope$num2 +'='+$result"
查看全部 -
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 多分支if條件語句
if [條件判斷式1]?
????then
????????code1
elif [條件判斷式2]
????then????
????????code 2
elif ...
else
????else code
fi
查看全部 -
read? -t 30? -p "please input a dir:" dir
-- 等待30秒,輸出提示信息
if [-d "$dir"]
????then?
????????????echo "yes"
????else?
????????????echo "no"
fi
查看全部 -
test -e /root/install.log
[-e /root/install.log]
[-d /root] && echo "yes" || ecjho "no"
按照文件類型進行判斷
查看全部 -
判斷分區使用率
#!?/bin/bash test=$(df?-h?|?grep?sda5?|?awk?'{print?$5}')?|?cut?-d?'%'?-f?1) if?["$test"?-ge?"80"] ????then ????????echo?"is?full!" fi
查看全部 -
> 輸出覆蓋? >>輸出追加
查看全部 -
grep -v grep 取出除grep以外的行
查看全部 -
read -t 30 -p “please input a dir : " dir
-t 等待30s
-p打印輸出信息
查看全部 -
grep 取包含sda5的一行
awk 打印出第五列的內容
cut 以%為分隔符 取出第一列
查看全部 -
[-n "$aa" -a "$aa" -gt 23]
查看全部 -
[-z $name] && echo yes || echo no
["$aa" == "$bb"]? ?# 比較字符串,注意,這里的$aa 和 $bb加了雙引號
查看全部 -
如:? [22 -eq 23]
查看全部 -
ln?/root/student.txt?/tmp/shu.tt #給student.txt創建硬鏈接
查看全部 -
[-w /root/install.log]
只要該文件擁有該權限,不會判斷是所屬者或者所屬組是否擁有。
查看全部 -
判斷的兩種格式:
test -e /root/install.log
[-e /root/install.log]
[-d /root] && echo "yes" || ecjho "no"
查看全部
舉報