Shell脚本测试(练习笔记)-【未完】

1、接受一个路径参数:

(1) 如果为普通文件,则说明其可被正常访问;

(2) 如果是目录文件,则说明可对其使用cd命令;

(3) 如果为符号链接文件,则说明是个访问路径;

(4) 其它为无法判断;

#!/bin/bash
if [ $# -lt 1 ];
    echo "please input a file."
    exit 1
fi
if [ -f $1 ];then
    echo "该文件为普通文件,可以被正常访问"
    elif [ -d $1 ];then
    echo "该文件为目录文件,可以使用cd命令"
    elif [ -l $1 ];then
    echo "该文件是链接文件,是访问路径"
else
    echo "无法判断"
fi

2、接受一个用户名为参数;

(1) 如果用户的id号为0,则显示其为管理员;

(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;

(3) 大于等于500,则显示其为普通用户;

#!/bin/bash
if [ $# -lt 1 ];
    echo "please input a username."
    exit 1
fi

if id -u $1 &> /dev/null ;then
    idnum=$(id -u $1)
    if [ $idnum -eq 0 ];then
        echo "$1 is an administrator."
        elif [ $idnum -gt 0 -a $idnum -lt 500 ]
        echo "$1 is a system user."
        elif [ $idnum -ge 500 ];then
        echo "$1 is the average user."
else
    echo "$1 is not exit."
    fi
fi

3、传递一个用户名参数给脚本;

(1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;

(2) 否则,则显示无法登录系统;

#!/bin/bash
if [ $# -lt 1 ];then
    echo "please input a username."
    exit 1
fi

shell=$(grep "^$1.*sh$" /etc/passwd | cut -d: -f3)

for i in $shell;do
    if [ $i -gt 500 ];then
        echo "a user can log systerm "
    else
        echo "can not login"
    fi
done

4、满足以下要求:

(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp目录中;

(2) 复制目录时,才使用cp -r命令;

(3) 复制文件时使用cp命令;

(4) 复制链接文件时使用cp -d命令;

(5) 余下的所有类型,使用cp -a命令;

#!/bin/bash
file=/var/log/*

for i in $file ;do
    if [ -d $i ];then
        cp -r $i /tmp
        elif [ -f $i ];then
        cp  $i /tmp
        elif [ -l $i ];then
        cp -d $i /tmp
else
    cp -a $i /tmp
    fi
done

5、满足以下要求:

(1)如果参数非此四者,则提示使用帮助后退出

(2)如果传递start参数,则在/var/log目录下创建一个与脚本名相同的文件,并显示启动。

(3)如果传递stop参数,就在/var/log目录下删除一个与脚本名相同的文件 ,并显示停止。

(4)如果传递restart参数,就先删除再创建此文件,而后显示重启完成。

(5)如果传递status参数, 如果文件存在,则显示running, 否则显示为stopped

#!/bin/bash
prog=$(basename $0)
FILE=/var/log/$prog

case $1  in
start)
    if [ -f $FILE ]; then
        echo "$prog is running yet."
    else
        touch $FILE
        [ $? -eq 0 ] && echo "start $prog ok."
    fi
    ;;
stop)
    if [ -f $FILE ]; then
        rm -f $FILE
        [ $? -eq 0 ] && echo "stop $prog finished."
    else
        echo "$prog is not running."
    fi
    ;;
restart)
    if [ -f $FILE ]; then
        rm -f $FILE
        touch $FILE
        echo "restart $prog ok."
    else
        touch -f $FILE
        echo "start $prog ok."
    fi
    ;;
status)
    if [ -f $FILE ]; then
        echo "$prog is running"
    else
        echo "$prog is stopped."
    fi
    ;;
*)
    echo "Usage: $prog {start|stop|restart|status}"
    exit 1
esac

6、判断给定的yhy用户是否登录了当前系统;

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

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

#!/bin/bash

if [ $# -lt 1 ];then
    echo "need a username."
    exit 1
fi
if id $1 &> /dev/null;then
    yhy=$(grep "$1" | cut -d" " -f1)
    if [ $yhy -ep $1 ];then
        echo "$1 上线"
        break
    else
        sleep 3
    fi
    echo "$1 不再上线"
    exit 2
fi

7、显示用户选定要查看的信息;

cpu) display cpu info

mem) display memory info

disk) display disk info

quit) quit

非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止; 

#!/bin/bash
cat << EOF
disk) show disks info
mem)show memory info
cpu)show cpu info
*)quit
EOF

read -p "Please input your option:" option

option=$(echo $option | tr [A-Z] [a-z])

if [ $option == "disk" ]; then
    fdisk -l
elif [ $option == "mem" ]; then
    free -m
elif [ $option == "cpu" ]; then
    lscpu
elif [ $option == "*" ]; then
    exit 1
else
    echo "Wrong option,please chose again."
fi

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值