Linux基础测试题(2)shell基础命令测试题

  1. 只显示网卡信息中使用的IP

ifconfig 网卡名 | head -n 2 | tail -n 1 | cut -d " " -f 10

在这里插入图片描述


  1. 找出能登陆系统用户中UID最大的用户,并显示其名称

grep bash /etc/passwd | cut -d : -f 1,3 | sort -nr -t : -k 2 | head -n 1 | cut -d : -f 1

在这里插入图片描述

  1. 当前主机为web服务器,抓取访问web服务器次数排在前5的IP地址

#方法1
cut -d " " -f 1 /etc/httpd/logs/access_log | uniq -c | sort -k 1 -nr | head -n 5 | awk '{print $2}'
#方法2
cut -d " " -f 1 /etc/httpd/logs/access_log | uniq -c | sort -k 1 -nr | head -n 5 | sed 's/^ *//g' | cut -d " " -f 2

  1. 执行脚本user_check.sh判断用户类型

用户类型为:

  1. super user
  2. system user
  3. common user

#!/bin/bash
username=$1
#判断有无输入用户名
[ -z $username ] && {
    
    
	echo "ERROR: Please input username following script !"
	exit
}	
#判断用户是否存在
id $username &> /dev/null || {
    
    
	echo "ERROR: user "$username" is not exist !"
	exit
}

userid=$(id -u $username)
user_shell=$( grep $username /etc/passwd | cut -d : -f 7 )
#用户id等于0为超级用户
[ "$userid" -eq "0" ] && {
    
    
	echo $username is super user !
	exit
}
#用户id小于1000,且可登陆就为系统用户
[ "$userid" -lt "1000" ] && [ ! "$user_shell" = "/bin/bash" ] && {
    
    
	echo $username is system user !
	exit
}
#用户id大于等于1000,且可登陆就为普通用户
[ "$userid" -ge "1000" ] && [ "$user_shell" = "/bin/bash" ] && {
    
    
	echo $username is common user !
	exit
}
#其他情况
echo "unknow user type !"

在这里插入图片描述

  1. 编写脚本file_check.sh完成以下任务
  1. 如果脚本后未指定检测文件,报错“未指定检测文件,请指定”
  2. 如果脚本后指定文件不存在,报错“此文件不存在”
  3. 当文件存在时,请检测文件类型并显示到输出中

#!/bin/bash
filename=$1
#判断有误输入文件名
[ -z $filename ] && {
    
    
	echo Error: please input file folloeing script !
	exit
	}
#判断此文件是否存在
[ -e $filename ] || {
    
    
	echo file $filename is not exist !
	exit
}
#判断此文件的文件类型是否为目录
[ -d $filename ] && {
    
    
	echo file $filename is 目录 !
	exit
}
#判断此文件的文件类型是否为套接字
[ -S $filename ] && {
    
    
    echo file $filename is 套接字 !
    exit
}
#判断此文件的文件类型是否为软链接
[ -L $filename ] && {
    
    
    echo file $filename is 软链接 !
    exit
}
#判断此文件的文件类型是否为普通文件
[ -f $filename ] && {
    
    
    echo file $filename is 普通文件 !
    exit
}
#判断此文件的文件类型是否为块设备
[ -b $filename ] && {
    
    
    echo file $filename is 块设备 !
    exit
}
#判断此文件的文件类型是否为字符设备
[ -c $filename ] && {
    
    
    echo file $filename is 字符设备 !
    exit
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46069582/article/details/111186752
今日推荐