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

相关推荐

  • 关于加密那点事

    作者【Jev Tse】 环境:sentos6.8     【本文预览】      一、关于加密      二、对称加密      三、非对称加密      四、单向散列 …

    Linux干货 2016-12-01
  • vim编辑器

    vim编辑器三种模式转换 G跳到尾行 gg:跳到首行 H:跳到当前页的首行 L: 跳到当前页的尾行 M: 跳到当前而的中间行 dd: 删除光标所在的行 #dd:多行删除 yy:复制行 #yy: 复制多行  Y: 复制整行 cc:删除当前行并输入新内容 #cc:删除多行 C:删除当前光标到行尾,并切换成插入模式 修改配置文件 全局: /etc/vimrc 用…

    2017-08-05
  • shell脚本

    最近学了shell脚本,自己感觉挺有难度的,今天就简单整理一些shell脚本的练习和作业 练习:    1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。#!/bin/bash# ———&#…

    2017-08-05
  • iptables之froward

    先克隆一台centos服务器添加虚拟网卡 同时两个服务器配上host only的网卡 echo 1 > /proc/sys/net/ipv4/ip_forward打开网卡间转发 有回包 但是没有10.0.0.3还是没有ping通原因是192.168.68.134 的网关指向了192.168.68.2 而不是192.168.68.144添加一条指向192…

    2017-11-12
  • grub安装

    创建两块磁盘分区 创建boot目录和根本目录rootfs mkdir /mnt/boot mkdir /mnt/rootfs mount /dev/sdb1 /mnt/boot mount /dev/sdb2 /mnt/rootfs 安装gurb gurb-install –root-directory=/mnt /dev/sdb1 在/mnt/…

    Linux干货 2017-11-14
  • N22-第二周作业-对Linux的基础操作命令的理解及用法示例

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 Linux上文件管理类命令常用的有:pwd、ls、cd、cp、touch、mv、rm、rmdir   1)pwd:显示当前工作目录     2)ls:列出指定目录下的内容    常用的选项有:   -a:列出目录中的所有文…

    Linux干货 2016-08-22

评论列表(1条)

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

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