until CONDITION; do
循环体
done
进入条件:false
退出条件:true
示例:求100以内所有正整数之和
1
2
3
4
5
6
7
8
9
10
|
[root@c7 shell]
# vim summary.sh
#!/bin/bash
#
declare
-i i=1
declare
-i
sum
=0
until
[ $i -gt 100 ];
do
let
sum
+=$i
let
i++
done
echo
"Sum: $sum"
|
示例:打印九九乘法表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@c7 shell]
# cat multi.sh
#!/bin/bash
#
declare
-i j=1
declare
-i i=1
until
[ $j -gt 9 ];
do
until
[ $i -gt $j ];
do
echo
-n -e
"${i}X${j}=$[$i*$j]\t"
let
i++
done
echo
let
i=1
let
j++
done
|
循环控制语句(用于循环体中):
continue [N]:提前结束第N层的本轮循环,而直接进入下一轮判断;
while CONDTIITON1;do
CMD1
...
if CONDITION2;then
continue
fi
CMDn
...
done
break[N]:提前结束循环;
while CONDTIITON1;do
CMD1
...
if CONDITION2;then
continue
fi
CMDn
...
done
示例1:求100以内所有偶数之和;要求循环遍历100以内的所有正整数;
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@c7 shell]
# cat even.sh
#!/bin/bash
#
declare
-i i=0
declare
-i
sum
=0
until
[ $i -gt 100 ];
do
let
i++
if
[ $[$i%2] -
eq
1 ];
then
continue
fi
let
sum
+=$i
done
echo
"Even sum: $sum"
|
创建死循环:
while true;do
循环体
done
until false;do
循环体
done
示例2:每隔3种钟到系统上获取已经登录的用户的信息,如果docker登录了,则记录于日志中,并退出;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@c7 shell]
# cat user1.sh
#!/bin/bash
#
read
-p
"Enter a user name:"
username
while
true
;
do
if
who
|
grep
"^$username"
&>
/dev/null
;
then
break
fi
sleep
3
done
echo
"$username logged on."
>>
/tmp/user
.log
方法二:
[root@c7 shell]
# cat user2.sh
#!/bin/bash
#
read
-p
"Enter a user name:"
username
until
who
|
grep
"^$username"
&>
/dev/null
;
do
sleep
3
done
echo
"$username logged on."
>>
/tmp/user
.log
|
视频51 32分钟
while循环的特殊用法(遍历文件的每一行);
while read line;do
循环体
done < /PATH/FROM/SOMEFILE
修改读取/PATH/FROM/SOMEFILE文件中的每一行,且将行赋值给变量line;
示例:找出其ID号为偶数的所有用户,显示其用户名及ID号
1
2
3
4
5
6
7
8
9
|
[root@c7 shell]
# cat evenid.sh
#!/bin/bash
#
while
read
line;
do
if
[ $[`
echo
$line |
cut
-d: -f3` % 2 ] -
eq
0 ];
then
echo
-e -n
"username:`echo $line | cut -d: -f1`\t"
echo
"uid:`echo $line | cut -d: -f3`"
fi
done
<
/etc/passwd
|
视频51 36分钟
for循环的特殊格式:
for ((控制变量的初始化;条件判断表达式;控制变量的修正表达式));do
循环体
done
控制变量初始化:仅运行到循环代码段时执行一次;
控制变量的修正表达式:每轮循环结束会先进行控制变量修正运算,而后再做条件判断;
示例:求100以内所有正整数之和;
1
2
3
4
5
6
7
8
|
[root@c7 shell]
# cat sum2.sh
#!/bin/bash
#
declare
-i
sum
=0
for
((i=1;i<=100;i++));
do
let
sum
+=$i
done
echo
"Sum: $sum."
|
示例:打印乘法表
1
2
3
4
5
6
7
8
9
|
[root@c7 shell]
# cat multi2.sh
#!/bin/bash
#
for
((j=1;j<=9;j++));
do
for
((i=1;i<=j;i++));
do
echo
-e -n
"${i}X${j}=$[$i*$j]\t"
done
echo
done
|
练习:写一个脚本,完成如下任务
(1)显示一个如下菜单:
cpu) show cpu information;
mem) show memory information;
disk) show disk information;
quit) quit
(2)提示用户选择选项
(3)显示用户选择的内容
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
|
[root@c7 shell]
# cat infosys.sh
#!/bin/bash
#
cat
<< EOF
cpu) show cpu information;
mem) show mem information;
disk) show disk information;
quit) quit
=========================================
EOF
read
-p
"Enter a option:"
option
while
[
"$option"
!=
'cpu'
\
-a
"$option"
!=
'mem'
\
-a
"$option"
!=
'disk'
\
-a
"$option"
!=
'quit'
];
do
read
-p
"Wrong option,Enter again:"
option
done
if
[
"$option"
==
'cpu'
];
then
lscpu
elif
[
"$option"
==
'mem'
];
then
cat
/proc/meminfo
elif
[
"$option"
==
'disk'
];
then
fdisk
-l
else
echo
"Quit"
exit
0
fi
|
进一步地:
用户选择,并显示完成后不退出脚本,而是提示用户继续选择显示其它内容,直到使用quit方式退出;
条件判断:case语句
case 变量引用 in
PAT1)
分支1
;;
PAT2)
分支2
;;
...
*)
默认分支
;;
esac
示例:
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
|
[root@c7 shell]
# cat infosys2.sh
#!/bin/bash
#
cat
<< EOF
cpu) show cpu information;
mem) show mem information;
disk) show disk information;
quit) quit
=========================================
EOF
read
-p
"Enter a option:"
option
while
[
"$option"
!=
'cpu'
\
-a
"$option"
!=
'mem'
\
-a
"$option"
!=
'disk'
\
-a
"$option"
!=
'quit'
];
do
read
-p
"Wrong option,Enter again:"
option
done
case
"$option"
in
cpu)
lscpu
;;
mem)
cat
/proc/meminfo
;;
disk)
fdisk
-l
;;
*)
echo
"Quit..."
exit
0
esac
|
视频25分钟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@c7 shell]
# cat f1.sh
#!/bin/bash
#
username=
'myuser'
function
adduser {
if
id
$username &>
/dev/null
;
then
echo
"$username exists."
return
1
else
useradd
$username
[ $? -
eq
0 ] &&
echo
"Add $username finished."
&&
return
0
fi
}
adduser
echo
$?
|
第二种 函数调用 视频28分钟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@c7 shell]
# cat f2.sh
#!/bin/bash
#
function
adduser {
if
id
$username &>
/dev/null
;
then
echo
"$username exists."
return
1
else
useradd
$username
[ $? -
eq
0 ] &&
echo
"Add $username finished."
&&
return
0
fi
}
for
i
in
{1..10};
do
username=myuser$i
adduser
done
|
练习:写一个脚本,完成如下要求 31分钟
(1)脚本可接受参数:start,stop,restart,status;
(2)如果参数非此四者之一,提示使用格式后报错退出;
(3)如果是start:则创建/var/lock/subsys/SCRIPT_NAME,并显示“启动成功”;
考虑:如何事先已经启动过一次,该如何处理?
(4)如果是stop:则删除/var/lock/subsys/SCRIPT_NAME,并显示“停止完成”;
考虑:如果事先已经停止过了,该如何处理?
(5)如果是restart,则先stop,再start;
考虑:如果本来没有start,如何处理?
(6)如果是status,则
如果/var/lock/subsys/SCRIPT_NAME文件存在,则显示“SCRIPT_NAME is running...”
如果/var/lock/subsys/SCRIPT_NAME文件不存在,则显示“SCRIPT_NAME is stopped...”
其中,SCRIPT_NAME 为当前脚本名;