[root@liruilong ~]$ history#查看历史记录 [root@liruilong ~]$ history -c #清空历史记录 [root@liruilong ~]$ vim /etc/profile #修改历史记录的数量 46 HISTSIZE=1000
命令别名 (alias)
1
[root@liruilong ~]$ alias#查看当前用户可以使用的别名
1 2 3 4
[root@liruilong ~]$ vim /root/.bashrc #重开一个终端生效,针对root用户生效 alias ens33='cat /etc/sysconfig/network-scripts/ifcfg-ens33' [root@liruilong ~]$ vim /etc/bashrc #重开一个终端生效,针对所有用户生效 alias ens33='cat /etc/sysconfig/network-scripts/ifcfg-ens33'
[root@liruilong ~]$ vim /root/shell/day01/user.sh #!/bin/bash #test positional parameters. echo$1 echo$2 echo$3 [root@liruilong ~]$ bash /root/shell/day01/user.sh aa 99 cc aa 99 cc
通过位置变量创建系统账户,配置密码
1 2 3 4 5 6 7 8
[root@liruilong ~]$ vim /root/shell/day01/user02.sh #!/bin/bash #read username and pass from positional parameters. useradd "$1"#创建用户 echo"$2" | passwd --stdin "$1"#给用户设置密码 [root@liruilong ~]$ sh /root/shell/day01/user02.sh tom 123 更改用户 tom 的密码 。 passwd:所有的身份验证令牌已经成功更新。
[root@liruilong ~]# touch a b c #创建的是三个文件 [root@liruilong ~]# touch "a b c" #双引号让其代表一个文件 [root@liruilong ~]# touch 'b c d' #单引号作用这里是与双引号相同 [root@liruilong ~]# ls -l #查看 -rw-r--r-- 1 root root 0 7月 6 09:36 a -rw-r--r-- 1 root root 0 7月 6 09:36 b -rw-r--r-- 1 root root 0 7月 6 09:36 c -rw-r--r-- 1 root root 0 7月 6 09:48 a b c -rw-r--r-- 1 root root 0 7月 6 09:48 b c d [root@liruilong ~]# rm -rf "a b c" #删除时要引号引起来 [root@liruilong ~]# rm -rf 'b c d'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[root@liruilong ~]# echo $HOME #输出家目录路径 /root [root@liruilong ~]# echo '$HOME' #只输出 $HOME 字符串,''屏蔽特殊符号的含义 $HOME [root@liruilong ~]# echo '$USER id is $UID' $USER id is $UID [root@liruilong ~]# echo $USER id is $UID root id is 0 [root@liruilong ~]# test=`grep root /etc/shadow` [root@liruilong ~]# test01=$(grep root /etc/shadow) [root@liruilong ~]# echo $test root:$6$Uebh9/WEzEaQfT$8b0B6oBgWUTEmYojDW9.6PHOw0.jD7A.SENsHFD/YPwh/L9jRJK0yWAtRF4BEteYZETeMiInp72dTviSKmLZf.:18501:0:99999:7::: dockerroot:!!:18691:::::: [root@liruilong ~]# echo $test01 root:$6$Uebh9/WEzEaQfT$8b0B6oBgWUTEmYojDW9.6PHOw0.jD7A.SENsHFD/YPwh/L9jRJK0yWAtRF4BEteYZETeMiInp72dTviSKmLZf.:18501:0:99999:7::: dockerroot:!!:18691:::::: [root@liruilong ~]#
[root@liruilong ~]# cd /root/shell/day03/ [root@liruilong day03]# touch {a,b,c,d,e,f}.txt [root@liruilong day03]# vim /root/shell/day03/rename1.sh #!/bin/bash ####批量修改目录下的以.txt结尾的文件为.doc结尾 for i in $(ls *.txt) do mv $i${i%.*}.doc #通过字符串去尾的去做 done ###脚本验证 [root@liruilong day03]# sh ./rename1.sh [root@liruilong day03]# ls [root@liruilong day03]# vim /root/shell/day03/rename2.sh #!/bin/bash ###批量修改文件扩展名,$1为原扩展名,$2为新扩展名 for i in $(ls *.$1) do mv $i${i%.*}.$2 done ###脚本验证 [root@liruilong day03]# sh ./rename1.sh doc pdf [root@liruilong day03]# ls
[root@liruilong ~]# echo "hello the world" | sed -r 's/^(.)(.*)(.)$/\3\2\1/' dello the worlh [root@liruilong ~]# echo "hello the world" | sed -r 's/^(.)(.*)(.)$/\3\1\2/' dhello the worl [root@liruilong ~]# echo "hello the world" | sed -r 's/^(.)(.*)(.)$/\3\3/' dd [root@liruilong ~]# echo "hello the world" | sed -r 's/^(.)(.*)(.)$/\3\3\2\1\1/' ddello the worlhh
1.8 打印行号
1 2 3 4 5 6 7 8 9 10 11 12 13 14
##打印第2行的行号 [root@liruilong ~]# sed -n '2=' /etc/passwd 2 ##打印包含root的行号 [root@liruilong ~]# sed -n '/root/=' /etc/passwd 1 10 ##打印bash结尾的行的行号 [root@liruilong ~]# sed -n '/bash$/=' /etc/passwd 1 42 ##$代表最后一行,打印最后一行的行号 [root@liruilong ~]# sed -n '$=' /etc/passwd 42
[root@liruilong ~]# mkdir /root/shell/day06 [root@liruilong ~]# vim /root/shell/day06/star.sh #!/bin/bash ####################### #此循环的作用是输出5个*在同一行 for i in {1..5} do echo -n "*" done echo echo ######################## #内层for循环在一行输出5个*;外层for循环,将内层循环执行5遍 for i in {1..5} do for j in {1..5} do echo -n "*" done echo done #####执行脚本 [root@liruilong ~]# sh /root/shell/day06/star.sh ***** ***** ***** ***** ***** *****
[root@liruilong ~]# mkdir -p /root/shell/day06 [root@liruilong ~]# vim /root/shell/day06/star.sh #!/bin/bash ####################### #此循环的作用是输出5个*在同一行 for i in {1..5} do echo -n "*" done echo echo ######################## #内层for循环在一行输出5个*;外层for循环,将内层循环执行5遍 for i in {1..5} do for j in {1..5} do echo -n "*" done echo done
[root@liruilong ~]# vim /root/shell/day06/bak_log.sh #!/bin/bash ###此脚本运用日期定义备份的文件名,方便与每天进行备份不重复 date=`date +"%Y%m%d"` if [ ! -f /tmp/log-$date.tar.gz ];then tar -zcPf /tmp/log-$date.tar.gz /var/log fi ###执行脚本并检查输出结果 [root@liruilong ~]# sh /root/shell/day06/bak_log.sh [root@liruilong ~]# ls /tmp/log-* /tmp/log-20200718.tar.gz
[root@liruilong ~]# vim /root/shell/day06/mysqldump.sh #!/bin/bash ###date 指定备份数据名;iuser 指定登录数据库的用户 ###ipass 指定登录密码,默认为空;db 指定要备份的数据库 date=$(date +"%Y%m%d") iuser=root ipass= db=mysql ###文件在/tmp 下不存在时才会进行备份 if [ ! -f /tmp/$db-$date.sql ];then mysqldump -u$iuser --password="$ipass"$db > /tmp/$db-$date.sql fi
1 2 3 4
###运行脚本 [root@liruilong ~]# sh /root/shell/day06/mysqldump.sh [root@liruilong ~]# ls -l /tmp/mysql-* -rw-r--r-- 1 root root 514619 7月 18 12:54 /tmp/mysql-20200718.sql
物理备份(buttaidong)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[root@liruilong ~]# vim /root/shell/day06/bak_mysql.sh #!/bin/bash ###对数据库中的mysql库下每一个表都进行打包备份;备份文件存放在/tmp/mysql目录下 date=$(date +"%Y%m%d") db_dir="/var/lib/mysql" db=mysql [ ! -d /tmp/$db ] && mkdir /tmp/$db for i in $(ls $db_dir/$db) do tar -zcf /tmp/$db/$i-$date.tar.gz $db_dir/$db/$i done ###运行脚本 [root@liruilong ~]# sh /root/shell/day06/bak_mysql.sh [root@liruilong ~]# ls /tmp/mysql
mysql> create databases liruilong; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databases liruilong' at line 1 mysql> create database liruilong; Query OK, 1 row affected (0.02 sec)
[root@liruilong ~]# vim /root/shell/day06/data.sh #!/bin/bash ###为/etc/下所有以 .conf 结尾的文件生成hash值,保存到/tmp/data.log文件中 for i in $(ls /etc/*.conf) do md5sum $i >> /tmp/data.log done
1 2 3
##运行脚本,查看结果 [root@liruilong ~]# sh /root/shell/day06/data.sh [root@liruilong ~]# cat /tmp/data.log
[root@liruilong ~]# vim /root/shell/day06/ssh_conf.sh #!/bin/bash ####安全的远程配置脚本 conf="/opt/sshd_config" sed -i '/^Port/s/22/1122/'$conf sed -i '/^PermitRootLogin/s/yes/no/'$conf sed -i '/^UseDNS/s/yes/no/'$conf sed -i '$a AllowUsers tom'$conf systemctl restart sshd
1 2 3 4 5 6 7 8 9 10 11 12
###运行脚本 [root@liruilong opt]# sh /root/shell/day06/ssh_conf.sh ###查看结果 [root@liruilong ~]# sed -n '/^Port/p' /opt/sshd_config Port 1122 [root@liruilong ~]# sed -n '/^PermitRootLogin/p' /opt/sshd_config PermitRootLogin no [root@liruilong ~]# sed -n '/^UseDNS/p' /opt/sshd_config UseDNS no [root@liruilong ~]# sed -n '$p' /opt/sshd_config AllowUsers tom
###输出多行文本 [root@liruilong ~]# cat << EOF > hello world > i am come here > i love the world > EOF hello world i am come here i love the world ###将多行文本保存到文件/opt/password中 [root@liruilong ~]# cat > /opt/password << EOF > hello world > i am come here > i love the world > EOF ###查看文件内容跟 [root@liruilong ~]# cat /opt/password hello world i am come here i love the world [root@liruilong ~]# vim /root/shell/day06/pxe.sh #!/bin/bash ##定义变量,让方便后面的调用,和提高适用性 DHCP_NET=192.168.4.0 DHCP_NETMASK=255.255.255.0 DHCP_MINIP=192.168.4.100 DHCP_MAXIP=192.168.4.200 DHCP_ROUTER=192.168.4.254 DHCP_NEXT_SERVER=192.168.4.5 HTTP_IP=192.168.4.5 ##安装软件包 yum -y install httpd dhcp tftp-server syslinux ##临时停用SELinux setenforce 0 ##配置 DHCP 服务 cat > /etc/dhcp/dhcpd.conf << EOF subnet $DHCP_NET netmask $DHCP_NETMASK { range $DHCP_MINIP $DHCP_MAXIP; option routers $DHCP_ROUTER; default-lease-time 600; max-lease-time 7200; next-server $DHCP_NEXT_SERVER; filename "pxelinux.0"; } EOF systemctl restart dhcpd systemctl enable dhcpd ##配置httpd共享服务器 if [ ! -e /dev/cdrom ];then echo"未检测到系统光盘/dev/cdrom,请插入光盘后再试" exit fi [ -d /var/www/html/cdrom ] || mkdir /var/www/html/cdrom mount /dev/cdrom /var/www/html/cdrom systemctl start httpd ##配置kickstart 文件 cat > /var/www/html/ks.cfg << EOF install keyboard 'us' rootpw --plaintext redhat url --url="http://$HTTP_IP/cdrom" lang en_US firewall --disabled auth --useshadow --passalgo=sha512 text selinux --disabled network --bootproto=dhcp --device=eth0 reboot timezone Asia/Shanghai bootloader --location=mbr zerombr clearpart --all --initlabel part /boot --fstype="xfs" --size=500 part / --fstype="xfs" --grow --size=1 %packages @base %end EOF ####配置tftp服务 cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ cp /var/www/html/cdrom/isolinux/* /var/lib/tftpboot/ [ -d /var/lib/tftpboot/pxelinux.cfg ] || mkdir /var/lib/tftpboot/pxelinux.cfg ##部署菜单文件 cat > /var/lib/tftpboot/pxelinux.cfg/default << EOF default vesamenu.c32 timeout 100 label linux menu label ^Install CentOS 7 kernel vmlinuz append initrd=initrd.img ks=http://$HTTP_IP/ks.cfg EOF systemctl start tftp ##临时清空防火墙规则 iptables -F ###执行脚本 [root@liruilong ~]# sh /root/shell/day06/pxe.sh ###检查三个服务【dhcpd,httpd,tftp】是否已经运行 [root@liruilong ~]# systemctl status dhcpd | grep Active Active: active (running) since 一 2020-07-20 12:06:31 CST; 16min ago [root@liruilong ~]# systemctl status tftp | grep Active Active: active (running) since 一 2020-07-20 12:05:35 CST; 17min ago [root@liruilong ~]# systemctl status httpd | grep Active Active: active (running) since 一 2020-07-20 12:05:34 CST; 17min ago