Linux基础之shell脚本编程(四)

1、写一个脚本

  (1) 能接受四个参数:start, stop, restart, status

   start: 输出“starting 脚本名 finished.”

  (2) 其它任意参数,均报错退出;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 flag=0
  5 if [ $# -eq 1 ];then
  6   case $1 in
  7     start)
  8       echo "start $0 finished"
  9       filg=1
 10       ;;
 11     stop)
 12       echo "stop $0 finished"
 13       flag=0
 14       ;;
 15     restart)
 16       echo "restart $0 finished"
 17       flag=1
 18       ;;
 19     status)
 20       if [ $flag -eq 1 ];then
 21         echo "$0 is running..."
 22       else
 23         echo "$0 stopped...."
 24       fi
 25       ;;
 26     *)
 27       echo "invalid argument,usage:$0 {start|stop|status|restart}"
 28       ;;
 29   esac
 30 else
 31   echo "usage : $0 {start|stop|status|restart}"
 32 fi

2、写一个脚本,判断给定的用户是否登录了当前系统;

  (1) 如果登录了,则显示用户登录,脚本终止;

  (2) 每3秒钟,查看一次用户是否登录;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 read -p "please input username:" username
  5 until who | grep "\b$username\b" &>/dev/null ;do
  6   echo "$username not login "
  7   sleep 3
  8 done
  9 echo "$username login!!"
 10 exit 0
 11

3、写一个脚本,显示用户选定要查看的信息;

   cpu) display cpu info

   mem) display memory info

   disk) display disk info

   quit) quit

   非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 echo  "please input what you want to do:"
  5 echo "cpu: display cpu info"
  6 echo "mem: display mem info"
  7 echo "disk: diplay disk info"
  8 echo "quit: quit this script"
  9 read  input
 10 until [ $input == "cpu" -o $input == "mem" -o $input == "disk" -o $input == "quit" ] ; do
 11   read -p "error input,please input what you want to do:" input
 12 done
 13 case $input in
 14   cpu)
 15     lscpu
 16     ;;
 17   mem)
 18     cat /proc/meminfo
 19     ;;
 20   disk)
 21     fdisk -l
 22     ;;
 23   quit)
 24     echo "quit..."
 25     exit 0
 26     ;;
 27 esac
 28

4、写一个脚本

  (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;

  (2) 提示用户输入一个用户名或输入“quit”退出;

    当输入的是用户名,则调用函数显示用户信息;

    当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit: 

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 function findUser
  5 {
  6   if id $1 &>/dev/null;then
  7     userid=$(grep "^\b$1\b" /etc/passwd | cut -d":" -f3)
  8     usershell=$(grep "^\b$1\b" /etc/passwd | cut -d":" -f7)
  9     echo "user $1: uid=$userid, shell=$usershell "
 10   else
 11     echo "the user not exist"
 12   fi
 13 }
 14 
 15 echo "input a user name for the user info,input quit for quit:" 
 16 read input
 17 until [ $input == "quit" ];do
 18   findUser $input
 19   echo "please input again or quit" 
 20   read input
 21 done
 22

5、写一个脚本,完成如下功能(使用函数)

   (1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;

   (2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;

   (3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;

  1 #!/bin/bash
  2 #author:BaoZhang
  3 #
  4 command_target_dir=/mnt/sysroot/
  5 lib_target_dir=/mnt/sysroot/rootfs/
  6 function copy
  7 {
  8   if $1 &>/dev/null;then
  9     command=$(which $1 | tail -1)
 10     cp $command $command_target_dir
 11     echo "$1 copy finished"
 12     for i in { $(ldd $command | cut -d" " -f3) };do
 13       cp $i $lib_target_dir &>/dev/null
 14     done
 15     echo "$1 lib file copy finished"
 16   else
 17     echo "error input .."
 18   fi
 19 }
 20 
 21 echo "input a command or quit for quit:"
 22 read input
 23 until [ $input == "quit" ];do
 24    if [ cd $command_target_dir &>/dev/null -a cd $lib_target_dir &>/dev/null ] ;then
 25      copy $input
 26    else
 27      mkdir -p $command_target_dir &>/dev/null
 28      mkdir -p $lib_target_dir &>/dev/null
 29      copy $input
 30    fi
 31   echo "you can input a command again or quit for quit:"
 32   read input
 33 done

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

(0)
上一篇 2016-11-27 23:04
下一篇 2016-11-28 01:00

相关推荐

  • LINUX下用户管理命令简述

    LINUX下用户管理命令简述 添加用户并设置密码 useradd [用户名] 创建用户 [root@localhost ~]# useradd jack [root@localhost ~]# cat /etc/shadow | grep jack jack:!!:17257:0:99999:7::: passwd [用户名] 设置密码 [root@loca…

    Linux干货 2017-04-05
  • 8月4号作业

    正则表达式表示18位身份证号 egrep "\b[0-9]{17}(x|X|[0-9])\b" 正则表达式表示手机号 egrep "\b1[3,5,8,7][0-9]{9}\b" phone 正则表达式表示邮箱 grep -E "\b[[:alnum:]].*@[[:alnum:]]{2,3}.[[:alnu…

    Linux干货 2016-08-08
  • iptables基础详解

    一.iptables基础认知二.iptables使用格式  一.iptables简介   1.Iptabels是与Linux内核集成的包过滤防火墙系统,几乎所有的linux发行版本都会包含Iptables的功能。如果 Linux 系统连接到因特网或LAN、服务器或连接 LAN 和因特网的代理服务器, 则Iptables有利于在 …

    2017-05-03
  • 一些鲜为人知的编程事实

    我的程序员经历让我明白了一些关于软件开发的事情。下面是一些在编程中可能会让人感到诧异的事情: 一个程序员用了大约只用了10%-20%的时间来编码,而且大多数程序员,无论他的水平如何,其平均每天只有10-12行的代码最终会进入最终的软件产品中。这是因为,优秀的程序员会花费90%的时间来思考、调查、研究最佳的设计。而糟糕的程序员则会花费90%的时间来调试代码,并…

    Linux干货 2016-07-11
  • NoSQL—mongodb常见使用和入门

    NoSQL介绍: NoSQL数据管理系统是目前非常流行的一种非关系性、分布式、不支持ACID设计规范式的数据库;NoSQL简单的数据模型、元数据和数据分离、弱一致性、高吞吐量、高水平扩展能力和低端硬件集群使其流行的主要原因,而mongodb就是NoSQL数据库一种非常流行的实现方式。   常见的NoSQL数据存储模型 列式模型 文档类型 应用场景:…

    2015-09-01
  • Docker之~集群配置

    一、前言 Kubernetes 是Google开源的容器集群管理系统,基于Docker构建一个容器的调度服务,提供资源调度、均衡容灾、服务注册、动态扩缩容等功能套件,目前最新版本为0.6.2。 本文介绍如何基于Centos7.0构建Kubernetes平台,在正式介绍之前,大家有必要先理解Kubernetes几个核心概念及其承担的功能。以下为Kubernet…

    2015-03-23