linux小测试

###张小强
>**`作业答案`**

- 1.写一个脚本
```
(1) 接受一个以上文件路径作为参数,一个路径或多个路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 并且说明一共统计了有多少文件,且一共多少行;
```
```
if [ $# -lt 1 ];then
    echo " $0 PATH1 PATH2.... "
    exit 1
fi

declare -i line
declare -i lines=0
declare -i FILE_NUMS=0
PATH_NUMS=$#

# 这里的注意点在于:使用变量去参数名需要这里来取:${!i} == $1 == 参数1
for i in `seq $PATH_NUMS`; do
        if [ -e ${!i} ];then
            line=$(wc -l ${!i} | cut -d" " -f1)
            lines=$[ $lines+$line ]
            let FILE_NUMS++
            echo -e "\E[1;33m${!i}有$line行 \033[0m"

        else
                echo -e "\E[1;31m${!i}不存在 \033[0m"  
        fi

done

echo -e "\E[1;33m一共统计了$FILE_NUMS个文件,一共$lines行 \033[0m"
```
- 2.写一个脚本
```
(1) 传递两个以上的字符串当作用户名,可以是3个也可以是多个哦,不限于两个哦;
(2) 创建这些用户;且密码同用户名;
(3) 统计一共创建了几个用户;
```
```
#!/bin/bash
# (1) 传递两个以上字符串当作用户名;
# (2) 创建这些用户;且密码同用户名;
# (3) 总结说明共创建了几个用户;

if [ $# -lt 2 ];then
    echo "请输入两个以上的用户名作为参数"
    exit 1
fi

declare -i USER_NUMS=0
ARGS=$#

for i in `seq $ARGS`; do

    if id ${!i} &> /dev/null;then

        # 如果用户存在
        echo -e "\E[1;31m用户${!i}存在 \033[0m"

    else

        # 如果用户不存在
        useradd ${!i}
echo "${!i}" | passwd --stdin ${!i} &> /dev/null
        let USER_NUMS++
        echo -e "\E[1;33m创建用户${!i} \033[0m"
    fi


done
echo -e "\E[1;34m一共创建了$USER_NUMS \033[0m"
```
- 3、写一个脚本
```
(0)给脚本传递两个数值用于指定用户的范围,例如:1 20 则创建20个用户 或 101 200 则创建100个用户
(1)批量新建用户,如:student1---student20, 或 student101---student200;
(2)密码与用户名相同;
(3)如果用户已经存在就不统计其UID,并给出提示
(4)计算新建用户的UID之和;
```
```
#!/bin/bash
# (0)给脚本传递两个数值用于指定用户的范围,例如:1 20 则创建20个用户 或 101 200 则创建100个用户
# (1)批量新建用户,如:student1---student20, 或 student101---student200;
# (2)密码与用户名相同;
# (3)如果用户已经存在就不统计其UID,并给出提示
# (4)计算新建用户的UID之和;

declare -i ID
declare -i ID_SUM=0

for i in `seq $1 $2`; do

    if id student$i &> /dev/null ;then

        # 如果用户存在
        echo -e "\E[1;31m用户student$i已经存在 \033[0m"

    else
        useradd student$i
echo "student$i" | passwd --stdin student$i &> /dev/null
        ID=$(id -u student$i)
        ID_SUM=$[ ID_SUM+ID ]
        echo -e "\E[1;34m创建了用户student$i \033[0m"
    fi

done
echo -e "\E[1;33m用户的UID之和为:$ID_SUM \033[0m"
```
- 4、写一个脚本
```
(1)分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
```
```
#!/bin/bash
# 分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及空白行数之和;

declare -i Line1=0
declare -i Line2=0


for file in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab; do
    Line1=$[ $Line1+$(egrep "^#" $file | wc -l) ]
    Line2=$[ $Line2+$(egrep "^[[:space:]]*$" $file | wc -l) ]
done


echo -e "\E[1;33m#号开头的行数之和为:$Line1\033[0m"
echo -e "\E[1;31m空白行数之和:$Line2\033[0m"
```
- 5、写一个脚本
```
(1)显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
```
```
#!/bin/bash


egrep "/bin/bash" /etc/passwd | cut -d: -f1

egrep "/bin/bash" /etc/passwd | cut -d: -f3

declare -i sum=0
for i in `egrep "/bin/bash" /etc/passwd | cut -d: -f3`;do
    sum=$[ $i+$sum ]
done 
echo -e "\E[1;33mUID之和为:$sum\033[0m"
```
- 6、写一个脚本
```
(1)使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;
(2)在线的主机使用绿色显示;
(3)不在线的主使用红色显示;
(4)请使用函数完成
```
```
#!/bin/bash
# 使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;
# 在线的主机使用绿色显示;
# 不在线的主使用红色显示;


PING(){
    if ping -c1 -w1 172.16.250.$1 &> /dev/null;then

        # 可以ping通
        echo -e "\E[1;32m172.16.250.$1 在线\033[0m"



    else
        # 不能ping通
        echo -e "\E[1;31m172.16.250.$1 不在线\033[0m"
    fi
}

for i in {1..254}; do
    PING $i
done
```
- 7、写一个脚本
```
(1) 添加10用户user1-user10;密码同用户名;
(2) 用户不存在时才添加;存在时则跳过;
(3) 最后显示本次共添加了多少用户;
```
```
#/bin/bash
# (1) 添加10用户user1-user10;密码同用户名;
# (2) 用户不存在时才添加;存在时则跳过;
# (3) 最后显示本次共添加了多少用户;
#(4)用函数实现

Adduser(){
    adduser $1
    echo "$1" | passwd --stdin $1 &> /dev/null
    echo "创建用户$1"
}

USER_COUNT=0

for i in `seq 10`;do

    if id user$i &> /dev/null;then

        continue

    else
        Adduser user$i
        let USER_COUNT++

    fi

done

echo "create users $USER_COUNT"
```
- 8、打印逆序九九乘法表
```
给脚本传递一个参数,如果是9那么打印逆序九九乘法表,如果是8那么打印逆序八八乘法表
```
```


```
- 9、写一个脚本
```
(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;如果不存在,则显示设备不存在;
```
```
#/bin/bash
# (1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
# (2) 如果存在,则显示此设备上的所有分区信息;如果不存在,则显示设备不存在;


if [ $# -lt 1 ];then

    echo "please enter /dev/sd[a-z]"
    exit 1

fi

if [ -b $1 ];then

    fdisk -l
else

    echo "设备不存在"

fi
```

