第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

相关推荐

  • 分析命令中含有e2fs

    分析命令中含有e2fs 目  录 1、mke2fs  2、tune2fs 3、dump2fs 4、e2fsck 这周学到了几条命令,超级纳闷为什么这些命令都带有e2fs ,看不懂。毕竟在我的认知中linux的命名大部分都是见名知义,例如history(查看历史命令),fdisk (分区),反观这些命令很长而且还是奇怪的…

    Linux干货 2017-08-20
  • 从Linux小白到大牛——与狼共舞的日子12(下)

    马哥教育网络班21期+第12周课程练习 5、为第4题中的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点; (1)要求使用证书认证,证书中要求使用的国家(CN)、州(HA)、城市(ZZ)和组织(MageEdu); (2)设置部门为Ops,主机名为www2.stuX.com,邮件为[email protected]; 1.CA生成私…

    Linux干货 2016-12-26
  • M20 – 1- 第三周博客(2):Linux用户、组

    一、Linux用户组详解 Linux系统中的每个用户都有一个用户组,系统能对一个用户组中的所有用户进行集中管理。不同Linux系统对用户组的规定有所不同,如Linux下的用户属于和他同名的用户组,这个用户组在创建用户时同时创建。用户组的管理涉及用户组的添加、删除和修改。组的增加、删除和修改实际上就对/etc/group文件的更新。 用户组(group)就是具…

    Linux干货 2016-08-05
  • Linux下vim编辑器的使用

    vim编辑器 概括 vim,是vi的升级版本,它不仅兼容vi的所有指令,还有一些新的特性在里面。 使用vi和vim的三种主要模式 vim在工作过程当中有三种模式,编辑模式(命令模式)、输入模式、末行模式     1、编辑模式:也叫命令模式,键盘操作常被理解为编辑命令;     2…

    Linux干货 2016-08-10
  • bash脚本编程

    bash脚本编程之用户交互:     脚本参数     用户交互:通过键盘输入数据     read [option]…[name]…     -p: "PROMPT"      -t: TIMEOUT &…

    Linux干货 2017-01-01
  • centos启动流程

    1、加电自检(power on system test POST) 加电,就是按下电源键使电脑主机通电,在诸多硬件设备中有一个被称为CMOS设备在计算机启动的过程中起到了至关重要的作用,CMOS在加电之后会启动保存在它上面的BIOS程序,即基本输入输出系统(Basic Input Output System)。自检,主要是检测各…

    Linux干货 2016-09-11

评论列表(1条)

  • luoweiro
    luoweiro 2017-02-23 07:54

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