0.創建shell腳本 ,及運行方法
創建:vim xxx.sh 腳本的第一行爲: #/bin/bash
運行: bash xxx.sh
1.變量的定義
#!/bin/bash
#變量的定義
#注意:不能像其他編程語言那樣留有空格
A=123
B=456
#引用,echo是輸出命令
$A
$B
echo "the value of A is $A"
#數字變量
$0 爲腳本的名稱
$1,...,$n 爲執行腳本的外部參數,如 bash xxx.sh 123 456 789
則腳本獲取到 $1=123 $2=456 $3=789
2.if條件判斷
#!/bin/bash
A= 123
if (($A>100));then
echo "the value of A is $A"
else
echo "error"
fi
3.For循環
#!/bin/bash
# ` `命令替換,先執行` `的內容 ,等價於 $(seq 1 15)
for i in `seq 1 15`
do
echo "$i"
done
#--------------------
j=0
for ((i=1;i<=100;i++))
do
let j=`expr $i+$j`
done
4.While循環
#!/bin/bash
i=0
#注意空格
while [ $i -le 10 ]
do
echo "$i"
i++
done
5.Until
#!/bin/bash
i = 0
Until [ $i -ge 10 ]
do
echo "$i"
i++
done
5.邏輯符號
大於 -gt greater than
小於 -lt less than
大於或等於 -ge greater than or equal
小於或等於 -le less than or equal
不相等 -ne not euqal
6.計算
#!/bin/bash
#shell默認變量都爲字符串,要做數值計算,有幾個方式
#1. expr
a=10
b=100
expr $a + $b
#注意空格
#2.$(())
$(($a+$b))
#3.$[]
$[$a+$b]
#$4.let
let c=$a+$b