第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
下一篇 2017-01-04

相关推荐

  • 命令行历史和调用命令参数

    history显示当前终端的历史记录 !! 重复执行上一条命令 !-n 重复执行倒数第n条命令 !n 重复执行第n条命令 ctrl+p 等于向上方向键,翻出上一条命令。 ctrl+n 等于向下方向键,翻出下一条命令。 ctrl+j 等于enter 执行当前命令 !:0 执行前一条命令(去除参数) !string 重复前一个以“string”开头的命令(只限于…

    2017-07-14
  • MAN手册各章节功能

         1.普通命令     2.系统调用 (方便查到调用的函数,需加的头文件)     3.库函数     4.特殊文件 例:/dev下的各种设备文件     5.文件的格式    &n…

    Linux干货 2016-10-18
  • RAID与逻辑卷

        RAID与逻辑卷作为磁盘管理的方式,各有各的优势。RAID是多个磁盘合成一个阵列,以便提供更好的性能;逻辑卷相对于分区来说,可以在线扩展空间,也可以缩减空间。可以利用软件的方式来实现RAID与逻辑卷。 一、利用软件方式实现RAID     利用sdb sdc sdd sd…

    2017-08-12
  • Linux的哲学思想

    Linux的哲学思想 linux有个哲学的思想是一切皆文件 其中linux硬件设备也是通过文件来表示的 物理终端 物理终端指的是显示器等硬件终端设备,文件存在于 /dev/console 这个路径下 虚拟终端 虚拟终端指的是在linux命令行连接的终端,文件存在于 /dev/tty# [1,6] 这个路径下 串行终端 指的是使用计算机串行端口连接的终端设备,…

    Linux干货 2018-02-23
  • 第四周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@centos7 ~]# cp -r /etc/skel/ /home/tuser1 [root@centos7 ~]# chmod …

    Linux干货 2017-01-16
  • 文件处理工具系列(二):行编辑器sed

    1、sed介绍     sed(Stream EDitor),是一种行编辑工具,它一次处理一行内容。处理时,把当前匹配到的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令对这些数据做出相应的增删改查操作,处理完成后,默认输出至屏幕,而未被匹配到的行也将默认输出至屏幕。而原来的文件内容并没有发生改变,除…

    Linux干货 2016-08-11

评论列表(1条)

  • luoweiro
    luoweiro 2017-02-23 07:54

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