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)
zhangbaozhangbao
上一篇 2016-11-27
下一篇 2016-11-28

相关推荐

  • CentOS 更改改网卡名称

    1 查看你的CentOS7网卡名字叫什么(通常第一个网卡叫做eno16777736) ip addr 2 编辑配置文件 vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 把配置文件里面所有eno16777736改为eth0 3 把网卡配置文件名字也修改一下 cd /etc/sysconfig/networ…

    2018-01-18
  • 用户和组相关的配置文件

    目录 l  用户 l  组账号 l  UID和GID号 l  /etc/passwd分析 l  /etc/shadow分析 l  /etc/group分析 l  /etc/gshadow分析 l  通过用户和用户组配置文件来查询或管理用户 l  总结 1、用户(User…

    Linux干货 2016-10-23
  • 一起学DHCP系列(四)安装、配置

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeffyyko.blog.51cto.com/28563/162580       本节将主要讲述DHCP的安装和配置。     …

    Linux干货 2015-03-25
  • 编程能力与编程年龄

    程序员这个职业究竟可以干多少年,在中国这片神奇的土地上,很多人都说只能干到30岁,然后就需要转型,就像《程序员技术练级攻略》这篇文章很多人回复到这种玩法会玩死人的一样。我在很多面试中,问到应聘者未来的规划都能听到好些应聘都说程序员是个青春饭。因为,大多数程序员都认为,编程这个事只能干到30岁,最多35岁吧。每每我听到这样的言论,都让我感到相当的无语,大家都希…

    Linux干货 2016-08-15
  • 正则表达式及其用法

    正则表达式及其用法 1.   什么是正则表达式 正则表达式是Global search REgular expression and Print out the line的缩写。是一类用字符所书写的模式,其中许多字符并不表示其字面意义,而是表达控制或通配等功能。 在Linux中,用正则表达式搜索文本的常用命令: grep: 使用基本正则表…

    Linux干货 2015-09-14
  • 关于生命中出现的那个男人

    绝对真男人     一个人的一生,会遇到很多很多的男人,哦,也会遇到很多很多的女人,但是我们今天就谈谈男人吧.在这芸芸众生中对于我们这些志向于在linux求发展谋生存的人来说,有个男人会是所有linux爱好者的良师益友.这个男人就是man,这个男人是绝对的男人,因为他是在linux这个系统中无所不知的那个男人. &nb…

    Linux干货 2016-10-27