bash脚本编程实例

bash脚本编程实例

  • 1.写一个脚本

    • 能接受四个参数:start、stop、restart、status
    • start:输出“starting脚本名finished.”
    • restart:输出“restarting脚本名finished.”
    • stop:输出“stoping脚本名finished.”
    • status:输出“status脚本名finished.”
    • 其他任意参数,均报错退出

      #!/bin/bash
      case $1 in
      start)
      echo "starting ${0} finished."
      ;;
      stop)
      echo "stoping ${0} finished."
      ;;
      restart)
      echo "restarting ${0} finished."
      ;;
      status)
      echo "status ${0} finished."
      ;;
      *)
      echo "error input."
      exit 1
      ;;
      esac
  • 2.写一个脚本,判断给定的用户是否登录了当前系统

    • 如果登录了,则显示用户登录,脚本终止
    • 每三秒钟,查看一次用户是否登录

      #!/bin/bash
      if [ $# -eq 0 ];then
       echo "there is no username inputing."
       exit 1
      fi
      
      while true;do
       if id $1 &> /dev/null;then
           user=$(who|grep "^$1"|cut -d' ' -f1)
           if [[ $user == $1 ]];then
               echo "$1 has logined."
               break
           else
               sleep 3
           fi
       else
          echo "there is no user like $1."
          exit 2
      fi
      done
  • 3.写一个脚本,显示用户选定要查看的信息

    • cpu)display cpu info
    • mem)display memory info
    • disk)display disk info
    • quit)quit
    • 非此四项选择,则提示错误,并要求用户重新选择,直到其给出正确的选择为止

      #!/bin/bash
      cat << EOF
      cpu)display cpu info
      mem)display memory info
      disk)display disk info
      quit)quit
      EOF
      
      read -p "please input:" ops
      
      while true;do
       if [ $ops != "cpu" -a $ops != "mem" -a $ops != "disk" -a ops != "quit" ];then
         read -p "please input again:" ops
       else
         break
       fi
      done
      
      case $ops in
      cpu)
        lscpu
      ;;
      mem)
        free -m
      ;;
      disk)
        fdisk -l
      ;;
      quit)
        exit 0
      ;;
      esac
  • 4.写一个脚本

    • 用函数实现返回一个用户的ID和SHELL;用户名通过参数传递而来
    • 提示用户输入一个用户名或者输入“quit”退出
    • 当输入的是用户名,则调用函数显示用户信息
    • 当用户输入quit,则退出脚本,进一步地,显示键入的用户相关信息后,再次提醒输出用户名或者quit

      #!/bin/bash
      
      userinfo(){
       if [ -z $1 ];then
           return 1
       elif id $param &>> /dev/null;then
           echo "$1 ID is $(id -u $1)."
           echo "$1 shell is $(grep "^$1" /etc/passwd|cut -d: -f7)"
       else
           return 1
       fi
      
      }
      
      while true;do
       read -p "please input a username or quit:" param
       if [[ $param == "quit" ]];then
          exit 0
       else
          userinfo $param
          [ $? -eq 1 ]&&echo "no user named $1."
       fi
      done

原创文章,作者:N27_xiaoni,如若转载,请注明出处:http://www.178linux.com/85821

(2)
N27_xiaoniN27_xiaoni
上一篇 2017-09-04 11:14
下一篇 2017-09-04 11:43

相关推荐

  • 马哥教育网络班22期-第1周博客作业2

    date命令 date – print or set the system date and time 显示或设置系统日期和时间 【SYNOPSIS】 date [OPTION]… [+FORMAT] 【OPTIONS】 -d:–date=STRING,显示字符串所指的日期与时间, 而不是当前时间,字符串前后必须加上双引号…

    Linux干货 2016-08-15
  • 第九周博客作业

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现 #!/bin/bash declare -i sum=0 declare -i sum_nologin=0 for i in $(cut -d: -f7 /etc/passwd);do if…

    Linux干货 2017-06-06
  • 马哥教育网络班22期+第十一周课程练习

    1、详细描述一次加密通讯的过程,结合图示最佳。第一阶段:ClientHello:    客户端(通常是浏览器)先向服务器发出加密通信的请求,这被叫做ClientHello请求。客户端向服务器提供以下信息:        支持的协议版本,比如TLS 1.2 &n…

    Linux干货 2016-12-27
  • 数据结构- 串的模式匹配算法:BF和 KMP算法

    Brute-Force算法的思想 1.BF(Brute-Force)算法   Brute-Force算法的基本思想是: 1) 从目标串s 的第一个字符起和模式串t的第一个字符进行比较,若相等,则继续逐个比较后续字符,否则从串s 的第二个字符起再重新和串t进行比较。 2) 依此类推,直至串t 中的每个字符依次和串s的一个连续的字符序列相等,则称模式匹…

    Linux干货 2015-04-07
  • Mariadb数据库复制系列(一):主从复制

      实验一:MySQL主从复制的实现 1、实验环境 确保各节点之间的时间同步 2、修改主节点的mysql服务配置文件,让其符合主从架构中主节点的要求 3、查看主节点现有数据情况和二进制日志使用情况 4、对主节点的数据利用xtrabackup进行完全备份 由于两个从节点是后期加入,故采取备份主节点的数据,还原到从节点上,然后让从节点从备份时刻的二进制日志的位置…

    2016-11-24
  • 马哥教育网络班21期+第5周作业

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行(以/boot/grub2/grub.cfg代替); [root@localhost ~]# grep '^[[:space:]]\+' /boot/grub2/grub.cfg    load_e…

    Linux干货 2016-08-08

评论列表(1条)

  • 马哥教育
    马哥教育 2017-10-10 13:02

    一开始的内容比较基础,但是非常重要,这篇博客整理的很不错,再接再励。