近来参加面试,遇到几个运维编程题,在这里做一些记录。
使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件。
#!/bin/bash letter=(`echo {a..z}`) function create_filename() { filename=`for i in $(seq 10) do echo -n ${letter[$((RANDOM%24))]} done`_oldboy.html } fori in $(seq 10) do create_filename touch $filename done
用两种将以上文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写
#!/bin/bash filelist=`ls |grep ".html$"` for i in $filelist do mv $i ${i%%_*}_oldgirl.HTML done
#!/bin/bash filelist=`ls | grep ".html$"` for i in $filelist do newfile=`echo $i | awk -F'_' '{print $1"_oldgirl.HTML"}'` mv $i $newfile done
批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)。
#!/bin/bash for i in `seq 10` do if [[ $i -lt 10 ]];then useradd oldboy0$i && echo `cat /dev/urandom | sed 's/[^a-zA-Z0-9]//g' | strings -n 8 | head -n 1` | passwd oldboy0$i --stdin else useradd oldboy$i && echo `cat /dev/urandom | sed 's/[^a-zA-Z0-9]//g' | strings -n 8 | head -n 1` | passwd oldboy$i --stdin fi done
写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些
单进程
#!/bin/bash for i in `seq 2 255` do ping 10.0.0.$i -c 1 1>/dev/null && echo 10.0.0.$i: live || echo 10.0.0.$i: dead done
多进程
#!/bin/bash tmp_fifofile="/tmp/$$.fifo" mkfifo $tmp_fifofile exec 6<>$tmp_fifofile rm -rf $tmp_fifofile thread=254 for ((i=0;i<$thread;i++)) do echo "" done >&6 for ((i=1;i<254;i++)) do read -u6 { ping 10.0.0.$i -c 1 1>/dev/null && echo 10.0.0.$i: live || echo 10.0.0.$i: dead echo "">&6 } & done wait exec 6>&- exit 0
bash for循环打印下面这句话中字母数不大于6的单词
#!/bin/bash content="I am oldboy teacher welcome to oldboy training class." new_content=`echo $content |sed 's/.//'` echo $new_content arg=(`echo $new_content`) for i in ${arg[@]} do if [[ ${#i} -le 6 ]] ;then echo $i fi done
已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果,请破解这些字符串对应的md5sum前的RANDOM对应数字?
21029299
00205d1c
a3da1677
1f6d12dd
890684b
1 #!/bin/bash 2 declare -a array 3 4 for i in $(seq 0 32767) 5 do 6 array[$i]=`echo $i | md5sum | cut -c 1-8` 7 done 8 9 keys=([0]=21029299 [1]=00205d1c [2]=a3da1677 [3]=1f6d12dd [4]=890684b) 10 echo "number ------- md5sum" 11 for key in ${keys[@]} 12 do 13 for i in `seq 0 32767` 14 do 15 if [[ "${array[i]}" == "$key" ]];then 16 echo $i ------- $key 17 fi 18 done 19 done