一、變數 1.變數的命名規則:以字母或下劃線開頭,後面跟數字,字母或下劃線,最好不要隨便命名,要做到看見變數名能猜出其含義 2.變數賦值: x=100 echo $x 刪除變數:unset x 3.定義變數名的邊界用大括弧 [root@bogon ~]# egon_salary=20000[root ...
一、變數
1.變數的命名規則:以字母或下劃線開頭,後面跟數字,字母或下劃線,最好不要隨便命名,要做到看見變數名能猜出其含義
2.變數賦值: x=100
echo $x
刪除變數:unset x
3.定義變數名的邊界用大括弧
[root@bogon ~]# egon_salary=20000
[root@bogon ~]# echo ${egon_salary}yuan
20000yuan
4.bash中不必聲明數據類型,預設都是字元型
二、運算符
1.算術運算符:+ - * / %
[root@bogon ~]# echo $[5%2]
1
2.賦值運算符:=,+=,-=,*=,/=,%=
[root@bogon ~]# x=10
[root@bogon ~]# ((x+=1))
[root@bogon ~]# echo $x
11
3.關係運算符:<,>,!=,==,>=,<=,||,&&
關係運算符常與(( ))連用,[]可以達到同樣的結果,但(( ))不能判斷一個文件的類型,判斷文件類型必須要用到[],[]又和test命令效果一樣
用$?查看命令執行結果,結果為0代表真,非0代表假
[root@bogon ~]# x=10
[root@bogon ~]# ((x>=8))
[root@bogon ~]# echo $?
0
4.shell里的計算器
之前說過用$[]可以進行一些簡單的運算,但是如果涉及到小數的運算,就需要用到shell裡面的計算器了
首先要安裝軟體,yum install -y bc
[root@bogon ~]# res=$(echo 'scale=2;1/3' |bc -l |cut -d'.' -f2)
[root@bogon ~]# echo ${res}%
33%
5.test 命令測試
test
-n str 字元串長度不為零
-z str 字元串長度為零
-b 文件存在且為塊文件
-d 文件存在且為目錄文件
-e 文件存在
-f 文件存在且為普通文件
-h 文件存在且為鏈接文件(同 -L)
-s 文件存在且大於零位元組
文件之間的比較
file1 -nt file2 file1 的創建時間比file2晚
file1 -ot file2 file1 的創建時間比file2早
整數之間的比較
int1 -ne int2 int1和int2不相等
int1 -eq int2 int1和int2 相等
int1 -lt int2 int1小於int2
int1 -le int2 int1小於等於int2
int1 -gt int2 int1大於int2
int1 -ge int2 int1大於等於int2
字元串之間比較
str1 = str2 str1和str2相等
str1 !=str2 str1和str2不相等
表達式之間的比較
expression1 -a expression2 表達式1與表達式2都為真
expression1 -o expression2 表達式1或表達式2為真
6.測試舉例
數字比較測試:
[root@bogon ~]# [[ 2 > 1 ]] [root@bogon ~]# echo $? 0 [root@bogon ~]# ((20>10)) [root@bogon ~]# echo $? 0 [root@bogon ~]# ((20<10)) [root@bogon ~]# echo $? 1
字元串測試
[root@bogon ~]# [ "abc" = "abc" ] [root@bogon ~]# echo $? 0 [root@bogon ~]# [[ "abc" = "abc" ]] [root@bogon ~]# echo $? 0 [root@bogon ~]# (("abc" = "abc")) [root@bogon ~]# echo $? 1
[root@bogon ~]# [[ a = a && 1 < 2 ]] [root@bogon ~]# echo $? 0 [root@bogon ~]# [[ a = a && 1 < 2 ]] [root@bogon ~]# echo $? 0
[root@bogon ~]# (( a = a || 1 > 2 )) [root@bogon ~]# echo $? 1 [root@bogon ~]# [[ a = a || 1 > 2 ]] [root@bogon ~]# echo $? 0
單純比較數字,用(( ))
除了單純數字之外的比較,用[[ ]]
三、流程式控制制
1.if 分支機構
1)驗證用戶賬號密碼:
input your name : zhangcan input password : 123 login successful [root@bogon ~]# ./usertest.sh input your name : hha input password : hag user or password error
#! /bin/bash user='zhangcan' password='123' read -p 'input your name : ' name read -p 'input password : ' code if [ $name = $user -a $code = $password ];then echo 'login successful' else echo 'user or password error' fi ~
2)判斷成績檔次
#!/bin/bash #根據用戶輸入的成績,判斷所屬檔次,並輸出給用戶 read -p 'input your score : ' score if [ $score -ge 90 ];then echo '優秀' elif [ $score -ge 70 -a $score -lt 90 ];then echo '良好' elif [ $score -ge 60 -a $score -lt 70 ];then echo '及格' elif [ $score -lt 60 ];then echo '較差' fi
2.while迴圈
while(條件)
do
命令
done
示例:判斷用戶輸入的文件是何種類型
#!/bin/bash while : do read -p 'input your file : ' file if [ -z $file ];then continue else break fi done if [ -f $file ];then echo "$file is regular file" elif [ -b $file ];then echo "$file is block file" elif [ -d $file ];then echo "$file is directory file" else echo "$file type unkonw" fi
3.for迴圈
for i in {1..10} #in後面不一定是數字,只要是有返回結果的命令都可以
do
echo $i
done
示例1:寫一個腳本,測試子網內可以使用的IP
#!/bin/bash for i in {1..50} do ping -c1 192.168.16.$i &> /dev/null # -c1表示ping一次 if [ $? -ne 0 ];then echo "192.168.16.$i successful" echo "192.168.16.$i" >> ~/ipavailable.txt fi done ~
示例2:統計/dev下每種文件類型的數量
#!/bin/bash dir='/dev' for i in $(ls $dir) do if [ -h $dir/$i ];then ((link+=1)) elif [ -f $dir/$i ];then (( rfile+=1)) elif [ -d $dir/$i ];then ((directory+=1)) elif [ -b $dir/$i ];then (( block+=1 )) else (( typeunknow+=1)) fi done echo 'block' $block echo 'regular file' $rfile echo 'directory' $directory echo 'link' $link echo 'unknow' $typeunknow
4.嵌套迴圈
示例1:輸出一個九九乘法表
#!/bin/bash for ((i=1;i<=9;i++)) do for ((j=1;j<=i;j++)) do echo -n "$i*$j=$[$i*$j]" done echo done
示例2:驗證用戶登陸賬號密碼,登陸成功後可以執行命令,當輸入quit時退出
#!/bin/bash user='zhangcan' password='123' tag=true while $tag do read -p 'input your name : ' name read -p 'input your password : ' code if [[ $name = $user ]] && [[ $code = $password ]];then echo 'login successful' while $tag do read -p '>>: ' cmd if [[ $cmd = 'quit' ]];then tag=false else $cmd fi done fi done