shell习题-29

匿名 (未验证) 提交于 2019-12-03 00:03:02

题目要求

用shell脚本实现,部署mysql主从,假设两台机器上已经安装了mysql,并且目前无新库。

参考答案

#!/bin/bash #这个脚本用来配置MySQL主从同步 #作者:猿课-阿铭 www.apelearn.com #日期:2018-12-17  #!/bin/bash master_ip=192.168.100.12 slave_ip=192.168.100.13 mysqlc="mysql -uroot -paminglinux"  check_ok() {     if [ $? -ne 0 ]     then         echo "$1 出错了。"         exit 1     fi }  f_exist() {     d=`date +%F%T`     if [ -f $1 ]     then         mv $1 $1_$d     fi }  ## 设置主mysql配置 if ! grep '^server-id' /etc/my.cnf then     sed -i '/^\[mysqld\]$/a\server-id = 1001' /etc/my.cnf fi  if ! grep '^log-bin.*=.*' /etc/my.cnf then     sed -i '/^\[mysqld\]$/a\log-bin = aminglinux' /etc/my.cnf fi  sed -i '/^log-bin.*/a\binlog-ignore-db = mysql ' /etc/my.cnf  /etc/init.d/mysqld restart check_ok "主上重启mysql"  ## 登录mysql,授权用户、锁表以及show master status。 $mysqlc <<EOF     grant replication slave on *.* to 'repl'@$slave_ip identified by 'yourpassword';     flush tables with read lock; EOF $mysqlc -e "show master status" > /tmp/master.log file=`tail -1 /tmp/master.log|awk '{print $1}'` pos=`tail -1 /tmp/master.log|awk '{print $2}'`  ## 创建在从上配置和操作的脚本 f_exist /tmp/slave.sh  cat > /tmp/slave.sh << EOF #!/bin/bash if ! grep '^server-id' /etc/my.cnf then     sed -i '/^\[mysqld\]$/a\server-id = 1002' /etc/my.cnf fi  /etc/init.d/mysqld restart check_ok "从上重启mysql"  $mysqlc  <<EOF     stop slave;     change master to master_host="$master_ip", master_user="repl", master_password="yourpassword", master_log_file="$file", master_log_pos=$pos;     start slave; EOF  EOF  ## 创建传输slave.sh的expect脚本 f_exist /tmp/rs_slave.expect  cat > /tmp/rs_slave.expect <<EOF #!/usr/bin/expect set passwd "aminglinux" spawn rsync -a /tmp/slave.sh root@$slave_ip:/tmp/slave.sh expect {     "yes/no" { send "yes\r"}     "password:" { send "\$passwd\r" } } expect eof EOF  ## 执行expect脚本 chmod +x /tmp/rs_slave.expect /tmp/rs_slave.expect check_ok "传输slave.sh"  ## 创建远程执行命令的expect脚本 f_exist /tmp/exe.expect  cat > /tmp/exe.expect <<EOF #!/usr/bin/expect set passwd "aminglinux" spawn ssh root@$slave_ip expect {     "yes/no" { send "yes\r"}     "password:" { send "\$passwd\r" } } expect "]*" send "/bin/bash /tmp/slave.sh\r" expect "]*" send "exit\r" EOF  ## 执行expect脚本 chmod +x /tmp/exe.expect /tmp/exe.expect check_ok "远程执行slave.sh"  ## 主上解锁表 $mysqlc -e "unlock tables"

题目要求

写一个脚本,实现一键管理docker容器,比如启动、关闭、删除容器等操作。
要求:

  1. 脚本支持启动全部容器、关闭全部容器、删除全部容器
  2. 需要提示用户如何使用该脚本,需给出范例

参考答案

