shell腳本循環與函數
for 循環
for 循環用于一次性讀取多個信息,逐一對信息進行操作處理,特別適合處理有范圍的數據
語法
for 變量名 in 取值列表
do命令序列
done
批量創建用戶
#!/bin/bashtouch /root/users.txt
echo aka blues cloe dio foks > /root/users.txtread -p "給用戶設置密碼:" PASSWD
for i in `cat /root/users.txt`
douseradd $iecho "$PASSWD" | passwd --stdin $iecho "$i"用戶創建成功
done
循環輸出
#!/bin/bash
echo "自增"
for ((i=1;i<=10;i++))
doecho $i
doneecho "自減"
for ((i=10;i>=1;i-=2))
doecho $i
done
計算數字1-100的整數和
#!/bin/bash
a=0
for ((i=1;i<=100;i++))
doa=$(($i+$a))
done
echo "1-100所有整數和為 $a"
while 循環
while 循環重復測試某個條件,只要條件成立就反復執行
顯示 0-10 的所有整數
#!/bin/bash
a=0
while [ $a -le 10 ]
doecho $alet a++
done
計算 1-100 的整數和
#!/bin/bash
a=0
b=1
while [ $b -le 100 ]
doa=$(($a+$b))let b++
done
echo "1-100的和: $a"
猜數字游戲
隨機猜1000以內的數字
#!/bin/bash
num=$(expr $RANDOM % 1000)
a=0
echo "猜數字的范圍為0-999的整數"
while true
do
read -p "請輸入你猜的數:" nlet a++
if [ $n -gt $num ];thenecho "猜的數字過大"
elif [ $n -lt $num ];thenecho "猜的數字過小"
elseecho "恭喜你猜對了"echo "你猜的次數為 $a"exit
fi
done
until 循環
until 循環重復測試某個條件,只要條件不成立就反復執行(與while相反)
求1-100的整數和
#!/bin/bash
a=0
b=1
until [ $b -gt 100 ]
doa=$(($a+$b))let b++
done
echo "1-100的和: $a"
雙for循環
在一個for循環內部再加一個for循環
#!/bin/bash
for ((a=1;a<=3;a++))
doecho "a=$a"for((b=1;b<=5;b++))doecho "b=$b"
done
done
a每加1,b就循環一次
break 和 continue
- break:跳出當前循環
- continue:中止某次循環,不會完全終止整個命令
#!/bin/bash
for ((a=1;a<=3;a++))
doecho "a=$a"for((b=1;b<=5;b++))doif [ $b -eq 3 ];thenbreak #當b=3時,就停止運行fiecho "b=$b"
done
done
#!/bin/bash
for ((a=1;a<=10;a++))
do
if [ $a -gt 4 ]&&[ $a -lt 8 ];thencontinue #當a大于4小于8時,停止循環
fiecho "$a"
done
用shell腳本寫一個九九乘法表
#!/bin/bash
for ((i=1; i<=9; i++))
dofor ((o=1; o<=i; o++))dolet c=$i*$oecho -n "$i*$o=$c "doneecho ""
done
[root@xieyuhui ~]# bash a.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
函數
函數將命令序列按格式寫在一起,可重復使用。
函數定義格式
# 方式一
function 函數名 {命令序列
}# 方式二
函數名() {命令序列
}
使用 return
退出函數并返回一個退出值(0-255),通過 $?
顯示
#!/bin/bash
function x {read -p "請輸入任意一個整數:" nreturn $[$n*2]
}
x
echo "$?"
函數傳參
函數變量的作用范圍(局部變量以及全局變量)
函數在shell腳本中僅在當前shell環境中有效
shell腳本中變量默認全局有效
將變量限定一個函數的內部local,即局部變量
#!/bin/bashnum1 (){num=$[ $1+$2 ]echo $num
}
num1 $1 $2
局部變量與全局變量
將變量限定在函數內部使用local命令
即這個local的變量僅僅在當前的函數內有效,在別的函數中無效。
#!/bin/bashabc (){echo "為經過local的變量$i" #此時局內變量未定義,因此是全局變量i=9local ii=6echo "函數內的變量$i" #此時局內變量已定義,因此為i=6
}
i=9
abc
echo "外面變量$i" #局內定義的變量只能在定義的函數中使用,因此此處為全局變量
遞歸函數
遞歸函數調用自身
#!/bin/bashfunction a(){if [ $1 -eq 1 ];thenecho 1elselocal temp=$[$1-1]local result=`a $temp`echo $[result * $1 ]fi
}read -p "輸入一個值:" va
result=`a $va`
echo "階乘的值為: $result"
函數庫
將常用函數集中放在一個腳本中,在需要使用時直接調用即可
先寫一個函數庫
[root@xieyuhui ~]# vim hsk.sh
#!/bin/bash
jia(){result=$[ $1 + $2 ]echo "$result"
}jian(){result=$[ $1 - $2 ]echo "$result"
}cheng(){result=$[ $1 * $2 ]echo "$result"
}chu(){if [ $2 -ne 0 ];thenresult=$[ $1 / $2 ]echo "$result"elseecho "除法中分母不能為0"fi
}
[root@xieyuhui ~]# chmod +x hsk.sh
[root@xieyuhui ~]# vim aa1.sh
#!/bin/bash
. /root/hsk.shread -p "請輸入第一個數字:" n
read -p "請輸入第二個數字:" mresult1=`jia $n $m`
result2=`jian $n $m`
result3=`cheng $n $m`
result4=`chu $n $m`echo "兩數之和為: $result1"
echo "兩數之差為: $result2"
echo "兩數之積為: $result3"
echo "兩數之商為: $result4"