for循环
1.for循环格式
for 变量名 in [取值列表] 苹果 香蕉 字符串 数字{}序列 命令
do
命令体
echo 呵呵
done
2.案例:使用for循环创建用户
cat user.txt
zs
ls
em
oldboy
[root@web scripts]# cat for1.sh
#!/bin/sh
for i in `cat user.txt`
do
useradd $i
done
3.命令行中使用for循环批量删除用户
for i in `cat user.txt`;do userdel -r $i;done
4.案例2:使用for循环 从1加到100 结果5050
seq -s + 100|bc
[root@web scripts]# cat ping.sh
#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
for i in `seq 254`
do
IP=10.0.0.$i
ping -c 1 -W 1 $IP >/dev/null 2>&1
[ $? -eq 0 ] && action "ping $IP is" /bin/true
done
for i in `seq 100`;do sum=$[sum+i];done;echo $sum
while 循环
1.while循环格式
while [ 条件表达式 ]
do
命令
done
2.案例: 1加到100
#!/bin/sh
i=0
while [ $i -le 100 ]
do
sum=$[i+sum]
let i++
done
echo $sum
3.流程控制语句 exit continue break
exit 退出整个脚本 不会继续执行
break 跳出本次循环 继续往下执行 跳出循环体
continue 结束当前此次的命令,继续下一次循环
4.exit continue break 举例
[root@web scripts]# cat exit.sh break.sh continue.sh
#!/bin/sh
while true
do
echo ok
exit
echo hehe
done
echo done......
#!/bin/sh
while true
do
echo ok
break
echo hehe
done
echo done......
#!/bin/sh
while true
do
echo ok
continue
echo hehe
done
echo done......
5.案例2 break continue
#!/bin/sh
for i in `seq 10`
do
user=oldboy$i
id $user &>/dev/null
if [ $? -ne 0 ];then
useradd $user
[ $? -eq 0 ] && echo "$user create is ok"
else
break
fi
done
echo hehe.......
#!/bin/sh
for i in `seq 10`
do
user=oldboy$i
id $user &>/dev/null
if [ $? -ne 0 ];then
useradd $user
[ $? -eq 0 ] && echo "$user create is ok"
else
continue
fi
done
echo hehe.......
函数
- 命令的集合 完成特定功能的代码块
模块化 复用
函数和变量类似 只有先定义才可以调用,如果只定义不调用 则不会执行
2.函数的定义和调用
#!/bin/sh
test1(){
echo "第一种函数定义方式"
}
function test2(){
echo "第二种函数定义方式"
}
function test3 {
echo "第三种函数定义方式"
}
test1
test2
test3
3.函数的传参 不能直接传参
在函数名的后面跟参数
全局配置 在函数的最上面 设置变量
local 只在函数体内部生效
fun(){
if [ -f $1 ];then
echo "$1 is exsis"
else
echo "$1 is no ex"
fi
}
fun $1
[root@web scripts]# cat fun.sh
#!/bin/sh
fun(){
if [ -f $1 ];then
echo "$1 exists"
else
echo "$1 no exists"
fi
}
fun $2 $3
#!/bin/sh
fun(){
num=20
for i in `seq 10`
do
sum=$[num+i]
done
echo $sum
}
fun
[root@web scripts]# cat fun1.sh
#!/bin/sh
fun(){
num=20
for i in `seq $1`
do
sum=$[num+i]
done
echo $sum
}
fun $3 $1 $2
[root@web scripts]# cat fun1.sh
#!/bin/sh
fun(){
num=20
for i in `seq $1`
do
sum=$[num+i]
done
echo $sum
}
fun $1
echo $num
4.错误 先定义 在调用
[root@web scripts]# cat fun1.sh
#!/bin/sh
fun $1
fun(){
num=20
for i in `seq $1`
do
sum=$[num+i]
done
echo $sum
}
函数的返回值 return
5.案例
#!/bin/sh
[ -f /etc/hosts ] && exit 50 || exit 100
#!/bin/sh
fun(){
if [ -f $1 ];then
return 50
else
return 100
fi
}
fun $1
re=$?
[ $re -eq 50 ] && echo "$1 该文件存在"
[ $re -eq 100 ] && echo "$1 该文件不存在"
6.函数内如果执行的上一条是函数名 则不会返回函数本身执行的返回值
[root@web scripts]# cat fun4.sh
#!/bin/sh
fun(){
echo 50
return 1
}
re=`fun`
echo "函数的返回值是?: $?"
echo "函数执行的结果是?: $re"
[root@web scripts]# sh fun4.sh
函数的返回值是?: 1
函数执行的结果是?: 50
--------------------------
[root@web scripts]# cat fun4.sh
#!/bin/sh
fun(){
echo 50
return 1
}
re=`fun`
name=oldboy
echo "函数的返回值是?: $?"
echo "函数执行的结果是?: $re"
[root@web scripts]# sh fun4.sh
函数的返回值是?: 0
函数执行的结果是?: 50