1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
grep实现:
打印用户个数:
~]# grep -v '/sbin/nologin$' /etc/passwd | wc -l
显示每个用户名:
~]# grep -v '/sbin/nologin$' /etc/passwd | cut -d':' -f 1
awk实现:
打印用户个数:
~]# awk -F':' '{if ($NF != "'"/sbin/nologin"'") print $1}' /etc/passwd | wc -l
显示每个用户名:
~]# awk -F':' '{if ($NF != "'"/sbin/nologin"'") print $1}' /etc/passwd
注意:
1、使用awk的条件语句时如果操作符右侧为字符串务必使用单引号引起来,否则条件不生效,同时需要用双引号转义单引号。
2、写代码行的时候使用反引号包围起来,避免发布之后博客中的英文引号变中文。
2、查出用户UID最大值的用户名、UID及shell类型
~]# cut -d':' -f 1,3,7 /etc/passwd | sed 's/:/ /g' | sort -k2 -nr | head -1
注意:sort的-k参数指定排序列默认以空格分隔,所以这里使用sed把分隔符替换了。
~]# awk -F':' '{print $1,$3,$NF}' /etc/passwd | sort -k2 -nr | head -1
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
~]# netstat -an | grep -w ESTABLISHED | awk -F'[ :]+' '{ip[$6]++} END {for (i in ip) print ip[i]}' | sort -nr
4、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等 信息
#!/bin/bash
#
#Author: Fluence
#Version:1.0
#Date:2019-11-24
#Function:接受用户输入一个用户名参数,检查指定用户是否存在;若不存在,则添加;显示添加的用户的ID号等信息。
#Email:Fluence@163.com
function check_or_create_user()
{
local user=$1
if id ${user} &>/dev/null
then
echo "There is a user <${user}> exist."
return 0
else
echo "There is not a user <${user}> exist, and add it..."
if useradd ${user} &>/dev/null
then
local user_id=$(id ${user} | awk -F'[ =()]+' '{print $2}')
echo "Added the user <${user}> success, and the user ID is <${user_id}>."
else
echo "Added the user <${user} fail, and please check...>"
return 1
fi
fi
return 0
}
function main()
{
local input_user
read -p "Please input the user name:>>>" input_user
if [ -n "${input_user}" ] && echo "${input_user}" | grep -E '^[[:alpha:]]+$' &>/dev/null
then
check_or_create_user "${input_user}" || return 1
else
echo "Invalid input and please input the alpha..."
return 1
fi
return 0
}
main
MAIN_RET=$?
exit ${MAIN_RET}
5、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
#!/bin/bash
#
#Author: fluence
#Version:0.1
#Date:2019-11-24
#Email:fluence@163.com
#Function:生成脚本的头信息,包括:解释的bash类型,作者,版本,时间,邮箱地址,脚本功能说明;执行时要生成的脚本文件需要输入,其他信息不输入将使用当前默认,时间会使用执行脚本时系统日期。
function make_head_shell()
{
local shell_file=$1
shift
local shell="${1:-/bin/bash}"
local author="${2:-fluence}"
local version="${3:-0.1}"
local Date="${4:-$(date +%F)}"
local email="${5:-fluence@163.com}"
local function="${6:-生成脚本头信息,包括:解释的bash类型,作者,版本,时间,邮箱地址,脚本功能说明}"
cat > ${shell_file} << EOF
#!${shell}
#
#Author:${author}
#Version:${version}
#Date:${Date}
#Email:${eamil}
#Function:${function}
EOF
return 0
}
function main()
{
read -p "Please input the file path:" file
read -p "Please input the shell type:" shell
read -p "Please input the author name:" author
read -p "Please input the version:" version
read -p "Please input the date:" Date
read -p "Please input the email address:" email
read -p "Please input the script function:" script_function
make_head_shell "${file}" "${shell}" "${author}" "${version}" "${Date}" "${email}" "${script_function}" || return 1
return 0
}
main
MAIN_RET=$?
exit ${MAIN_RET}
注意:使用EOF时最后一个需要占单独一行,且前后不能有空格,否则会报语法错误。