- 10、写一个脚本
```
传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;
(1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/tmp目录中,并命名为/tmp/etc-20170613.tar.gz;
(2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/tmp目录中,并命名为/tmp/etc-20170613.tar.bz2;
(3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/tmp目录中,并命名为/tmp/etc-20170613.tar.xz;
(4) 其它任意值,则显示错误压缩工具,并执行非正常退出;
```
```
#/bin/bash
# 传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;
# (1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/tmp目录中,并命名为/tmp/etc-20170613.tar.gz;
# (2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/tmp目录中,并命名为/tmp/etc-20170613.tar.bz2;
# (3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/tmp目录中,并命名为/tmp/etc-20170613.tar.xz;
# (4) 其它任意值,则显示错误压缩工具,并执行非正常退出;

if [ $# -lt 1 ];then
        echo "please enter an argument"
        exit 1
fi

if [ "$1" == "gzip" ];then

        tar czvf /tmp/etc-20170813.tar.gz /etc && echo "使用gzip归档"


elif [ "$1" == "bzip2" ];then

        tar cjvf /tmp/etc-20170813.tar.bz2 /etc && echo "使用bzip2归档"

elif [ "$1" == "xz" ];then

        tar Jcf /tmp/etc-20170813.tar.xz /etc && echo "使用xz归档"
else

        echo "压缩错误"
        exit 1

fi
```- 11、写一个脚本
```
接受一个路径参数:
(1) 如果为普通文件,则说明其可被正常访问;
(2) 如果是目录文件,则说明可对其使用cd命令;
(3) 如果为符号链接文件,则说明是个访问路径;
(4) 其它为无法判断;
```
```
#/bin/bash
# (1) 如果为普通文件,则说明其可被正常访问;
# (2) 如果是目录文件,则说明可对其使用cd命令;
# (3) 如果为符号链接文件,则说明是个访问路径;
# (4) 其它为无法判断;

if [ $# -lt 1 ];then

    echo "please enter an argument"
    exit 1
fi

if [ -f $1 ];then
    echo "可以被正常访问"
elif [ -d $1 ];then
        echo "可以使用cd命令"
elif [ -L $1 ];then
        echo "是个访问路径"

else
        echo "无法判断"

fi
```
- 12、写一个脚本
```
接受一个用户名为参数;
(1) 如果用户的id号为0,则显示其为管理员;
(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;
(3) 大于等于500,则显示其为普通用户;
```
```
#!/bin/bash
#(1)传递一个参数给脚本,此参数为用户名
# (2)

if [ $# -lt 1 ]; then
    echo "A argument is needed."
    exit 1
fi

# 如何输入的用户名存在
# 注意通过命令来判断成功与否不能加[]
if id -u $1 &>/dev/null;then
    userid=$(id -u $1)

    if [ $userid -eq 0 ];then
        echo "Administrator"
    elif [ $userid -gt 1 -a $userid -lt 500 ];then
        echo " System user"
    elif [ $userid -ge 500 ];then
        echo "Login user"
    fi

else
    echo "NO such user."   
fi
```

