while循環:條件滿足,則循環;失敗,則退出
如何退出?
必須有時刻,條件測試不成功
? :條件控制變量
while 條件測試:do
循環體
done
until循環;條件不滿足,則循環;否則,退出
until 測試條件;do
循環體
done
bash編程之組合測試條件
邏輯與:多個條件同時滿足
[ CONDITION1 ] && [ CONDITION2 ]
[ CONDITION1 -a CONDITION2 ]
[[ CONDITION1 && CONDITION2 ]]
注意:前兩個使用單雙中括號都可,但&&不允許用於單個中括號中,所有第三者只能用於雙中括號中
邏輯或:多個條件滿足一個
[ CONDITION1 ] || [ CONDITION2 ]
[ CONDITION1 -o CONDITION2 ]
[[ CONDITION1 || CONDITION2 ]]
注意 || 不允許出現在單中括號中
得摩根定律
!(條件1或者 條件2) = !條件1 並且!條件2
!(條件1且條件2)=!條件1 或者 !條件2
練習:
1.:通過鍵盤提示用戶輸入字符,將用戶輸入的小寫字母轉換為大寫,轉換一次之后,在此提醒,再輸入再轉換,直到輸入quit退出;
1
2
3
4
5
6
7
8
9
|
#! /bin/bash
#
read
-p -t 5
"Enter a Word: "
word
while
[[
"$word"
!=
"quit"
]];
do
echo
$word |
tr
'a-z'
'A-Z'
read
-p -t 5
"Enter a Word again: "
word
done
|
2.寫一個腳本,實現如下功能;
1、顯示如下菜單:
CPU) show cpu info;
men) show memory info;
disk) show disk info;
quit) quit
Enter your option:
2、根據用戶的選擇輸出相應信息,每次執行后,不退出,而由用戶咋此指定新的選項
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#! /bin/bash
#
cat
<<EOF
cpu) print cpu infomation
men) print memory infomation
disk) print disk infomation
quit) Quit
EOF
read
-p
"Enter your option: "
option
option=`
echo
$option |
tr
'a-z'
'A-Z'
`
while
[[
"$option"
!=
"QUIT"
]];
do
if
[[
"$option"
==
"CPU"
]];
then
cat
/proc/cpuinfo
elif
[[
"$option"
==
"MEM"
]];
then
free
-m
elif
[[
"$option"
==
"DISK"
]];
then
df
-Th
else
echo
"Wrong Option..."
fi
read
-p
"Enter your option: "
option
option=`
echo
$option |
tr
'a-z'
'A-Z'
`
done
|
3.提示用戶輸入一個用戶名,顯示用戶名UID和SHELl信息,否則,則顯示無此用戶,顯示完成后,提示用戶再次輸入,如果quit則退出;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#! /bin/bash
#
cat
<<EOF
Username)Enter your Username..
quit)quit..
EOF
read
-p
"Enter Your userName: "
userName
userName=`
echo
$userName |
tr
'A-Z'
'a-z'
`
while
[[
"$userName"
!=
"QUIT"
]];
do
sysUser=`
cat
/etc/passwd
|
grep
^$userName |
cut
-d: -f1 |
tr
'A-Z'
'a-z'
`
if
[[
"$userName"
==
"$sysUser"
]];
then
echo
"This $userName `cat /etc/passwd | grep -i ^$userName | cut -d: -f3,7`"
else
echo
"No Such $userName.."
fi
read
-p
"Enter Your userName: "
userName
userName=`
echo
$userName |
tr
'A-Z'
'a-z'
`
done
#! /bin/bash
#
read
-t 2 -p
"Enter a user name: "
userName
userName=`
echo
$userName |
tr
'A-Z'
'a-z'
`
UID=`
grep
"^$userName\>"
/etc/passwd
|
cut
-d: -f3`
SH=`
grep
"^$userName\>"
/etc/passwd
|
cut
-d: -f7`
while
[[
"$userName"
!=
"quit"
]];
do
if
[ -z
"$userName"
];
then
echo
"Username null...."
elif
id
$userName &>
/dev/null
;
then
echo
"$userName uid: $UID"
echo
"$userName Shell: $SH"
else
echo
"No such user...."
fi
read
-t 2 -p
"Enter a user name again(quit to exit) "
userName
done
|
4.求100以內所有正整數的和;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#! /bin/bash
#
declare
-i
sum
=0
declare
-i i=1
while
[ $i -
le
100 ];
do
let
sum
+=$i
let
i++
done
echo
$
sum
#! /bin/bash
#
declare
-i
sum
=0
declare
-i i=1
until
[ $i -gt 100 ];
do
let
sum
+=$i
let
i++
done
echo
$
sum
|
5.求100以內所有偶數之和;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#! /bin/bash
#
declare
-i evensum=0
declare
-i i=1
while
[ $i -
le
100 ];
do
if
[ $[$i%2] -
eq
0 ];
then
let
evensum+=$i
fi
let
i++
done
echo
$evensum
#! /bin/bash
#
declare
-i
sum
=0
declare
-i i=0
while
[[ $i -
le
100 ]];
do
let
sum
+=$i
let
i+=2
done
echo
$
sum
|
6.用until求100以內整數之和;
1
2
3
4
5
6
7
8
9
10
11
|
#! /bin/bash
#
declare
-i
sum
=0
declare
-i i=1
until
[ $i -gt 100 ];
do
let
sum
+=$i
let
i++
done
echo
$
sum
|
7.提供一個用戶名,判斷用戶是否登陸當前系統;
1.如果沒有登陸,則停止5秒之后再次判斷,直到用戶登錄系統,顯示用戶登錄,而后退出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#! /bin/bash
#
read
-t 5 -p
"Enter Your userName: "
userName
userName=`
echo
$userName |
tr
'A-Z'
'a-z'
`
who
|
grep
"$userName"
&>
/dev/null
retVal=$?
while
[ $retVal -
ne
0 ];
do
sleep
5
read
-t 5 -p
"Enter Your userName: "
userName
userName=`
echo
$userName |
tr
'A-Z'
'a-z'
`
done
echo
"Welcome $userName login System..."
#! /bin/bash
#
read
-t 5 -p
"Enter Your userName: "
userName
while
!
id
$userName &>
/dev/null
;
do
read
-t 5 -p
"Enter Your userName: "
userName
done
who
|
grep
"^$userName"
&>
/dev/null
retVal=$?
while
[ $retVal -
ne
0 ];
do
sleep
5
who
|
grep
"$userName"
&>
/dev/null
retVal=$?
done
echo
"Welcome $userName login System..."
#! /bin/bash
#
read
-t 5 -p
"Enter Your userName: "
userName
while
!
id
$userName &>
/dev/null
;
do
read
-t 5 -p
"Enter Your userName again: "
userName
done
while
!
who
|
grep
"^$userName"
&>
/dev/null
;
do
sleep
5
done
echo
"Welcome $userName login System..."
#! /bin/bash
#
read
-t 5 -p
"Enter Your userName: "
userName
until
[ -n
"$userName"
] &&
id
$userName &>
/dev/null
;
do
read
-t 5 -p
"Enter Your userName again: "
userName
done
until
who
|
grep
"^$userName"
&>
/dev/null
;
do
sleep
5
done
echo
"Welcome $userName login System..."
|
8.取出當前系統上,默認shell為bash的用戶;
1
2
3
4
5
|
#! /bin/bash
#
while
read
line;
do
[[ `
echo
$line |
cut
-d: -f7` ==
"/bin/bash"
]] &&
echo
$line |
cut
-d: -f1
done
<
/etc/passwd
|
9.顯示其ID號為偶數的用戶;
1
2
3
4
5
6
7
8
9
|
#! /bin/bash
#
while
read
line;
do
userID=`
echo
$line |
cut
-d: -f3`
if
[ $[$userID%2] -
eq
0 ];
then
echo
-n
"$userID: "
echo
$line |
cut
-d: -f1
fi
done
<
/etc/passwd
|
10.顯示/etc/rc.d/rc.sysinit文件中,其總字符個數大於30的行;
1
2
3
4
5
6
7
8
9
|
#! /bin/bash
#
while
read
line;
do
charCounts=`
echo
$line |
wc
-c`
if
[ $charCounts -gt 30 ];
then
echo
-n
"$charCounts: "
echo
$line
fi
done
<
/etc/rc
.d
/rc
.sysinit
|
11.顯示用戶其UID和GID均為偶數的用戶;
1
2
3
4
5
6
7
8
9
10
|
#! /bin/bash
#
while
read
line;
do
userID=`
echo
$line |
cut
-d: -f3`
groupID`
echo
$line |
cut
-d: -f4`
if
[ $[$userID%2] -
eq
0 -a $[$groupID%2] -
eq
0 ];
then
echo
-n
"$userID,$groupID: "
echo
$line |
cut
-d: -f1
fi
done
<
/etc/passwd
|
12.顯示/etc/rc.d/rc.sysinit文件中,其總字符個數大於30且以非#開頭的行;
1
2
3
4
5
6
7
8
9
|
#! /bin/bash
#
while
read
line;
do
charCounts=`
echo
$line |
wc
-c`
if
[ $charCounts -gt 30 ] && [[
"$line"
=~ ^[^
#] ]]; then
echo
-n
"$charCounts: "
echo
$line
fi
done
<
/etc/rc
.d
/rc
.sysinit
|
13.寫一個腳本,完成如下任務;
1.提示用戶輸入一個磁盤設備文件路徑不存在或不是一個塊設備,則提示用戶重新輸入,知道輸入正確為止,或者輸入quit以9為退出碼結束腳本
2.提示用戶"下面的操作會清空磁盤的數據,並提問是否繼續"
如果用戶給出字符y或yes,則繼續,否則,則提供以8為退出碼結束腳本
3.將用戶指定的磁盤上的分區清空,而后創建兩個分區,大小分別為100M和512M
4.格式化這兩個分區
5.將第一個分區掛載至/mnt/boot目錄,第二個分區掛載至/mnt/sysroot目錄