Shell经典面试题
1、需求:判断192.168.1.0/24网络中,当前在线的ip有哪些,并编写脚本打印出来。
编写脚本:
脚本名1.sh
#!/bin/bash
network="192.168.1."
for i in `seq 1 254`
do
ip="${network}${i}"
ping -c 1 -W 1 $ip > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$ip is online"
fi
done
执行脚本结果:
[root@localhost ~]# vim 1.sh
[root@localhost ~]# chmod +x 1.sh
[root@localhost ~]# ./1.sh
192.168.1.2 is online
192.168.1.11 is online
192.168.1.51 is online
然后被我终止了,有几个参考就行了
注意:
1. 网络与安全
防火墙:系统或网络设备防火墙可能阻止ICMP等探测包,致主机被误判离线,需调整规则。
访问控制:MAC过滤或认证限制,可能使虚拟机无权限扫描网段,影响检测。
2. 脚本执行权限:Linux脚本需执行权限,Windows批处理可能要管理员权限运行。
依赖:使用如nmap等工具,需先安装,且注意版本差异。
2、设计一个 Shell 程序,在/userdata 目录下建立50个目录,即 userl~user50,并设置每个
目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组
的权限为:读、执行。
编写脚本:
脚本名4.sh
#!/bin/bash
#########################
#File name:4.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2025-01-12 02:16:17
#Description:
#########################
target_dir="/userdata"
if [ ! -d "$target_dir" ]; then
mkdir -p "$target_dir"
fi
cd "$target_dir"
for i in {1..50}; do
mkdir "user$i"
chmod 754 "user$i"
done
echo "50个目录已创建并设置权限完成。"
执行脚本结果:
[root@localhost ~]# vim 4.sh
[root@localhost ~]# chmod +x 4.sh
[root@localhost ~]# sudo ./4.sh
mkdir: cannot create directory ‘user1’: File exists
mkdir: cannot create directory ‘user2’: File exists
mkdir: cannot create directory ‘user3’: File exists
mkdir: cannot create directory ‘user4’: File exists
mkdir: cannot create directory ‘user5’: File exists
mkdir: cannot create directory ‘user6’: File exists
mkdir: cannot create directory ‘user7’: File exists
mkdir: cannot create directory ‘user8’: File exists
mkdir: cannot create directory ‘user9’: File exists
mkdir: cannot create directory ‘user10’: File exists
mkdir: cannot create directory ‘user11’: File exists
mkdir: cannot create directory ‘user12’: File exists
mkdir: cannot create directory ‘user13’: File exists
mkdir: cannot create directory ‘user14’: File exists
mkdir: cannot create directory ‘user15’: File exists
mkdir: cannot create directory ‘user16’: File exists
mkdir: cannot create directory ‘user17’: File exists
mkdir: cannot create directory ‘user18’: File exists
mkdir: cannot create directory ‘user19’: File exists
mkdir: cannot create directory ‘user20’: File exists
mkdir: cannot create directory ‘user21’: File exists
mkdir: cannot create directory ‘user22’: File exists
mkdir: cannot create directory ‘user23’: File exists
mkdir: cannot create directory ‘user24’: File exists
mkdir: cannot create directory ‘user25’: File exists
mkdir: cannot create directory ‘user26’: File exists
mkdir: cannot create directory ‘user27’: File exists
mkdir: cannot create directory ‘user28’: File exists
mkdir: cannot create directory ‘user29’: File exists
mkdir: cannot create directory ‘user30’: File exists
mkdir: cannot create directory ‘user31’: File exists
mkdir: cannot create directory ‘user32’: File exists
mkdir: cannot create directory ‘user33’: File exists
mkdir: cannot create directory ‘user34’: File exists
mkdir: cannot create directory ‘user35’: File exists
mkdir: cannot create directory ‘user36’: File exists
mkdir: cannot create directory ‘user37’: File exists
mkdir: cannot create directory ‘user38’: File exists
mkdir: cannot create directory ‘user39’: File exists
mkdir: cannot create directory ‘user40’: File exists
mkdir: cannot create directory ‘user41’: File exists
mkdir: cannot create directory ‘user42’: File exists
mkdir: cannot create directory ‘user43’: File exists
mkdir: cannot create directory ‘user44’: File exists
mkdir: cannot create directory ‘user45’: File exists
mkdir: cannot create directory ‘user46’: File exists
mkdir: cannot create directory ‘user47’: File exists
mkdir: cannot create directory ‘user48’: File exists
mkdir: cannot create directory ‘user49’: File exists
mkdir: cannot create directory ‘user50’: File exists
50个目录已创建并设置权限完成。
为什么使用sudo ./4.sh
因为需要root用户身份运行脚本(因为需要在/userdata
目录下创建目录)
chmod +x 文件名 是给脚本文件添加执行权限
注意:
确保以root用户身份运行脚本,因为普通用户可能没有权限在/userdata
目录下创建目录。
如果/userdata
目录已经存在并且已经有内容,脚本会继续在该目录下创建新的目录,不会覆盖已有的内容。