shell脚本练习集合1

练习题一: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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值