#!/bin/bash #这个脚本用来管理docker容器 #作者:猿课-阿铭 www.apelearn.com #日期:2018-12-17  while true do     read -p "请输入你要执行的操作:(stop/start/rm) " opt     if [ -z "$opt" ]     then         echo "请输入要执行的操作。"         continue     else         break     fi done  docker ps -a |awk '{print $1}' > /tmp/id.txt case $opt in   stop)     for id in `cat /tmp/id.txt`     do         docker stop $id     done     ;;   start)     for id in `cat /tmp/id.txt`     do         docker start $id     done   rm)     for id in `cat /tmp/id.txt`     do         read -p "将要删除容器$id,是否继续?(y|n)" c         case $c in           y|Y)             docker rm -f $id             ;;           n|N)             echo "容器$id不会被删除。"             ;;           *)             echo "你只能输入'y'或者'n'。"             ;;         esac     done   *)     echo "你只能输入start/stop/rm。"     ;; esac

题目要求

写个shell脚本,能够实现一键安装并配置samba服务,执行该脚本时需要带一个参数,为共享的目录,目录若不存在,需自动创建。

要求,任何人都可以访问,且不用密码,并且目录是只读的。

参考答案

#!/bin/bash #这个脚本用来一键安装并配置samba #作者:猿课-阿铭 www.apelearn.com #日期:2018-12-17  if [ "$#" -ne 1 ] then     echo "运行脚本的格式为:$0 /dir/"     exit 1 else     if ! echo $1 |grep -q '^/.*'     then         echo "请提供一个绝对路径。"         exit 1     fi fi  if ! rpm -q samba >/dev/null then     echo "将要安装samba"     sleep 1     yum install -y samba     if [ $? -ne 0 ]     then         echo "samba安装失败"         exit 1     fi fi  cnfdir="/etc/samba/smb.conf" cat >> $cnfdir <<EOF [share]         comment = share all         path = $1         browseable = yes         public = yes         writable = no EOF  if [ ! -d $1 ] then     mkdir -p $1 fi  chmod 777 $1 echo "test" > $1/test.txt  #假设系统为CentOS7 systemctl start smb if [ $? -ne 0 ] then     echo "samba服务启动失败,请检查配置文件是否正确。" else     echo "samba配置完毕,请验证。" fi

题目要求

假如公司的一个业务,域名为www.aminglinux.com,现在有5台机器在跑。为了快速查看这5台机器的负载,需要你写一个Shell脚本,运行脚本后,就能一下子把5台机器的负载全部打印出来。

参考答案

#!/bin/bash #这个脚本用来批量查机器负载 #作者:猿课-阿铭 www.apelearn.com #日期:2018-12-17  for ip in `cat /tmp/ip.list` do     echo $ip      ssh $ip "uptime" done

题目要求

我们使用的云主机,购买一块云盘后,默认并不是挂载状态的,用shell写一个脚本,只要把盘符和挂载点以参数的形式提供给脚本,该脚本就可以自动格式化、挂载。

要求:

  1. 不用分区,直接格式化
  2. 格式化为ext4文件系统类型

参考答案

#!/bin/bash #这个脚本用来自动挂载磁盘 #作者:猿课-阿铭 www.apelearn.com #日期:2018-12-17  if [ $# -ne 2 ] then     echo "Useage $0 盘符 挂载点, 如: $0 /dev/xvdb /data"     exit 1 fi #检测 $1设备是否存在 if [ ! -b $1 ] then     echo "你提供的盘符不正确,请检查后再操作"     exit 1 fi #格式化$1 echo "格式化$1" mkfs -t ext4 $1 #检测$2目录是否存在 if [ ! -d $2 ] ;then         mkdir -p $2 fi #检测/etc/fstab/ 中是否已经挂载该目录 n=`awk '$NF == "$2"' /etc/fstab|wc -l` if [ $n -eq 0 ] then     echo "$1              $2                      ext4   defaults  0  0" >> /etc/fstab     mount -a else     echo "配置文件/etc/fstab中已经存在挂载点$2,请检查一下."     exit 1 fi
转载请标明出处:shell习题-29
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!