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

相关推荐

  • 磁盘管理

    1、创建一个10G分区,并格式为ext4文件系统。 (1)虚拟机添加21G硬盘 Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1&nbsp…

    Linux干货 2016-12-18
  • raid,lvm,及sed命令的相关使用总结

    raid0 条带化 chunk最小写入单位 至少2块盘 空间大小 单块磁盘大小*nraid1 镜像  1/n   读性能提升,写入比单块盘慢  空间始终是一块盘的大小   容错率高  可以坏  n-1块盘raid10 容错高于raid01 但空间损失更多,读写性能 提升 造价高。raid4&nbsp…

    2017-08-26
  • bash脚本编程之数组、高级字符串处理、高级变量操作、用户环境配置

    概述:     承接之前脚本编程部分,本篇将介绍一下脚本编程基础语法的最后一部分内容,具体分为:         1、数组简介         2、高级字符串处…

    Linux干货 2016-08-24
  • 网络23期心得

    1、描述计算机的组成及其功能 计算机系统是由五大部分组成:运算器、控制器、存储器、输入设备、输出设备   运算器(Datapath):运算器的功能是对数据进行各种算术去处和逻辑运算,即对数据进行加工处理。   控制器(Control):控制器是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,根据其要求进行控制,调度程序、数据、…

    Linux干货 2016-09-19
  • 磁盘管理之MBR,GPT的概念及MBR和GPT分区工具的使用

    一,概述 设备文件 I/O ports: I/O设备地址 一切皆文件 open() read() write()close() 设备类型: 块设备:block,存取单位“块”,磁盘 字符设备:char,存取单位”字符“,键盘 块设备和字符设备的区别 块设备可以随机访问,字符设备必须连续访问,如,/dev/null 块设备有缓存,字符设备没有缓存,因为字符设备…

    Linux干货 2016-09-06
  • 马哥网络教育班第21期+第六周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/ [root@localho…

    Linux干货 2016-08-02