第十周练习-脚本部分

1、写一个脚本

(1) 能接受四个参数:start, stop, restart, status

start: 输出“starting 脚本名 finished.”

(2) 其它任意参数,均报错退出;

#!/bin/bash
#
case $1 in
start)
    echo "starting $0 finished"
    ;;
stop)
    echo "stopping $0 finished"
    ;;
restart)
    echo "restarting $0 finished"
    ;;
status)
    echo "status $0 finished"
    ;;
*)
    echo "Usage:$(basename $0){start|stop|restart|status}"
    exit 1
    ;;
esac

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

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

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

#!/bin/bash
#
method() {
    status=$(who | grep $1)
    if [[ -n "$status" ]];then
        touch /tmp/online
    fi
}
while true;do
    method $1 && sleep 3
    if [ -f /tmp/online ];then
        echo "$1 is online"
    else
        echo "$1 isn't online"
    fi
    rm -f /tmp/online
done

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

#!/bin/bash
#
cat <<EOF
    输入mem>display memory info
    输入disk>display disk info
    按Q>退出
EOF
read -p ‘请按对应的字母键,否则五秒后自动退出’ -t 5 name
if [[ "$name" == "Q" ]];then
    echo "您选择退出";
    exit 1
elif [[ "$name" == "cpu" ]];then
    cat /proc/cpuinfo
elif [[ "$name" == "mem" ]];then
    df -lh
elif [[ "$name" == "disk" ]];then
    fdisk -l
else
    echo "您输入有误,请重新选择"
    bash $0
fi

4、写一个脚本

(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;

(2) 提示用户输入一个用户名或输入“quit”退出;

当输入的是用户名,则调用函数显示用户信息;

当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:

#!/bin/bash
#
doing() {
    read -p "请输入用户名或quit退出" name
    if [[ "$name" == "quit" ]];then
        exit 0
    else
        userinfo $name
    fi
}
userinfo() {
    id $1 &> /dev/null
    if [ $? -eq 0 ];then
        grep -E ^$1 /etc/passwd | awk -F: '{print $3,$NF}'
        doing
    else
        echo "user not exist"
        exit
    fi
}
doing

5、写一个脚本,完成如下功能(使用函数)

(1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;

(2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;

(3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;

#!/bin/bash
#
doing() {
    mkdir -p /mnt/sysroot
    read -p "请输入一个可执行命令" name
    which $name &> /dev/null
    if [ $? -eq 0 ];then
        path=$(which $name)
        ldd $path
        echo "==========开始复制命令文件=========="
        new_command_path=$(echo ${path%/*})/
        cp -r $path /mnt/sysroot/$new_command_path &> /dev/null
        echo "==========复制命令文件完毕=========="
        echo "==========开始复制命令依赖的库文件=========="
        mkdir -p /mnt/sysroot/$new_command_path/$name-lib
    for i in $(ldd $path);do
        cp $i /mnt/sysroot/$new_command_path/$name-lib &> /dev/null
    done
        echo "==========复制命令依赖的库文件完毕=========="
    else
        echo "the command not found"
        exit 1
    fi
}
doing

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

(0)
N24_涩味N24_涩味
上一篇 2016-12-31 21:03
下一篇 2016-12-31 21:25

相关推荐

  • awk详解

    —————————— 课外练习 只处理用户ID为奇数的行,并打印用户名和ID号 [root@localhost ~]# awk -F: '{if($3%2!=0) {print&n…

    Linux干货 2016-09-25
  • linux基础之lvm操作流程

    linux基础之lvm基本操作流程    LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,目前最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。Linux用户安装L…

    2017-03-19
  • fdisk命令

    fdisk命令用于观察硬盘实体使用情况,也可对硬盘分区。

    2017-12-05
  • 网络N22期-第五周作业

    1、显示当前系统上root、fedora或user1用户的默认shell; [root@localhost ~]#egrep '^(root|fedora|user1)\>' /etc/passwd | cut -d: -f7 /bin/bash 2、找出/etc…

    Linux干货 2016-09-15
  • grep的用法

    grep的用法 1、复制/etc/skel目录为/home/tuser1及其内容文件的属组和其它用户均没有任何访问权限。 cp -R /etc/skel /home/thuser1 #复制文件/etc/skel 到/home/目录下并改名为thuser1 chmod -R 700 /home/thuser1 #更改/home/thuser1目录权限为属主全部…

    Linux干货 2017-07-23
  • N21第五周

    1.显示/boot/grub2/grub.cfg中以至少一个空白字符开头的行; ]# grep '^[[:space:]]\+' /boot/grub2/grub.cfg 2.显示/etc/rc.d/init.d/functions文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行; ]#…

    Linux干货 2016-08-15

评论列表(1条)

  • 马哥教育
    马哥教育 2017-01-04 16:20

    写的很好,下次可以把一次作业写在一个里面