2017年最新企业面试题之shell(一)
**********************************************
企业Shell面试题1:批量生成随机字符文件名案例 *
**********************************************
使用for循环在/oldboy目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串oldboy,名称示例如下:
|
1
2
3
4
5
|
[root@oldgirl C19]# ls /oldboyapquvdpqbk_oldboy.html mpyogpsmwj_oldboy.html txynzwofgg_oldboy.htmlbmqiwhfpgv_oldboy.html mtrzobsprf_oldboy.html vjxmlflawa_oldboy.htmljhjdcjnjxc_oldboy.html qeztkkmewn_oldboy.htmljpvirsnjld_oldboy.html ruscyxwxai_oldboy.html |
解答:
脚本内容如下:(for循环实现脚本)
|
1
2
3
4
5
6
7
8
|
#!/bin/bash#date=2017-8-3cd /oldboyfor (( i=0;i<10;i++ ))do r=` head -c 500 /dev/urandom|tr -dc [a-z]|head -c 10|sed -r ‘s#[^a-z]#m#g‘` touch "$r"_oldboy.htmldone |
说明:
脚本执行结果如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@ceshiji oldboy]# ll总用量 8-rw-r--r-- 1 root root 0 8月 3 13:35 defozmpplm_oldboy.html-rw-r--r-- 1 root root 175 8月 3 13:35 for.sh-rw-r--r-- 1 root root 0 8月 3 13:35 fpwxukgqho_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:35 gwtwtmdwaf_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:35 htttummyjh_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:35 myqmgyixuj_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:35 rmsyxakjbx_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:35 sxarorojil_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:35 wfqaymdmxc_oldboy.html-rw-r--r-- 1 root root 184 8月 3 13:32 while.sh-rw-r--r-- 1 root root 0 8月 3 13:35 yvgviwswze_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:35 zhikoyxipv_oldboy.html |
脚本内容如下:(while循环实现脚本)
|
1
2
3
4
5
6
7
8
9
10
|
#!/bin/bash#date=2017-8-3cd /oldboyi=0while (( i<10 ))do r=` head -c 500 /dev/urandom|tr -dc [a-z]|head -c 10|sed -r ‘s#[^a-z]#m#g‘` touch "$r"_oldboy.html ((i++))done |
说明:
脚本执行结果如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@ceshiji oldboy]# ll总用量 4-rw-r--r-- 1 root root 0 8月 3 13:28 atncjmfwtd_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:28 bmbhdhvqmb_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:28 bmuqvcehgc_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:28 irfrxjjmny_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:28 mvprsmwvah_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:28 nxyfjkvekl_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:28 omqyxuykgq_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:28 smkxwfjepd_oldboy.html-rw-r--r-- 1 root root 184 8月 3 13:28 while.sh-rw-r--r-- 1 root root 0 8月 3 13:28 wkwmkktdcn_oldboy.html-rw-r--r-- 1 root root 0 8月 3 13:28 xtznpcuxem_oldboy.html |
************************************
企业Shell面试题2:批量改名特殊案例 *
************************************
将以上面试题1中结果文件名中的oldboy字符串全部改成oldgirl(最好用for循环实现),并且将扩展名html全部改成大写。
解答:
for循环实现脚本如下:
|
1
2
3
4
5
6
7
|
#!/bin/bash#date=2017-8-3cd /oldboyfor i in `ls|grep .*html`do mv $i `echo $i|sed -r ‘s#(.*)_oldboy.html#\1_oldgirl.HTML#‘`done |
说明:
脚本执行结果如下:
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@ceshiji oldboy]# sh for2.shahqrvgmewi_oldgirl.HTMLcekphjpxmf_oldgirl.HTMLfodvkohejd_oldgirl.HTMLllymsvwhim_oldgirl.HTMLmejlzbfmna_oldgirl.HTMLmxqrnbgmnt_oldgirl.HTMLpkviyuhrum_oldgirl.HTMLshmmlxzkdu_oldgirl.HTMLwmgokfirna_oldgirl.HTMLwmmknomcmy_oldgirl.HTML |
其他方法参考:http://wutengfei.blog.51cto.com/10942117/1951146
********************************************
企业Shell面试题3:批量创建特殊要求用户案例 *
********************************************
批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机数,要求字符和数字等混合)。
不用for循环的实现思路:http://user.qzone.qq.com/49000448/blog/1422183723
解答:
|
1
2
3
4
5
6
7
|
#!/bin/bash#date=2018-8-3for i in `seq -w 1 10`do useradd oldboy$i -M echo "`openssl rand -base64 8`"|passwd --stdin oldboy$idone |
说明:
2017年最新企业面试题之shell(二)
练习题1:写一个shell脚本,将192.169.5.0/24网段在线的ip列出来。(找出活动ip)
要求如下:
1.将在线ip与不在线ip分别放在两个文件中,方便后期查阅;
2.不影响对当前终端进行操作;
3.脚本运行结束后,给予提示信息,表明脚本已经运行结束。
脚本内容如下:
方法一:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/bin/bash[ -f /etc/init.d/functions ] && . /etc/init.d/functions||exit1# 验证系统函数文件是否存在,如存在则调用系统函数,否则退出!ips="192.169.5."for i in $(seq 254)doping -c 2 $ips$i >/dev/null 2>/dev/nullif [ "$?" == "0" ]then echo "echo $ips$i is online" >>/root/ip_online.txtelse echo "echo $ips$i is not online" >>/root/ip_noline.txtfidoneif [ "$ips$i" != "192.169.5.255" ];then action "shell脚本执行完毕!" /bin/truefi |
方法二:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/bin/bash[ -f /etc/init.d/functions ] && . /etc/init.d/functions||exit1 # 验证系统函数文件是否存在,如存在则调用系统函数,否则退出!for ip in $(cat /root/ip.txt)doping -c 2 $ip >/dev/null 2>/dev/nullif [ "$?" == "0" ]then echo "echo $ip is online" >>/root/ip_online.txtelse echo "echo $ip is not online" >>/root/ip_noline.txtfidoneif [ "$ip" != "192.169.5.255" ];then action "shell脚本执行完毕!" /bin/truefi |
说明:
(1)如果脚本名字 ip_online.sh ,则执行脚本时为不影响当前终端的使用,使用 sh ip_online.sh &命令执行。
(2)不建议使用方法二,因为编辑/root/ip.txt文件太浪费时间。
本文出自 “圣骑士控魔之手” 博客,请务必保留此出处http://wutengfei.blog.51cto.com/10942117/1961226
原文:http://www.cnblogs.com/liujiacai/p/7819458.html