几个有意思的运维编程题

使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件,名称例如为:

 
coaolvajcq_oldboy.html  qnvuxvicni_oldboy.html  vioesjmcbu_oldboy.html
gmkhrancxh_oldboy.html  tmdjormaxr_oldboy.html  wzewnojiwe_oldboy.html
jdxexendbe_oldboy.html  ugaywanjlm_oldboy.html  xzzruhdzda_oldboy.html
qcawgsrtkp_oldboy.html  vfrphtqjpc_oldboy.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
}

for i 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的单词(昆仑万维面试题)。
I am oldboy teacher welcome to oldboy training class.

#!/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

 


#!/bin/bash
declare -a array

for i in $(seq 0 32767)
do
array[$i]=`echo $i | md5sum | cut -c 1-8`
done

keys=([0]=21029299 [1]=00205d1c [2]=a3da1677 [3]=1f6d12dd [4]=890684b)
echo "number ------- md5sum"
for key in ${keys[@]}
do
for i in `seq 0 32767`
do
if [[ "${array[i]}" == "$key" ]];then
echo $i ------- $key
fi
done
done





你可能感兴趣的:(编程,脚本,运维)