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
下一篇 2017-09-04

相关推荐

  • > 输出重定向

    重定向输出 > >> 追加 目 录 1、 >正确定向等同于1> 2、 2> 错误定向 3. &> 把错误导成正确的{洗钱} 4. 正确的变成错误的 5. > 覆盖 6. 举一反三 最近学的命令中学到了一些很有意思的命令,分享一下 > 重定向输出 最…

    Linux干货 2017-08-13
  • (一)Linux发行版及基础命令简述

    计算机 Linux 发行版 命令 帮助

    2017-12-11
  • 进程和计划任务

    一、知识整理 1、网络客户端工具:lftp,ftp,lftget,wget 子命令:get,mget,ls,help等 wget [opt] …[url] -q 静默模式 -c 断点续传 -O 保存位置 –limit-rates=  指定传输速率 登录ftp之后:lcd 在本机切换目录;get下载单个文件 !ls 查看本机文…

    Linux干货 2016-09-13
  • 程序包管理

    程序包 linux的程序包主要分为两类;二进制可执行安装包和源代码程序文件包     二进制应用程序的组成部分:     二进制文件、库文件、配置文件、帮助文件   查看二进制程序所依赖的库文件:     ldd&nbs…

    Linux干货 2016-05-30
  • Linux任务计划及内核管理

    作业管理:     前台作业:通过终端启动,且启动后一直占据终端;     后台作业:通过终端启动,但启动后随即转入后台运行(释放终端) 将作业放入后台的操作     1)运行中的作业:ctrl+z     2)还未执行的作业:command &   …

    Linux干货 2016-09-09
  • MongoDB-index

    索引:特殊的数据结构,存储表的数据的一小部分以实现快速查询 优点:     1、大大减少了服务器需要扫描的数据量     2、索引可以帮助服务器避免排序或使用临时表     3、索引可以将随机io转换为顺序io 索引评估:三星(非常好) 一…

    Linux干货 2016-11-27

评论列表(1条)

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

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