本节所讲内容:
跳出循环
将windows中的脚本导入到Linux系统后执行报错
Shift参数左移指令
shell中函数使用方法
shell脚本实战:mysql自动备份和自动解压ZIP 文件脚本
跳出循环:break和continue
Break:跳出整个循环
Continue:跳过本次循环,进行下次循环
[root@localhost test]#vim breakcontiue.sh
#!/bin/bash
while true
do
echo "********************************"
echo "*** 以下为可选的操作选项 ***"
echo "*** 1 复制 ***"
echo "*** 2 删除 ***"
echo "*** 3 备份 ***"
echo "*** 4 退出 ***"
echo "********************************"
read -p "请确定你要执行的操作:" OP
case $OP in
1)
continue
echo "即将对以下文件执行复制"
;;
2)
echo "即将对以下文件执行删除"
;;
3)
echo "即将对以下文件执行备份"
;;
4)
echo "即将退出......"
break
;;
*)
echo "错误:$OP不是可用参数,请重新输入"
esac
done
将windows中的脚本导入到Linux系统后执行报错
[root@localhost test]#rpm -ivh /mnt/Packages/dos2unix-6.0.3-4.el7.x86_64.rpm
warning:/mnt/Packages/dos2unix-6.0.3-4.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature,key ID fd431d51: NOKEY
Preparing... #################################[100%]
Updating / installing...
1:dos2unix-6.0.3-4.el7 #################################[100%]
[root@localhost test]#dos2unix v2.sh
dos2unix: converting filev2.sh to Unix format ...
Shift:参数左移指令:
每执行一次,参数序列顺次左移一个位置,$#的值减1,用于分别处理每个参数,移出去的参数,不再可
用。
例子:
做一个加法计算器,求出所有参数的和
11 12 13 14
$1 $2 $3 $3
[root@localhost test]#vim shift.sh
#!/bin/bash
if [ $# -le 0 ]
then
echo "err!:Not enoughparameters"
exit 124
fi
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`
shift
done
echo $sum
shell函数使用方法
函数:把一个功能封装起来。 使用时直接调用函数名。 这样的脚本好处:模块,代码可读性强。
函数的定义:
f(x) = x + 1
f(2) = 3
语法:
函数名 ()
{
命令序列
}
或:
function 函数名() # function 可以不写
{
命令序列
}
注:函数调用时:不带()
调用语法:
函数名 参数1 参数2 …
函数中的变量均为全局变量,没有局部变量
调用函数时,可以传递参数。在函数中用$1、$2…来引用传递的参数
#!/bin/bash
abc=123
#定义一个函数
example()
{
abc=456
}
echo $abc
#调用函数
example
echo $abc
输出结果
[root@localhost test]#./f1.sh
123
456
例2:演示函数参数传递
#!/bin/bash
delete()
{
rm -rf $1
mkdir $2
}
delete /root/a.txt /root/mydir1
例3:通过脚本调用其他脚本中的函数
通过source调用其他脚本中的函数
首先定义一个包含不同函数的功能模块
#!/bin/bash
delete()
{
rm -rf $de
}
copy()
{
cp -rf $sdir $tdir
}
backup()
{
tar zcvf $tar_name $tar_dir &> /dev/null
}
quit()
{
exit
}
创建一个可选菜单的脚本对模块内容进行调用
#!/bin/bash
source/root/filemanager.sh
while true
do
cat <<EOF
****************************************
The follwing is optional
****************************************
1) Copy
2) Delete
3) Backup
4) Exit
****************************************
EOF
read -p "Pleaseenter your options: " option
case $option in
1)
read -p "Please input your want tocopy the source file: " sdir
read -p "Please input your targetdirectory: " tdir
copy
;;
2)
read -p "Please input your targetdirectory: " de
delete
;;
3)
read -p "Please input your backupfile name: " tar_name
read -p "Please input your bakcupfile: " tar_dir
backup
;;
4)
quit ; break
;;
*)
echo "$option is not optionaloperation."
;;
esac
done
执行结果:
[root@localhost ~]#./file.sh
****************************************
The follwing is optional
****************************************
1) Copy
2) Delete
3) Backup
4) Exit
****************************************
Please enter youroptions: 2
Please input your targetdirectory: /root/mydir
****************************************
The follwing is optional
****************************************
1) Copy
2) Delete
3) Backup
4) Exit
****************************************
Please enter youroptions: 3
Please input your backupfile name: grub2.tar.gz
Please input your bakcupfile: /boot/grub2/grub.cfg
****************************************
The follwing is optional
****************************************
1) Copy
2) Delete
3) Backup
4) Exit
****************************************
Please enter youroptions: 4
[root@localhost ~]#
[root@localhost ~]# ls
anaconda-ks.cfg Documents filemanager.sh grub2.tar.gz Music mydir2 Pictures Templates useradd.sh
学习过程中如果问题,请留言。更多内容请加:
学神IT-linux讲师-RM老师QQ:2805537762
学神IT-戚老师QQ:3341251313
学神IT-旭斌QQ:372469347
学神IT教育RHEL7交流群:468845589
转载于:https://blog.51cto.com/xuegodlinux/1729769