- 13、写一个脚本
```
传递一个用户名参数给脚本;
(1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;
(2) 否则,则显示无法登录系统;
```
```
#/bin/bash
# 传递一个用户名参数给脚本;
# (1) 如果用户的id号大于等于500,且其默认shell为/bin/bash,则显示“a user can log system.”类的字符串;
# (2) 否则,则显示无法登录系统;

if [ $# -lt 1 ];then

    echo "please enter an username"
    exit 1

fi
if id $1 &> /dev/null;then
    USER_ID=$(id -u $1)
    SHELL_TYPE=$(egrep "^$1" /etc/passwd | cut -d: -f7)
else

    echo "user doesn't exist!"

fi

if [ "$USER_ID" -ge 500 -a "$SHELL_TYPE" == "/bin/bash" ];then
    echo "a user can log system."

else
    echo "a user can not log system."
fi


```
- 14、写一个脚本
```
(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp目录中;
(2) 复制目录时,才使用cp -r命令;
(3) 复制文件时使用cp命令;
(4) 复制链接文件时使用cp -d命令;
(5) 余下的所有类型,使用cp -a命令;
```
```
#/bin/bash
# (1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp目录中;
# (2) 复制目录时,才使用cp -r命令;
# (3) 复制文件时使用cp命令;
# (4) 复制链接文件时使用cp -d命令;
# (5) 余下的所有类型,使用cp -a命令;


for file in /var/log/*;do

    if [ -f $file ];then

        cp $file /tmp

    elif [ -d $file ];then

        cp -r $file /tmp


    elif [ -d $file ];then

        cp -d $file /tmp

    else

        cp -a $file /tmp

    fi

done
```
- 15、写一个脚本
```
1:如果参数非此四者,则提示使用帮助后退出
2:如果传递start参数,则在/var/log目录下创建一个与脚本名相同的文件,并显示启动。
3:如果传递stop参数,就在/var/log目录下删除一个与脚本名相同的文件 ,并显示停止。
4:如果传递restart参数,就先删除再创建此文件,而后显示重启完成。
5:如果传递status参数, 如果文件存在,则显示running, 否则显示为stopped
```
```
#!/bin/bash
#
# chkconfig: - 50 50
# description: test service script

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
```
- 16、写一个脚本
```
判断给定的yhy用户是否登录了当前系统;
(1) 如果登录了,则显示用户登录,脚本终止;
(2) 每3秒钟,查看一次用户是否登录;
```
```
while true; do
    sleep 3
    if who | grep yhy &>/dev/null;then
        echo "yhy user is login"
        break
    fi
done
```
- 17、写一个脚本
```
显示用户选定要查看的信息;
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
```
```
#/bin/bash
# 显示用户选定要查看的信息;
# cpu) display cpu info
# mem) display memory info
# disk) display disk info
# quit) quit
# 非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;

EOF
cat << EOF
cpu)display cpu information
mem)display memory information
disk)display disk information
quit)quit
=============================
EOF

read -p "Enter your option:" option

# 这里的while,通过read命令循环输入option,使得循环可以继续进行
while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ]; do
    echo "please input cpu , mem , disk , quit "
    read -p "Enter your option:" option
done

if [ "$option" == "cpu" ];then
    lscpu
elif [ "$option" == "mem" ];then
        free -m
elif [ "$option" == "disk" ];then
        fdisk -l /dev/sd[a-z]
elif [ "$option" == "quit" ];then
        echo "quit"
        exit 0
fi
```

