1.使用shell管理数据库
实验环境:安装好数据库并安全初始化。
实验一:建立三个名为tom,harry,natash的数据库,每个数据库建立一个表,然后备份数据库
[root@localhost mnt]# cat mariadb.sh
#! /bin/bash
passwd="redhat"
for i in tom1 harry1 natash1
do
mysql -uroot -p$passwd -e "CREATE DATABASE $i;"
mysql -uroot -p$passwd -e "USE $i; CREATE TABLE 1_$i(username varchar (50) not null);"
mysqldump -uroot -p$passwd $i > /mnt/$i.sql
done
[root@localhost mnt]# sh mariadb.sh
[root@localhost mnt]# ls
black.sh function5.sh function9.sh overall2.sh trap2.sh
function1.sh function6.sh harry1.sql overall.sh trap.sh
function3.sh function7.sh mariadb.sh tiaoban.sh xargs.sh
function4.sh function8.sh natash1.sql tom1.sql
实验二:数据库备份,执行script.sh $dbpasswd 备份数据库中所有库到/mnt/mysqldump目录中,备份文件名称为“库名称.sql”,当此文件存在时进入交互模式,询问动作,输入“s”跳过备份,输入“b”,备份“库名称.sql”为“库名称_backup.sql”,输入“O”时,覆盖原文件,e表示退出
2.数组
1)数组定义方法:
方法1:
[root@localhost ~]# array=(1 2 3)
[root@localhost ~]# echo ${array[*]}
1 2 3
方法2:
[root@localhost ~]# array=([1]=one [2]=two [3]=three)
[root@localhost ~]# echo ${array[*]}
one two three
方法3:
[root@localhost ~]# array[0]=a
[root@localhost ~]# array[1]=b
[root@localhost ~]# array[2]=c
[root@localhost ~]#
[root@localhost ~]# echo ${array[1]}
b
方法4:动态定义数组变量,并使用命令的输出结果作为数组的内容
[root@localhost ~]# array=($(ls /array))
[root@localhost ~]# echo ${array[*]}
1.txt 2.txt 3.txt
2)数组的打印和输出
一、打印数组元素
[root@localhost ~]# array=(one two three)
[root@localhost ~]# echo ${array[0]}
one
[root@localhost ~]# echo ${array[1]}
two
[root@localhost ~]# echo ${array[*]}
one two three
[root@localhost ~]# echo ${array[@]}
one two three
二、打印元素个数
[root@localhost ~]# echo ${#array[*]}
3
[root@localhost ~]# echo ${#array[@]}
3
三、数组的赋值
#如果下标不存在,则自动添加一个新的元素,如果存在,则覆盖原来的值
[root@localhost ~]# array[3]=four
[root@localhost ~]# echo ${array[*]}
one two three four
[root@localhost ~]#
[root@localhost ~]# array[0]=westos
[root@localhost ~]#
[root@localhost ~]# echo ${array[*]}
westos two three four
四、数组的删除
[root@localhost ~]# unset array[1]
[root@localhost ~]# echo ${array[*]}
westos three four
[root@localhost ~]# unset array
五、数组内容截取和替换
[root@localhost ~]# echo ${array[@]:1:3}
2 3 4
[root@localhost ~]# array=($(echo {a..z}))
[root@localhost ~]# echo ${array[@]}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@localhost ~]#
[root@localhost ~]# echo ${array[@]:1:3}
b c d
替换
[root@localhost ~]# array=(1 2 3 1 1)
[root@localhost ~]#
[root@localhost ~]# echo ${array[@]/1/b}
b 2 3 b b
实验1:循环输出数组元素
[root@localhost mnt]# cat shuzu.sh
#!/bin/bash
array=(1 2 3 4 5)
for ((i=0;i<${#array[*]};i++))
do
echo ${array[i]}
done
[root@localhost mnt]# sh shuzu.sh
1
2
3
4
5
实验2:利用for循环打印下面这句话中字母个数小于于6的单词
I am westos teacher welcome to westos training class
[root@localhost mnt]# cat selcard.sh
#!/bin/bash
arr=(I am westos teacher welcome to westos training class)
for ((i=0;i<${#arr[*]};i++))
do
if [ ${#arr[$i]} -lt 6 ];then
echo "${arr[$i]}"
fi
done
[root@localhost mnt]# sh selcard.sh
I
am
to
class
3.随机数的生成
方法1:$RANDOM
[root@localhost mnt]# echo $RANDOM
10052
[root@localhost mnt]# echo $RANDOM
8108
方法二:openssl
openssl rand -base64 40
命令 64位编码 取40位
实验:使用for循环在/westos目录下批量创建10个html文件
其中每个文件需要包含10个随即小写字母加固定字符串westos
[root@localhost mnt]# cat openssl.sh
#!/bin/bash
Path=/westos ##路径变量
[ -d "$Path" ] || mkdir -p $Path ##查看路径是否存在,不存在创建路径
for i in `seq 10` ##循环开始
do
random=$(openssl rand -base64 40 | sed 's/[^a-z]//g' | cut -c 3-12) ##sed为选取小写字母,cut为选择其中的10个
touch $Path/${random}_westos.html
done
[root@localhost mnt]# sh openssl.sh
[root@localhost mnt]# ls /westos/
akkrqxsjdy_westos.html lmaobflvcg_westos.html rgkxnsufuu_westos.html
bsmvvimyrq_westos.html odhxyeoapu_westos.html yzcrfetjzw_westos.html
ejqvxdfwbg_westos.html pybfgbqqrj_westos.html
iambwtxlaa_westos.html qyugtczlcr_westos.html
方法三:使用hash函数生成随机数
[root@localhost mnt]# echo "westos$RANDOM" | md5sum |cut -c 7-14
ef1eea66
此命令的含义为将westos+随机数,进行md5哈希运算,然后选择其中的7-14位
实验:创立10个名字位westos01-westos10的用户,并且把密码改为随机数
[root@localhost mnt]# cat user.sh
#!/bin/bash
. /etc/init.d/functions ##使用一些函数库
user="westos" ##定义变量
passfile="/tmp/user.log" ##定义路径
for num in `seq -w 10` ##循环开始,-w 位输出01,02,03........
do
pass="`echo $RANDOM | md5sum | cut -c 3-12`" ##生成随机数作为密码
useradd $user$num &> /dev/null && { ##用户创立不显示
echo "$pass" | passwd --stdin $user$num &> /dev/null ##修改密码位pass
echo -e "user:$user$num\tpasswd:$pass" >> $passfile ##将密码保存
}
if [ $? -eq 0 ];then ##判断是否成功
action "$user$num is ok" /bin/true ##action,和/bin/true 都是函数
else
action "$user$num is failed" /bin/false
fi
done
[root@localhost mnt]# sh user.sh
westos01 is ok [ OK ]
westos02 is ok [ OK ]
westos03 is ok [ OK ]
westos04 is ok [ OK ]
westos05 is ok [ OK ]
westos06 is ok [ OK ]
westos07 is ok [ OK ]
westos08 is ok [ OK ]
westos09 is ok [ OK ]
westos10 is failed [FAILED]
实验结果
[root@localhost mnt]# cat /tmp/user.log
user:westos01 passwd:ca2d1cc946
user:westos02 passwd:c3c95df19a
user:westos03 passwd:1ba3c1cd27
user:westos04 passwd:c497c8cc49
user:westos05 passwd:3eb621a1f9
user:westos06 passwd:449dd3d206
user:westos07 passwd:19e2306d3e
user:westos08 passwd:0047e9ae2b
user:westos09 passwd:12e85f8974
[root@localhost mnt]# su - student
Last login: Sun Jan 6 08:35:15 EST 2019 on pts/0
[student@localhost ~]$ su - westos01
Password:
[westos01@localhost ~]$
4.linux6.5环境的使用
1)网络的配置:
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 ##vim命令变为vi
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.13
NETMASK=255.255.255.0
[root@localhost ~]# /etc/init.d/network restart ##系统的开启是使用脚本,在/etc/init.d目录下
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: Determining if ip address 172.25.254.13 is already in use for device eth0...
[ OK ]
2)yum源的配置
[root@localhost init.d]# vi /etc/yum.repos.d/rhel-source.repo
[root@localhost init.d]# cat /etc/yum.repos.d/rhel-source.repo
[Server]
name=rhel7.0
baseurl=http://172.25.254.85/6.5yumpak
gpgcheck=0
[root@localhost init.d]# yum repolist
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Server | 3.9 kB 00:00
repo id repo name status
Server rhel7.0 3,690
repolist: 3,690
3)系统的管理
开启,关闭,重启,状态查看:6.5以前的服务的启动是依靠脚本执行的,在/etc/init.d目录下
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# ls
auditd halt lvm2-lvmetad network rhnsd saslauthd
blk-availability ip6tables lvm2-monitor postfix rhsmcertd single
crond iptables netconsole rdisc rsyslog sshd
functions killall netfs restorecond sandbox udev-post
4)nginx服务的安装
Nginx是一个轻量级、高性能、稳定性高、并发性好的HTTP和反向代理服务器。也是由于其的特性,其应用非常广。
步骤一:传送nginx的压缩包
yum install openssh-clients -y ##安装这个,可以使用scp命令
[kiosk@foundation85 html]$ scp ~/Desktop/12.31/nginx-1.14.0.tar.gz root@172.25.254.13:~
root@172.25.254.13's password:
nginx-1.14.0.tar.gz 100% 992KB 992.5KB/s 00:00
步骤二:解压安装包,并删除一些debug
tar zxf nginx-1.14.0.tar.gz ##解压
cd nginx-1.14.0 ##进入目录
vi src/core/nginx.h ##编辑修改14行为下面
14 #define NGINX_VER "nginx/"
vi auto/cc/gcc ##编辑修改172行为下面
172 #CFLAGS="$CFLAGS -g"
步骤三:安装nginx和支持的安装包
yum install gcc -y
yum install pcre-devel -y
yum install openssl-devel -y
./configure --prefix=/usr/local/nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginxls
make && make install
步骤四:编写默认发布目录,启动nginx
实验前先关闭火墙,6.5为关闭iptable
iptables -F
默认发布目录:
/usr/local/nginx/html/index.html
[root@localhost html]# cat index.html
<h1>server2-172.25.254.13</h1>
启动
[root@localhost html]# useradd nginx ##这里需要添加nginx用户
[root@localhost html]# nginx
实验结果
5)使用shell对nginx服务进行管理,可以开启,关闭nginx服务。
[root@localhost ~]# cat /mnt/nginx.sh
#!/bin/bash
. /etc/init.d/functions
function usage() {
echo $"usage:$0 {start|stop|restart}"
exit 1
}
function start() {
/usr/local/nginx/sbin/nginx
sleep 1
if [ `netstat -antlpe | grep nginx | wc -l` -ge 1 ];then
action "nginx is started." /bin/true
else
action "nginx is started." /bin/false
fi
}
function stop() {
killall nginx &> /dev/null
sleep 1
if [ `netstat -antlpe | grep nginx | wc -l` -eq 0 ];then
action "nginx is stoped." /bin/true
else
action "nginx is stoped." /bin/false
fi
}
function main() {
if [ $# -ne 1 ];then
usage $0
fi
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
usage $0
;;
esac
}
main $*
实验结果
[root@localhost ~]# sh /mnt/nginx.sh start
nginx is started. [ OK ]
[root@localhost ~]# sh /mnt/nginx.sh stop
nginx is stoped. [ OK ]
[root@localhost ~]# sh /mnt/nginx.sh restart
nginx is stoped. [ OK ]
nginx is started. [ OK ]