Article Directory
Linux cloud computing architecture-shell entry to proficient (give up)[2]
1. Break out of the loop
continue: Jump out of this loop and proceed to the next loop
Ignore the remaining code in this loop. By default, the remaining code of this loop is ignored, namely continue 1; if it is continue 2, it means that the code of this and next loop is ignored.
[root@client ~]# vim break.sh
#!/bin/bash
# 跳出当前循环,即仅无4*4=16,其后的结果继续显示
for i in `seq 9`
do
sum=$(($i*$i))
if [ $sum -eq 16 ] ; then
continue
else
echo "$i*$i=$sum"
fi
done
[root@client ~]# chmod +x break.sh
[root@client ~]# bash break.sh
1*1=1
2*2=4
3*3=9
5*5=25
6*6=36
7*7=49
8*8=64
9*9=81
break: jump out of the entire loop
Ignore the code of the entire loop body and proceed directly to the code after the loop body.
#!/bin/bash
# 跳出当整个循环体,即4*4=16以后的结果都不显示,包括4*4=16
for i in `seq 9`
do
sum=$(($i*$i))
if [ $sum -eq 16 ] ; then
break
else
echo "$i*$i=$sum"
fi
done
[root@client ~]# vim continue.sh
[root@client ~]# chmod +x continue.sh
[root@client ~]# bash continue.sh
1*1=1
2*2=4
3*3=9
2. Shift parameter left shift (recursive iteration)
shift means that the parameter is shifted to the left by one bit, and shift 2 means that the parameter is shifted to the left by two positions. Similar to the effect of recursive iteration.
[root@client ~]# vim shift.sh
#!/bin/bash
# 累加求和
if [ $# -eq 0 ] ; then
echo "没有足够的参数"
exit 0
fi
sum=0
while [ $# -gt 0 ] ; do
sum=$(($sum+$1))
shift
done
echo "the result is $sum"
[root@client ~]# chmod +x shift.sh
[root@client ~]# bash shift.sh
没有足够的参数
[root@client ~]# bash shift.sh 1 2 3
the result is 6
[root@client ~]# bash shift.sh 1 2 3 4 5 6 7 8 9 10
the result is 55
3. Functions
Functions are script code blocks. Can make the script modular and readable.
3.1 Define function
# 方法1
function name{
commands
}
# 方法2
name(){
commands
}
# 函数的书写和脚本类似
3.2 Call function
# 调用函数
name par1 par2 par3 ...
# 函数的参数传递
# 法1:通过脚本的位置参数传递给函数的位置参数【调用参数时使用位置参数】
[root@client ~]# bash rm.sh a.txt
#!/bin/bash
fun1(){
rm -rf $1
}
fun1 $1
# 法2:调用函数时直接使用参数
[root@client ~]# bash rm.sh
#!/bin/bash
fun1(){
rm -rf $1
}
fun1 /root/a.txt
# 法3:在函数中使用多个位置参数,调用函数时使用多参数传递
3.3 Variable handling in functions
Local variable: The variable defined in the function is only valid inside the function.
Global variable: A variable defined in the script, but within a function, the variable can also be used.
[root@client ~]# vim fun.sh
#!/bin/bash
function fun1 {
for i in `seq $var`
do
sum=$(($i*$i))
echo "$i*$i=$sum"
done
}
read -p "input a num:" var
fun1
[root@client ~]# chmod +x fun.sh
[root@client ~]# bash fun.sh
input a num:8
1*1=1
2*2=4
3*3=9
4*4=16
5*5=25
6*6=36
7*7=49
8*8=64
3.4 Function note
①The function contains multiple functions with the same name, and the last one shall prevail.
[root@client ~]# vim fun.sh
#!/bin/bash
fun1(){
echo "aaa"
}
fun1(){
echo "bbb"
}
fun1(){
echo "ccc"
}
fun1
[root@client ~]# chmod +x fun.sh
[root@client ~]# bash fun.sh
ccc
② A function can have a return value. Using a return
command to return a value means that return
the code after the function is not executed. The return value is a status code. The value range of the status code is 0-255 [return status code]
[root@client ~]# vim fun.sh
#!/bin/bash
fun1(){
echo "aaa"
return 5
}
fun1
[root@client ~]# bash fun.sh
aaa
[root@client ~]# echo $?
5
③The echo $?
command can output the return value of the previous function.
④ Functions are often used in calculations, so when assigning values, the function value should be a number or string.
4. Actual combat
4.1 Automatically backup mysql database
# 安装mysql数据库,这里用mariadb数据库代替。
# 安装并启动mariadb数据库,从监听来看,服务用的还是mysqld,不愧是父女。
[root@client ~]# ls /media/cdrom/Packages/maria*
/media/cdrom/Packages/mariadb-5.5.60-1.el7_5.x86_64.rpm /media/cdrom/Packages/mariadb-libs-5.5.60-1.el7_5.x86_64.rpm
/media/cdrom/Packages/mariadb-bench-5.5.60-1.el7_5.x86_64.rpm /media/cdrom/Packages/mariadb-server-5.5.60-1.el7_5.x86_64.rpm
/media/cdrom/Packages/mariadb-devel-5.5.60-1.el7_5.x86_64.rpm /media/cdrom/Packages/mariadb-test-5.5.60-1.el7_5.x86_64.rpm
[root@client ~]# yum install mariadb-server -y
[root@client ~]# ll /usr/bin/mysql
-rwxr-xr-x. 1 root root 3539456 8月 16 2018 /usr/bin/mysql
[root@client ~]# rpm -qf /usr/bin/mysql
mariadb-5.5.60-1.el7_5.x86_64
[root@client ~]# systemctl start mariadb
[root@client ~]# netstat -antup | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 19902/mysqld
# 登录并创建数据库,然后往数据库中插入数据
# 再次登录,可以看到数据库abong是有一个数据表name的。
[root@client ~]# mysqladmin -u root password "123456"
[root@client ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abong |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use abong;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [abong]> show tables;
+-----------------+
| Tables_in_abong |
+-----------------+
| name |
+-----------------+
1 row in set (0.00 sec)
MariaDB [abong]> select * from name;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
MariaDB [abong]> exit
Bye
# 备份步骤:
1、检查当前用户是否是root,检查自动备份mysql的目录是否存在。
2、使用mysqldump备份数据库,生成sql文件,并压缩成tar.gz格式的压缩包。
3、删除sql文件
4、输出备份成功及备份目录信息
[root@client ~]# vim mysqlbak.sh
#!/bin/bash
BAKDIR=/opt/mysqlbak/`date +%Y%m%d`
MYSQLUSER=root
MYSQLPASSWD=123456
MYSQLDB=abong
if [ $UID -ne 0 ] ; then
echo "it's not root"
exit 0
fi
if [ ! -d $BAKDIR ] ; then
mkdir -p $BAKDIR
else
echo "the $BAKDIR is exist"
exit 0
fi
mysqldump -u$MYSQLUSER -p$MYSQLPASSWD $MYSQLDB > $BAKDIR/${MYSQLDB}_db.sql
tar czf $BAKDIR/${MYSQLDB}_db.tar.gz $BAKDIR/*.sql &> /dev/null
find $BAKDIR -type f -name *.sql -exec rm -f {
} \;
if [ $? -eq 0 ] ; then
echo "$MYSQLDB database backup successfully"
echo "backup dir is $BAKDIR"
fi
[root@client ~]# chmod +x mysqlbak.sh
[root@client ~]# bash mysqlbak.sh
abong database backup successfully
backup dir is /opt/mysqlbak/20200802
[root@client ~]# ll /opt/mysqlbak/20200802
总用量 4
-rw-r--r--. 1 root root 782 8月 2 09:20 abong_db.tar.gz
# 可以看到,非root用户是不可以备份mysql数据库的。
[root@client ~]# cp mysqlbak.sh /home/abong/
[root@client ~]# su - abong
[abong@client ~]$ ll
总用量 4
-rwxr-xr-x. 1 root root 544 8月 2 09:23 mysqlbak.sh
[abong@client ~]$ bash mysqlbak.sh
it's not root
4.2 Startup script of nginx service
[root@client ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 2
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -3 $(cat $PIDF)
;;
restart)
$0 stop &> /dev/null
if [ $? -ne 0 ] ; then continue ; fi
$0 start
;;
reload)
kill -1 $(cat $PIDF)
;;
*)
echo "Userage: $0 { start | stop | restart | reload }"
exit 1
esac
exit 0