Linux basic test questions (2) shell basic command test questions

  1. Only display the IP used in the network card information

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

Insert picture description here


  1. Find the user with the largest UID among the users who can log in to the system and display its name

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

Insert picture description here

  1. The current host is a web server, and the top 5 IP addresses that have accessed the web server are captured

#方法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. Execute the script user_check.sh to determine the user type

The user types are:

  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 !"

Insert picture description here

  1. Write the script file_check.sh to complete the following tasks
  1. If the test file is not specified after the script, an error "No test file specified, please specify" is reported
  2. If the specified file does not exist after the script, an error "This file does not exist" is reported
  3. When the file exists, please check the file type and display it in the output

#!/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
}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46069582/article/details/111186752