#!/bin/bash read -t 30 -p "请输入创建文件的数目:" n test=$(echo $n | sed 's/[0-9]//g') #检测非数字输入 if [ -n "$n" -a -z "$test" ] #检测空输入 then for ((i=0;i<$n;i=i+1 )) do name=$(date +%N) [ ! -d ./temp ] && mkdir -p ./temp touch "./temp/$name" echo "创建 $name 成功!" done else echo "创建失败" exit 1 fi
7.批量改名
将/root/temp目录下所有文件名重命名为”旧文件名-递增数字” 重命名命令
1
rename 旧文件名 新文件名 旧文件所在位置
脚本代码file5.sh
1 2 3 4 5 6 7 8 9 10 11
#!/bin/bash filenames=$(ls /root/temp) number=1 for name in $filenames do printf "命令前:%s" ${name} newname=${name}"-"${number} rename $name ${newname} /root/temp/* let number++ #每个改名后的文件名后缀数字加1 printf "重命名后:%s \n" ${newname} done
#!/bin/bash ULIST=$(cat /root/users.txt) ##/root/users.txt 里面存放的是用户名,一个名一行 for UNAME in $ULIST do useradd $UNAME echo "123456" | passwd --stdin $UNAME &>/dev/null [ $? -eq 0 ] && echo "$UNAME用户名与密码添加初始化成功!" done
9.筛选单词
问题: 根据给出的数据输出里面单词长度大于3的单词 数据准备
1
I may not be able to change the past, but I can learn from it.
shell脚本file7.sh
1
echo "I may not be able to change the past, but I can learn from it." | awk -F "[ ,.]" '{for(i=1;i<NF;i++){ if(length($i)>3){print $i}}}'
10.单词及字母去重排序
按单词出现频率降序排序!
按字母出现频率降序排序!
file8.txt 文件内容
1
No. The Bible says Jesus had compassion2 on them for He saw them as sheep without a shepherd. They were like lost sheep, lost in their sin. How the Lord Jesus loved them! He knew they were helpless and needed a shepherd. And the Good Shepherd knew He had come to help them. But not just the people way back then. For the Lord Jesus knows all about you, and loves you too, and wants to help you.
awk -F "" '{for(i=1;i<=NF;i++)S[$i]++}END{for(key in S)print S[key],key}' file8.txt |sort -rn|head
11.扫描网络内存活主机
扫描192.168.56.1到192.168.56.254之间ip的是否存活,并输出是否在线
服务器ip存活分析
1 2
ping ip地址 -c 2 # 如果ip地址存活发送2个数据包会至少接收返回1个数据包
效果如图
完整脚本代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/bin/bash count=0 for i in 192.168.56.{1..254} do # 使用ping命令发送2个包测试, 并获取返回接收到包的个数 receive=$(ping $i -c 2|awk 'NR==6{print $4}') # 接收返回包大于0 说明主机在线 if [ ${receive} -gt 0 ] then echo "${i} 在线" ((count+=1)) else echo "${i} 不在线" fi