练习题
练习题一:ifconfig 网卡可以显示此网卡的信息
显示信息包含此网卡使用的ip地址,请用命令过滤此网卡ip,且只显示ip不显示其他信息
ifconfig | cut -d: '' 10 | head -2
练习题二:找出能登录系统用户中UID最大的用户,并显示其名称
grep /bin/bash /etc/passwd | sort -t : -nrk 3 | head -1| cut -d: -f 1
练习题三:检测系统中是否存在hellow用户
方法一:
vim test.sh;
#!/bin/bash
grep hellow /etc.passwd && {
echo hellow is exits
} || {
echo hellow is not exits
}
方法二:
vim test2.sh
#!/bin/bash
read -p "please input a username:" username
if id -u $username > /dev/null 2>&1; then
echo "user exists"
else
echo "user not exists"
fi
练习题四:检测数字是否大于等于10,是输出yes,不是输出no
vim num_check.sh:
#!/bin/bash
[ "$1" -ge "0" -a "$1" -lt "10" ] && {
echo no
} || {
ech
o yes
}
练习题五:检测目标是否存在,并判断目标的类型
vim check.sh;
#!/bin/bash
[ -z "$1" ] && {
echo "Error:nothing fing, Please input something !!"
exit
}
[ -e "$1" ] && {
echo "Error: $1 is not exits !!"
exit
}
[ -L "$1" ] && {
echo "$1 is a link file"
exit
}
[ -f "$1" ] && {
echo " $1 is a common file"
exit
}
[ -d "$1" ] && {
echo "$1 is a directory"
exit
}
[ -S "$1" ] && {
echo "$1 is a socket"
exit
}
[ -b "$1" ] && {
echo "$1 is a device file"
exit
}
[ -c "$1" ] && {
echo "$1 is a char"
exit
}
练习题六:请显示系统中能被su命令切换的用户名称
vim su.sh:
#!/bin/bash
grep -E “bash$|sh$” /etc/passwd | cut -d : -f 1
练习题七:编写脚本Apache.sh,此脚本接入的什么数字,http的端口就改为这个数字。(假设selinux关闭)
#!/bin/bash
setenforce 0 &> /dev/null
[ -z "$1" ] && {
echo "Error:Please input port following script !!"
exit
}
rpm -q httpd &> /dev/null || {
echo "Error:Apache is not installed !!"
exit
}
systemctl status httpd | grep "running" &> /dev/null || {
echo "Error:Apache is not running !!"
exit
}
netstat -antlupe | grep -E ":$1\>" &> /dev/null && {
echo "Error:$1 is in used !!"
exit
}
sed "/^Listen/cListen $1" -i /etc/httpd/conf/httpd.conf
systemctl restart httpd