for循環和select循環
本篇主要介紹内容:
- for循環的定義及使用
- select循環的用途
for循環
第一種
for循環語句為變量取值型,文法結構如下:
for 變量名 in 變量取值清單
do
指令...
done
提示:
在此結構中“in變量取值清單”可以省略,省略時相當于in“[email protected]”,也就是使用for i就相當于使用for i in“[email protected]”。
在這種for循環語句文法中,for後面的變量名取自變量清單中的元素,每次取一個,并且變量取值清單以空格區分。
第二種
for循環語句稱為for循環語句,文法結構如下:
for((exp1;exp2;exp3))
do
指令..
done
for關鍵字後的雙括号内是三個表達式,第一個是變量初始化(例如:i=0),第二個為變量的範圍(例如:i<100),第三個為變量自增或自減(例如:i++)。當第一個表達式的初始化值符合第二個變量的範圍時,就進入循環執行;當條件不滿足時就退出循環。
總結:
1)如果希望程式持續運作,則多用while,包括守護程序。
2)如果是有限次循環,則多用for,實際工作中使用for的機會更多。
實踐-豎向列印數字
豎向列印5、4、3、2、1五個數字
[[email protected] shell]# cat 11_2_1.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_2_1.sh
#Description: This is a test script.
#***********************************************
for num in 5 4 3 2 1
do
echo $num
done
[[email protected] shell]# sh 11_2_1.sh
5
4
3
2
1
第二種,使用了{}生成數字序列的方法
[[email protected] shell]# cat 11_2_2.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_2_2.sh
#Description: This is a test script.
#***********************************************
for n in {5..1}
do
echo $n
done
[[email protected] shell]# sh 11_2_2.sh
5
4
3
2
1
第三種,采用seq生成數字序列的用法
[[email protected] shell]# cat 11_2_3.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_2_3.sh
#Description: This is a test script.
#***********************************************
for n in $(seq 5 -1 1)
do
echo $n
done
[[email protected] shell]# sh 11_2_3.sh
5
4
3
2
1
實踐-批量修改檔案字尾
将下面的txt檔案改為log檔案
[[email protected] shell]# ls -l stu*
-rw-r--r-- 1 root root 2 3月 7 15:59 stu_102999_1.txt
-rw-r--r-- 1 root root 2 3月 7 15:59 stu_102999_2.txt
-rw-r--r-- 1 root root 2 3月 7 15:59 stu_102999_3.txt
-rw-r--r-- 1 root root 2 3月 7 15:59 stu_102999_4.txt
提示:通過此題的解答可以學習到sed、awk、rename、mv等指令的實戰應用。
我們先将上面的檔案名都加上“_finished”,使用awk指令,其中
,
表示空格,
$0
表示目前檔案名,
$1
表示按
.
切割後的前半段,
$2
表示按
.
切割後的後半段
[[email protected] shell]# ls stu*|awk -F "." '{print "mv", $0,$1"_finished."$2}'
mv stu_102999_1.txt stu_102999_1_finished.txt
mv stu_102999_2.txt stu_102999_2_finished.txt
mv stu_102999_3.txt stu_102999_3_finished.txt
mv stu_102999_4.txt stu_102999_4_finished.txt
[[email protected] shell]# ls |awk -F "." '{print "mv",$0,$1"_finished.","$2"}' |bash
第一種:采用shell腳本、for循環加sed的方法
[[email protected] shell]# cat 11_5_1.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_5_1.sh
#Description: This is a test script.
#***********************************************
for file in `ls stu*.txt`
do
mv $file `echo $file|sed 's/_finished//g'`
done
[[email protected] shell]# sh 11_5_1.sh
[[email protected] shell]# ls stu*
stu_102999_1.txt stu_102999_2.txt stu_102999_3.txt stu_102999_4.txt
第二種:通過專業的改名指令rename來實作,rename第一個參數為待替換字元串,第二個參數為替換後的值,第三個表示替換哪些檔案
[[email protected] shell]# ls stu*
stu_102999_1_finished.jpg stu_102999_2_finished.jpg stu_102999_3_finished.jpg stu_102999_4_finished.jpg
[[email protected] shell]# rename "_finished" "" *.jpg
[[email protected] shell]# ls stu*
stu_102999_1.jpg stu_102999_2.jpg stu_102999_3.jpg stu_102999_4.jpg
實踐-列印九九乘法表
[[email protected] shell]# cat 11_8_1.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_8_1.sh
#Description: This is a test script.
#***********************************************
COLOR='\E[47;30m'
RES='\E[0m'
for num1 in `seq 9`
do
for num2 in `seq 9`
do
[ $num1 -ge $num2 ] && {
if (((num1*num2)>9))
then
echo -en "${DOLOR}${num1}x${num2}=$((num1*num2))$RES "
else
echo -en "${COLOR}${num1}x${num2}=$((num1*num2))$RES "
fi
}
done
echo " "
done
[[email protected] shell]# sh 11_8_1.sh
1x1=1
2x1=2 2x2=4
3x1=3 3x2=6 3x3=9
4x1=4 4x2=8 4x3=12 4x4=16
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
實踐-批量建立10個系統賬号
批量建立10個系統賬号(oldboy01-oldboy10),并設定密碼(密碼為随機數,要求是字元和數字的混合)
給數字加字首0的方法有兩種,一種用seq,另一種用
{01..10}
[[email protected] bin]# seq -w 10
01
02
03
04
05
06
07
08
09
10
[[email protected] bin]# echo {01..10}
01 02 03 04 05 06 07 08 09 10
無互動設定密碼,如下:
[[email protected] bin]# useradd oldgirl
[[email protected] bin]# echo 123456|passwd --stdin oldgirl
更改使用者 oldgirl 的密碼 。
passwd:所有的身份驗證令牌已經成功更新。
[[email protected] bin]#
設定随機數密碼:
[[email protected] bin]# echo $RANDOM
30441
[[email protected] bin]# echo $RANDOM|md5sum
96185997a39274f5f559aca8bc950181 -
[[email protected] bin]# echo $RANDOM|md5sum|cut -c 5-12
a264f7b8
[[email protected] bin]#
方法一:
[[email protected] shell]$ cat 11_14_2.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_14_2.sh
#Description: This is a test script.
#***********************************************
. /etc/init.d/functions
user="oldboy"
passfile="/u01/learn/shell/user.log"
for num in $(seq -w 1 5)
do
pass="$(echo "test$RANDOM"|md5sum|cut -c 3-11)"
useradd $user$num &>/dev/null &&\
echo "$pass"|passwd --stdin $user$num &>/dev/null &&\
echo -e "user:$user$num\tpasswd:$pass" >> $passfile
if [ $? -eq 0 ]
then
action "$user$num is ok" /bin/true
else
action "$user$num is fail" /bin/false
fi
done
echo --------------------------------------
cat $passfile && >$passfile
[[email protected] shell]# vim 11_14_2.sh
[[email protected] shell]# sh 11_14_2.sh
oldboy1 is ok [ 确定 ]
oldboy2 is ok [ 确定 ]
oldboy3 is ok [ 确定 ]
oldboy4 is ok [ 确定 ]
oldboy5 is ok [ 确定 ]
--------------------------------------
user:oldboy1 passwd:62f2a9de0
user:oldboy2 passwd:c2df01198
user:oldboy3 passwd:324d74def
user:oldboy4 passwd:7104be690
user:oldboy5 passwd:fca412731
方法二:
要批量建立密碼,可使用chpasswd來實作,chpasswd是一個批量更新使用者密碼的工具。
給多個使用者設定密碼的指令為:
chpasswd < 密碼檔案
但密碼檔案的内容必須以下面的格式來書寫,并且不能有空行;并且使用者必須存在
使用者名1:密碼1
使用者名2:密碼2
例如:
[[email protected] shell]# cat 11_14_3.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_14_2.sh
#Description: This is a test script.
#***********************************************
. /etc/init.d/functions
user="oldboy"
passfile="/u01/learn/shell/user.log"
for num in $(seq -w 8 9)
do
pass="$(echo "test$RANDOM"|md5sum|cut -c 3-11)"
useradd $user$num &>/dev/null &&\
echo -e "$user$num:$pass" >> $passfile
if [ $? -eq 0 ]
then
action "$user$num is ok" /bin/true
else
action "$user$num is fail" /bin/false
fi
done
echo --------------------------------------
#<====讀取密碼檔案進行密碼設定
chpasswd < $passfile
cat $passfile && >$passfile
[[email protected] shell]# sh 11_14_3.sh
oldboy8 is ok [ 确定 ]
oldboy9 is ok [ 确定 ]
--------------------------------------
oldboy8:2467d4d93
oldboy9:82bfd134d
産生随機數的6種方法
1.通過系統環境變量(
$RANDOM
)實作,示例代碼如下:
[[email protected] shell]# echo $RANDOM
14070
[[email protected] shell]# echo $RANDOM
1366
[[email protected] shell]#
RANDOM的随機數範圍為0~32767,是以,加密性不是很好,可以通過在輸出的随機數後增加加密字元串(就是和密碼生成有關的一個字元串)的方式解決,最後再一起執行md5sum操作并截取結果的後n位,這樣一來,就無法根據随機數範圍0~32767來猜出具體結果了。
例如:
[[email protected] shell]# echo "lth$RANDOM"|md5sum|cut -c 8-15
b9bc1312
[[email protected] shell]# echo "lth$RANDOM"|md5sum|cut -c 8-15
25f9a0c1
[[email protected] shell]#
2.通過openssl産生随機數,例如
[[email protected] shell]# openssl rand -base64 8
k2E/W/FZjGk=
[[email protected] shell]# openssl rand -base64 80
jbZCtvMtQD93zOJdqeex3bTAJ+4QqokUgZOao1J02+yaLevB8IzfXV6dXvRHFvsf
QxbJeQQs0YJNJDuCXLqjInfPQbcnsct+NqnlhZT7Xss=
[[email protected] shell]#
令數字與大小寫字元相結合,并且帶上特殊字元,可以達到很長的位數,這樣的随機數很安全。
3.通過時間date獲得随機數,例如
[[email protected] shell]# date +%s%N
1615800941013912847
[[email protected] shell]# date +%s%N
1615800942636001306
[[email protected] shell]#
4.通過/dev/urandom配合chksum生成随機數
[[email protected] shell]# head /dev/urandom|cksum
2932423259 3502
[[email protected] shell]# head /dev/urandom|cksum
2937343284 3780
/dev/random裝置存儲着系統目前運作環境的實時資料。它可以看作系統在某個時候的唯一值,是以可以用作随機數中繼資料。我們可以通過檔案讀取的方式,讀到裡面的資料。/dev/urandom這個裝置的資料與random裡的一樣。隻是,它是非阻塞的随機數發生器,讀取操作不會産生阻塞。
5.通過UUID生成随機數
[[email protected] shell]# cat /proc/sys/kernel/random/uuid
6bc84a00-e601-428f-9910-532b7c6b59e1
[[email protected] shell]# cat /proc/sys/kernel/random/uuid
f3542880-0ee8-42d0-a08d-b07da8549477
[[email protected] shell]#
select循環語句介紹及用法
select循環語句的主要作用可能就是建立菜單,在執行帶select循環語句的腳本時,輸出會按照數字順序的清單顯示一個菜單項,并顯示提示符(預設是#? ),同時等待使用者輸入數字進行選擇,下面就來帶大家看看生成菜單項的文法及具體案例實踐。
第一種
select 變量名 [ in 菜單取值清單 ]
do
指令...
done
執行個體一:直接使用清單字元串。
[[email protected] shell]# cat 11_15_1.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_15_1.sh
#Description: This is a test script.
#***********************************************
select name in oldboy oldgirl tingting
do
echo $name
done
[[email protected] shell]# sh 11_15_1.sh
1) oldboy
2) oldgirl
3) tingting
#? 1
oldboy
#? 2
oldgirl
#? 3
tingting
#? f
#? ^C
執行個體二:采用數組做變量清單。
[[email protected] shell]# cat 11_15_2.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_15_2.sh
#Description: This is a test script.
#***********************************************
array=(oldboy oldgirl tingting)
select name in "${array[@]}"
do
echo $name
done
[[email protected] shell]# sh 11_15_2.sh
1) oldboy
2) oldgirl
3) tingting
#? 1
oldboy
#? a
#? 2
oldgirl
#? ^C
[[email protected] shell]#
執行個體三:把指令結果作為變量清單(菜單項)
[[email protected] shell]# ls stu*
stu_102999_1.jpg stu_102999_2.jpg stu_102999_3.jpg stu_102999_4.jpg
[[email protected] shell]# vim 11_15_3.sh
[[email protected] shell]# sh 11_15_3.sh
1) stu_102999_1.jpg 3) stu_102999_3.jpg
2) stu_102999_2.jpg 4) stu_102999_4.jpg
#? 1
stu_102999_1.jpg
#? ^C
[[email protected] shell]#
[[email protected] shell]# cat 11_15_3.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_15_3.sh
#Description: This is a test script.
#***********************************************
select name in $(ls stu*)
do
echo $name
done
執行個體四:調整select循環菜單項的預設提示符及利用select變量列印數字序号。本範例重點講解了select循環的兩個特殊變量,其中PS3系統環境變量用于控制select循環的提示符,REPLY變量用于擷取菜單項對應的數字,也就是使用者輸入的數字。
[[email protected] shell]# cat 11_15_4.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-15
#FileName: 11_15_4.sh
#Description: This is a test script.
#***********************************************
PS3="please select a num from menu:"
select name in oldboy oldgirl tingting
do
echo -e "i guess you selected the menu is:\n $REPLY) $name"
done
[[email protected] shell]# sh 11_15_4.sh
1) oldboy
2) oldgirl
3) tingting
please select a num from menu:1
i guess you selected the menu is:
1) oldboy
please select a num from menu:2
i guess you selected the menu is:
2) oldgirl
please select a num from menu:4
i guess you selected the menu is:
4)
please select a num from menu:3
i guess you selected the menu is:
3) tingting
please select a num from menu:^C
執行個體五:列印選擇菜單,按照選擇一鍵安裝不同的Web服務。
[[email protected] shell]# cat 11_15_10.sh
#!/bin/bash
#***********************************************
#Author: luotianhao
#Mail: [email protected]
#Version: 1.0
#Date: 2021-03-16
#FileName: 11_15_10.sh
#Description: This is a test script.
#***********************************************
RETVAR=0
sh_path=/u01/learn/shell/scripts
[ ! -d "$sh_path" ] && {
mkdir $sh_path -p
}
Usage() {
echo "Usage:$0 argv"
return 1
}
function InstallService() {
[ 1 -ne $# ]&&{
Usage
}
local RETVAR=0
echo "start installing ${1}"
sleep 2;
[ ! -x "$sh_path/${1}.sh" ] && {
echo "$sh_path/${1}.sh does not exist or can not be exec"
return 1
} || {
$sh_path/${1}.sh
return $RETVAR
}
}
main() {
PS3="echo pls input the num you want:"
select var in "Install lamp" "Install lnmp" "exit"
do
case "$var" in
"Install lamp")
InstallService lamp
RETVAR=$?
;;
"Install lnmp")
InstallService lnmp
RETVAR=$?
;;
"exit")
echo bye
return 3
;;
*)
echo "the num you input must be {1|2|3}"
echo "Input ERROR"
esac
done
exit $ RETVAR
}
main
[[email protected] shell]# sh 11_15_10.sh
1) Install lamp
2) Install lnmp
3) exit
echo pls input the num you want:1
start installing lamp
lamp
echo pls input the num you want:2
start installing lnmp
lnmp
echo pls input the num you want:3
bye
[[email protected] shell]#