[root@gzh-8 test]# sh demo2.sh The script's name is : demo2.sh Parameter : Parameter : Parameter : Parameter : Parameter : Parameter : [root@gzh-8 test]# sh demo2.sh a b c d e f g The script's name is : demo2.sh Parameter : a Parameter : b Parameter : c Parameter : d Parameter : e Parameter : f
[root@gzh-8 test]# sh demo2.sh a b c d e f g The script's name is : demo2.sh Parameter : a Parameter : b Parameter : c Parameter : d Parameter : e Parameter : f Parameter count: 7 ALL parameter: a b c d e f g ALL parameter: a b c d e f g PID: 6499
[root@gzh-8 ~]# grep -n ^# /etc/sysctl.conf 1:# sysctl settings are defined through files in 2:# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. 3:# 4:# Vendors settings live in /usr/lib/sysctl.d/. 5:# To override a whole file, create a new file with the same in 6:# /etc/sysctl.d/ and put new settings there. To override 7:# only specific settings, add a file with a lexically later 8:# name in /etc/sysctl.d/ and put new settings there. 9:# 10:# For more information, see sysctl.conf(5) and sysctl.d(5).
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正则表达式!例如 1,20s/old/new/g 就是啦!
例:
# 先编辑文件testfile [root@gzh-8 test]# cat testfile HELLO LINUX! Linux is a free unix-type opterating system. This is a linux testfile! Linux test Google Taobao Runoob Tesetfile Wiki
# 在 testfile 文件的第四行后添加一行,并将结果输出到标准输出 [root@gzh-8 test]# sed -e 4a\gzh大帅哥 testfile HELLO LINUX! Linux is a free unix-type opterating system. This is a linux testfile! Linux test gzh大帅哥 Google Taobao Runoob Tesetfile Wiki # 将 testfile 的内容列出并且列印行号,同时,请将第 2~5 行删除! [root@gzh-8 test]# nl testfile | sed '2,5d' 1 HELLO LINUX! 6 Taobao 7 Runoob 8 Tesetfile 9 Wiki
[root@gzh-8 test]# cat for.sh #!/bin/bash for var in 1 2 3 4 5 do echo$var done [root@gzh-8 test]# sh for.sh 1 2 3 4 5
while循环
格式
while expression do 命令语句 done
循环输出1-5
[root@gzh-8 test]# cat while.sh #!/bin/bash num=1 while (($num<=5)) do echo$num let num++ done [root@gzh-8 test]# sh while.sh 1 2 3 4 5
until循环
格式
until expression do 命令语句 done
循环输出1-5
[root@gzh-8 test]# cat until.sh #!/bin/bash num=1 until [ $num -gt 5 ] do echo$num let num++ done [root@gzh-8 test]# sh until.sh 1 2 3 4 5 # 要注意的是[]里面的空格不能省略
5.3break与continue
break
当num变量值为3时退出循环
[root@gzh-8 test]# cat break.sh #!/bin/bash num=1 until [ $num -gt 5 ] do echo$num if [ $num -eq 3 ]; then break fi let num++ done echo"now,exit until circulation" [root@gzh-8 test]# sh break.sh 1 2 3 now,exituntil circulation
continue
输出1-50内可以被5整除的数
[root@gzh-8 test]# cat continue.sh #!/bin/bash for num in {1..50} do let temp=num%5 if [ $temp -ne 0 ]; then continue fi echo$num done [root@gzh-8 test]# sh continue.sh 5 10 15 20 25 30 35 40 45 50
6.脚本实例
6.1模拟用户登录
[root@gzh-8 test]# cat -n dl.sh 1 #!/bin/bash 2 read -ep "请输入用户名: " name 3 if [ "$name" == "gzh" ]; then 4 read -ep "请输入密码: " passwd 5 if [ "$passwd" == "guo123" ]; then 6 echo"登陆成功" 7 else 8 echo"密码错误!" 9 fi 10 else 11 echo"用户名错误!" 12 fi [root@gzh-8 test]# sh dl.sh 请输入用户名: gzh 请输入密码: guo123 登陆成功 [root@gzh-8 test]# sh dl.sh 请输入用户名: guo 用户名错误! [root@gzh-8 test]# sh dl.sh 请输入用户名: gzh 请输入密码: gzh 密码错误!
完毕! [root@gzh-8 test]# yum repolist enabled | grep "mysql.*-community.*" mysql-connectors-community MySQL Connectors Community mysql-tools-community MySQL Tools Community mysql80-community MySQL 8.0 Community Server [root@gzh-8 test]# yum module disable mysql MySQL 8.0 Community Server 236 kB/s | 3.2 MB 00:13 MySQL Connectors Community 8.4 kB/s | 102 kB 00:12 MySQL Tools Community 59 kB/s | 794 kB 00:13 上次元数据过期检查:0:00:01 前,执行于 2023年09月21日 星期四 23时51分24秒。 依赖关系解决。 ========================================================================================================== 软件包 架构 版本 仓库 大小 ========================================================================================================== 禁用模块: mysql
# 注意: Mysql安装完以后会生成随机密码,查看随机密码并更改他 [root@gzh-8 test]# grep 'temporary password' /var/log/mysqld.log 2023-09-22T03:54:04.925109Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: I1!j8ZBk8f(& [root@gzh-8 test]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.34
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Guozhihang0206.'; Query OK, 0 rows affected (0.01 sec)
mysql> set global validate_password.policy=low; Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=4; Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'guo123'; Query OK, 0 rows affected (0.00 sec)
要备份数据库先确定备份的数据库列表,这里新建一个数据库列表test
[root@gzh-8 test]# mysql -u root -pguo123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 8.0.34 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.
#删除10天前的备份文件(-exec rm -rf {} \是固定写法,删除查询出来的数据) find $BACKUP -mtime +10 -name "*.tar.gz" -execrm -rf {} \; echo"==========备份完成===========" [root@gzh-8 test]# sh sqlbak.sh ==========开始备份=========== 备份的路径是 /root/mysqlbak/2023_09_22_032545.tar.gz mysqldump: [Warning] Using a password on the command line interface can be insecure. 2023_09_22_032545/ 2023_09_22_032545/2023_09_22_032545.sql.gz ==========备份完成=========== [root@gzh-8 test]# ls ../mysqlbak/ 2023_09_22_032545.tar.gz