SHELL跳出循环、shift左移、函数的使用和数据库自动备份脚本
2016-05-19 16:28
856 查看
本节所讲内容:
• 跳出循环
• 将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
[root@xuegod60 test]# rpm-ivh /mnt/Packages/dos2unix-6.0.3-4.el7.x86_64.rpm
[root@xuegod60 test]#dos2unix v2.sh
dos2unix: converting filev2.sh to Unix format ...
[root@xuegod60 test]#./v2.sh
*******************************************
1 Copy
2 Delete
3 Backup
4 Quit
*******************************************
Shift:参数左移指令:
每执行一次,参数序列顺次左移一个位置,$#的值减1,用于分别处理每个参数,移出去的参数,不再可
用。
例子:
做一个加法计算器,求出所有参数的和
11 12 13 14
$1 $2 $3 $4
[root@xuegod60 ~]# vimshift.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) = 2x + 1 g(x) = cosx + 1
f(2)=5
g(3)=?
语法:
函数名 ()
{
命令序列
}
或:
function 函数名()
# function 可以不写
{
命令序列
}
注:函数调用时:不带()
调用语法:
函数名 参数1 参数2 …
函数中的变量均为全局变量,没有局部变量
调用函数时,可以传递参数。在函数中用$1、$2…来引用传递的参数
#!/bin/bash
abc=123
#定义一个函数
example()
{
abc=456
}
echo $abc
#调用函数
example
echo $abc
[root@xuegod60 ~]# chmod+x fun1.sh
[root@xuegod60 ~]#./fun1.sh
123
456
例2:演示函数参数传递
[root@xuegod60 ~]# vim fun2.sh
#!/bin/bash
delete()
{
rm -rf $1
mkdir $2
}
delete /root/a.txt /root/mydir
例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/test/filemanager.sh
while true
do
cat <<EOF
*****************************
The following 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 you 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 enter your backupfile names: " tar_name
read -p "Please enter your backupfile: " tar_dir
backup
;;
4 )
quit ; break
;;
*)
echo "$option is not optionaloperation."
;;
esac
done
执行结果:
[root@xuegod60 test]#./file.sh
*****************************
The following is optional
*****************************
1) Copy
2) Delete
3) Backup
4) Exit
*****************************
Please enter youroptions: 1
Please input you want tocopy the source file: /etc/passwd
Please input your targetdirectory: /root/test/
*****************************
The following is optional
*****************************
1) Copy
2) Delete
3) Backup
4) Exit
*****************************
Please enter youroptions: 4
[root@xuegod60 test]# ls
filemanager.sh passwd v2.sh
file.sh shift.sh v3-此脚本初学者了解一下就可以-使用函数封装-公司自动处理文件备份管理的脚本.txt
mysql-back-auto.sh useradd.sh zip-auto.sh
实战:数据库自动备份的脚本
安装数据库MariaDB
[root@xuegod60 ~]# yum -yinstall mariadb-server
启动数据库
[root@xuegod60 ~]#systemctl restart mariadb
基本的使用
[root@xuegod60 ~]# mysql
MariaDB [(none)]> showdatabases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye
• 跳出循环
• 将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
[root@xuegod60 test]# rpm-ivh /mnt/Packages/dos2unix-6.0.3-4.el7.x86_64.rpm
[root@xuegod60 test]#dos2unix v2.sh
dos2unix: converting filev2.sh to Unix format ...
[root@xuegod60 test]#./v2.sh
*******************************************
1 Copy
2 Delete
3 Backup
4 Quit
*******************************************
Shift:参数左移指令:
每执行一次,参数序列顺次左移一个位置,$#的值减1,用于分别处理每个参数,移出去的参数,不再可
用。
例子:
做一个加法计算器,求出所有参数的和
11 12 13 14
$1 $2 $3 $4
[root@xuegod60 ~]# vimshift.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) = 2x + 1 g(x) = cosx + 1
f(2)=5
g(3)=?
语法:
函数名 ()
{
命令序列
}
或:
function 函数名()
# function 可以不写
{
命令序列
}
注:函数调用时:不带()
调用语法:
函数名 参数1 参数2 …
函数中的变量均为全局变量,没有局部变量
调用函数时,可以传递参数。在函数中用$1、$2…来引用传递的参数
#!/bin/bash
abc=123
#定义一个函数
example()
{
abc=456
}
echo $abc
#调用函数
example
echo $abc
[root@xuegod60 ~]# chmod+x fun1.sh
[root@xuegod60 ~]#./fun1.sh
123
456
例2:演示函数参数传递
[root@xuegod60 ~]# vim fun2.sh
#!/bin/bash
delete()
{
rm -rf $1
mkdir $2
}
delete /root/a.txt /root/mydir
例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/test/filemanager.sh
while true
do
cat <<EOF
*****************************
The following 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 you 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 enter your backupfile names: " tar_name
read -p "Please enter your backupfile: " tar_dir
backup
;;
4 )
quit ; break
;;
*)
echo "$option is not optionaloperation."
;;
esac
done
执行结果:
[root@xuegod60 test]#./file.sh
*****************************
The following is optional
*****************************
1) Copy
2) Delete
3) Backup
4) Exit
*****************************
Please enter youroptions: 1
Please input you want tocopy the source file: /etc/passwd
Please input your targetdirectory: /root/test/
*****************************
The following is optional
*****************************
1) Copy
2) Delete
3) Backup
4) Exit
*****************************
Please enter youroptions: 4
[root@xuegod60 test]# ls
filemanager.sh passwd v2.sh
file.sh shift.sh v3-此脚本初学者了解一下就可以-使用函数封装-公司自动处理文件备份管理的脚本.txt
mysql-back-auto.sh useradd.sh zip-auto.sh
实战:数据库自动备份的脚本
安装数据库MariaDB
[root@xuegod60 ~]# yum -yinstall mariadb-server
启动数据库
[root@xuegod60 ~]#systemctl restart mariadb
基本的使用
[root@xuegod60 ~]# mysql
MariaDB [(none)]> showdatabases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye
相关文章推荐
- Shell正则表达式与通配符
- CentOS6 /etc/profile ~/.bash_profile ~/.bashrc等文件的执行顺序
- shell脚本中一些特殊符号
- 交互式和非交互式shell、登录和非登录shell之间的区别
- 几点基于Web日志的Webshell检测思路
- 一条shell统计代码行数
- Linux Shell变量使用
- 常用工具类,文件和内存的大小获取,shell脚本的执行
- Shell引号
- bash自动化
- Shell脚本$系列参数意义
- Shell及 子Shell关系
- Shell 命令总结
- shell命令-read详解
- shell中job管理
- bash中的管道和重定向
- SHELLcase流程控制和for、while循环语句的使用
- shell-7.shell 字符串的使用
- Shell 统计PV, UV ,独立IP
- LinuxShell日期遍历