第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

相关推荐

  • liunx文本处理三剑客及文本处理工具的使用与练习。

    linux文本处理三剑客: grep ==擅长过滤,把想要的或者不想要的分离开。linux三剑客 老三  -A: 除了显示匹配的一行之外,并显示该行之前的num行;-B:除了显示匹配的一行之外,并显示该行之后的num行;-C:除了显示匹配的一行之外,并显示该行之前后各num行     sed==擅长取行、替换。三剑客老…

    2017-07-28
  • 对ssh的简单理解

    ssh:secure shell,protocol,22、tcp安全的远程登录 具体的软件实现:openssh:ssh协议的开源实现,centos默认安装dropbear:另一个开源实现ssh协议版本v1:基于crc-32做MAC,不安全;man-in-middle 已淘汰v2:双方主机协议选择安全的MAC方式基于DH算法做密钥交换,基于RSA或DSA实现身…

    2017-09-29
  • DNS 主从协作及配置父子域实验

    实验:DNS主从协作及配置父子域实验 实验拓扑图 实验准备     1、所有主机关闭防火墙和selinux         service iptables stop       …

    Linux干货 2016-08-15
  • wk_02 作业

    Linux 文件管理命令 cp命令 功能 Linux 系统中cp命令是用来复制目录/文件的。 概要 单源复制 cp [OPTION]… [-T] SOURCE DEST DEST不存在则事先创建此文件,并复制源文件的数据流至DEST中; DEST存在 DEST是非目录文件:则覆盖目标文件; DEST是目录文件:则先…

    Linux干货 2016-12-11
  • 系统用户基本信息

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 who | cut -d" " -f1 | sort -u who | cut -d" " -f1 | uniq 2、取出最后登录到当前系统的用户的相关信息。 who | tail -1 3、取出当前系统上被用户当作其默认she…

    Linux干货 2016-11-20
  • grep命令与正则表达式的应用示例、find命令应用示例

    grep命令与正则表达式的应用示例、find命令应用示例 显示当前系统上root、fedora或user1用户的默认shell。 使用扩展正则表达式,它支持使用A|B表示或者之意。查看用户的默认shell,cat /etc/passwd文件,根据用户名所处位置进行锚定,其最后一个字段即表示该用户的默认shell。实现方式及结果如下:   找出/et…

    Linux干货 2016-11-28

评论列表(1条)

  • luoweiro
    luoweiro 2017-02-23 07:54

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