- 18、写一个脚本
```
(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
(2) 提示用户输入一个用户名或输入“quit”退出;
    当输入的是用户名,则调用函数显示用户信息;
    当用户输入quit,则退出脚本;
    进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit
    直到用户输入了quit之后退出提示
```
```
#/bin/bash
# (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
# (2) 提示用户输入一个用户名或输入“quit”退出;
#     当输入的是用户名,则调用函数显示用户信息, 如果用户不存在,提示用户
#     当用户输入quit,则退出脚本;
#     进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit
#     直到用户输入了quit之后退出提示


read -p "请输入username或quit:  " username

while [ "$username" != "quit" ]; do
    USER_ID=$(id -u $username)
    if id $username &> /dev/null ;then
        USER_SHELL=$(egrep "^$username" /etc/passwd | cut -d: -f7)
        echo "UID is $USER_ID and SHELL is $USER_SHELL"

    else
        echo "user doesn't exist"
    fi

    read -p "请输入username或quit:  " username
done

exit 0
```
- 19、写一个脚本
```
(1)清除/var/log/messages的日志文件
(2)必须是root用户才能执行脚本,否则给出友好提示且终止脚本
(3)用户必须成功切换至/var/log目录,否则给出友好提示且终止脚本
(4)清理日志,如果清理完毕给出正确的提示
(5)如果清理失败给出失败提示
```
```
#/bin/bash
# (1)清除/var/log/messages的日志文件
# (2)必须是root用户才能执行脚本,否则给出友好提示且终止脚本
# (3)用户必须成功切换至/var/log目录,否则给出友好提示且终止脚本
# (4)清理日志,如果清理完毕给出正确的提示
# (5)如果清理失败给出失败提示

LOG_DIR=/var/log
ROOT_UID=0


if [ "$UID" -ne $ROOT_UID ];then
    echo "必须是root用户才能执行脚本"
    exit 1
fi

cd $LOG_DIR || {
    echo "无法进入$LOG_DIR目录"
    exit 1
}

cat /dev/null > messages && {
    echo "日志清理完毕"
    exit 0
}

echo "日志清理失败"
exit 1
```
- 20、写一个脚本
```
整数判断可以使用: expr $nub1 + 2 &> /dev/null; echo $?
实现一个能够计算 +、 -、 *、 / 的计算器
(1)给脚本交互式的输入3个值
(2)每次都对输入的值进行类型判断
(3)最后输出计算结果
例如:
[root@6 yhy]# bash 3.sh
请输入第一个数值:  4
请输入操作符:  -
请输入第二个数值:  100
4 - 100 = -96
```
```
#/bin/bash
# 实现一个能够计算 +、 -、 *、 / 的计算器
# (1)给脚本交互式的输入3个值,例如: 3 + 3
# (2)每次都对输入的值进行类型判断,如果用户交互式输入的值的类型不是整数,提示用户且退出脚本
# (3)最后输出计算结果


read -p "请输入第一个数值:  " num1

# 判断是否是整数,如果删除所有的数值之后,引号内的字符串依然不为空,那么就提示用户输入一个整数
# -n 表示如果字符串不为空则为真
if [ -n "`echo $num1 | sed -r 's/[[:digit:]]+//g'`" ];then
    echo -e "必须输入整数\n"
    exit 1
fi

read -p "请输入操作符:  " operation

if [ "$operation" != "+" ]&&[ "$operation" != "-" ]&&[ "$operation" != "*" ]&&[ "$operation" != "/" ];then
    echo -e "只能输入{+|-|*|/}\n"
    exit 1
fi

read -p "请输入第二个数值:  " num2

# 判断是否是整数,如果删除所有的数值之后,引号内的字符串依然不为空,那么就提示用户输入一个整数
# -n 表示如果字符串不为空则为真
if [ -n "`echo $num2 | sed -r 's/[[:digit:]]+//g'`" ];then
    echo -e "必须输入整数\n"
    exit 1
fi

sum=$[ ${num1}${operation}${num2} ]
echo -e "${num1} ${operation} ${num2} = $sum\n"
```

 

posted @ 2017-08-18 19:31  Dust-disappear  阅读(202)  评论(0编辑  收藏  举报