第10周作业(下)

4、写一个脚本
(1) 能接受四个参数:start, stop, restart, status
start: 输出“starting 脚本名 finished.”
(2) 其它任意参数,均报错退出。

#!/bin/bash
#
[ $# -ne 1 ] && echo "You should give one parameter." && exit 1

case  $1 in
start)
    echo "Starting $0 finished."
    ;;
stop)
    echo "Stopping $0 finished."
    ;;
restart)
    echo "Restarting $0 finished."
    ;;
status)
    echo "Status of $0 is normal."
    ;;
*)
    echo "You gave wrong parameter.Now,exiting..."
    exit 2
    ;;
esac

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

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

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

#!/bin/bash
#
[ $# -ne 1 ] && echo "You should give one parameter." && exit 1

while true;do
    if who | grep -o "^\($1\)";then
        echo "$1 is already logged on."
        exit 0
    fi    
    sleep 3
done

6、写一个脚本,显示用户选定要查看的信息。
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止。

#!/bin/bash
#
cat << EOF
cpu) Display CPU information.
mem) Display memory information.
disk) Display disk information.
quit) Quit.
=======================================
EOF

read -p "Please enter your choice:" option

while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ];do
    echo "You should enter cpu,mem,disk,or quit."
    read -p "Enter your choice again:" option
done

case $option in
cpu)
    lscpu
    ;;
mem)
    free -m
    ;;
disk)
    fdisk -l /dev/sd[a-z]
    df -h
    ;;
quit)
    echo "Exiting..."
    exit 0
    ;;
esac

7、写一个脚本
(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来。
(2) 提示用户输入一个用户名或输入“quit”退出。
当输入的是用户名,则调用函数显示用户信息。
当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit。

#!/bin/bash
#
cat <<EOF
You  give one username,then you'll get some information,e.g. user id and user shell.
=============================================================================
EOF

user_info() {
    while true;do
	    read -p "Please enter a username:" username   
	    if id -u $username &>/dev/null;then
	        echo "$username's id is $(id -u $username)"
	        echo "$username's shell is `grep "^\($username\)\>" /etc/passwd | cut -d: -f7`."
	    else
	        if [ $username == "quit" ];then
		        echo "Existing..."
		        exit 0
	        else
		        echo "Please enter a correct username."
		        continue
	        fi
	    fi
    done
}

user_info

8、写一个脚本,完成如下功能(使用函数)
(1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件。
(2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中。
(3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求。

#!/bin/bash
#
cat <<EOF
Enter your command ,then the program will copy the command file and lib files to /mnt/sysroot/lib64/ .
==================================================================================================
EOF

copycmd() {
    while true;do
        read -p "Enter yours command:" command
	    if which $command &>/dev/null;then
            sour=$(whereis -b $command | grep -o "/.*")
		    dest="/mnt/sysroot"$sour
		    cp $sour $dest
		        if [ $? -eq 0 ];then
			        echo "A copy of $command is finished."
		        else
			        echo "A copy of $command is failed."
			        continue
                fi
            sour1=$(ldd $sour | grep -o "/lib64/.*[[:space:]]")
	        dest2="/mnt/sysroot/lib64"
		    cp $sour1 $dest2
		        if [ $? -eq 0 ];then
		            echo "A copy of $command's lib files is finished." 
    		    else
			        echo "A copy of $command's lib files is failed."
			        continue
		        fi
	    else
		    if [ $command == "quit" ];then
		        echo "Existing..."
		        exit 0
		    else 
		        echo "Please enter a correct command."
		        continue
		    fi
	    fi
done
}
		    
copycmd

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

(0)
N24_lantianN24_lantian
上一篇 2017-01-03 22:19
下一篇 2017-01-04 00:05

相关推荐

  • ​Linux 硬连接与软连接的区别

        Linux 硬连接与软连接的区别                        &…

    Linux干货 2016-10-25
  • GlusterFS测试报告-01

    一、GlusterFS基础环境的介绍 1、关于GlusterFS文件系统和架构的介绍 http://jingyan.baidu.com/article/046a7b3ef65250f9c27fa9d9.html 2、实验的目的 a. 利用多台性能较低并且老旧的服务器,实现企业的云盘功能 b. GlusterFS服务端和客户端的部署和配置 c. 实现Glust…

    Linux干货 2016-07-22
  • 如何安装CentOS 6.8 ?

    一 ,首先我要先在虚拟机的平台上搭建一下我们需要的环境。 1,选择创建一个新的虚拟机。之后选择自定义安装。点击下一步 2,选择硬件兼容性workstation12.0,(根据情况你也可以选择下面的几个版本,不过要记得选择版本之后,不能再这个版本以下打开虚拟机) 3,点击下一步选择安装程序光盘映像文件项。点击下一步。 4,之后创建一下用户信息,点击下一步。 5…

    Linux干货 2017-02-14
  • Linux软件包安装

    Linux系统和Windows系统在软件包安装上区别很大:     软件运行环境:         API: Application Programming Interface    POSIX: P…

    Linux干货 2016-08-21
  • CentOS环境下,ab性能测试功具介绍及使用

    网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环。只有让服务器处在高压情况下,才能真正体现出软件、硬件等各种设置不当所暴露出的问题。 性能测试工具目前最常见的有以下几种:ab、http_load、webbench、siege。 ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型…

    Linux干货 2017-07-22
  • LVS

        Linux虚拟服务器(LVS)是基于Linux内核的操作系统的负载平衡软件。     LVS提供了良好的可扩展性,可靠性和可服务性。 LVS项目的主要工作是开发先进的IP 负载平衡软件(IPVS),应用级负载平衡软件(KTCPVS)和集群管理组件。 IPVS:在…

    Linux干货 2017-08-21

评论列表(1条)

  • luoweiro
    luoweiro 2017-02-23 07:54

    脚本写的很不错,对于脚本传参有了深刻的总结。