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