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

相关推荐

  • 简述一些基础指令

    tree命令 tree -L 1 -d /用来查看目录结构 -L 指定层数 -d 只查看目录 /boot 跟内核有关的文件 grub 内核和BootLoader ├── bin 存放用户使用的基本命令(可执行程序,二进制文件) 不能单独分区的 ├── boot 跟内核有关的文件 grub 内核和BootLoader&n…

    Linux干货 2017-04-03
  • DNS配置案例 二

    DNS配置案例 二 DNS配置案例 二 1 §·DNS的配置与案例 1 §·测试工具 3 §·主配置文件格式:/etc/named.conf 7 §·缓存名称服务器的配置: 8 §·配置解析一个正向区域 : 以loveme.com域为例: 10 §·配置解析一个反向区域 : 以loveme.com域为例: 15 §·配置从DNS服务器 19 §·问…

    Linux干货 2016-09-26
  • bash功能特性四 文件名通配符

    文件名通配(globbing)     通配符在bash中是一个非常有用的功能,它可以使我们更加方便的查找符合特定条件的文件。     文件通配符的包括以下几种:          *:任意长度的任意…

    Linux干货 2015-04-21
  • 16 文本处理工具

    文本处理工具一 一、杂项知识整理 1、ps axo user,ruser,cmd 查看命令发起者的身份和真正的登录身份: [root@localhost test]# ps axo user,ruser,cmd USER     RUSER  &nb…

    Linux干货 2016-08-05
  • 马哥教育网络班22期+第2周课程练习

    1、  Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 cp命令 单源复制:cp [OPTION]… [-T] SOURCE DEST 多源复制:cp [OPTION]… SOURCE… DIRECTORY 多源复制:cp [OPTION]… -t DIRECTORY SOU…

    Linux干货 2016-08-22

评论列表(1条)

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

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