2017 the latest enterprise face Test shell (i)
**********************************************
Enterprise Shell face question 1: Batch generate random character file name case *
**********************************************
Use the For loop to bulk create 10 HTML files in the/oldboy directory, where each file needs to contain 10 random lowercase characters plus a fixed string oldboy, with the following name examples:
12345 |
[[email protected] C19] # ls /oldboy apquvdpqbk_oldboy.html mpyogpsmwj_oldboy.html txynzwofgg_oldboy.html bmqiwhfpgv_oldboy.html mtrzobsprf_oldboy.html vjxmlflawa_oldboy.html jhjdcjnjxc_oldboy.html qeztkkmewn_oldboy.html jpvirsnjld_oldboy.html ruscyxwxai_oldboy.html |
Answer:
The script reads as follows: (For loop implementation script)
12345678 |
#!/bin/bash
#date=2017-8-3
cd /oldboy
for
(( 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.html
done
|
Description
The script execution results are as follows:
1234567891011121314 |
[[email protected] 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
|
The script reads as follows: (While loop implementation script)
12345678910 |
#!/bin/bash
#date=2017-8-3
cd
/oldboy
i=0
while
(( 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
|
Description
The script execution results are as follows:
12345678910111213 |
[[email protected] 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
|
************************************
Enterprise Shell face question 2: Batch renaming special case *
************************************
The Oldboy string in the result file name in question 1 above will be changed to oldgirl (preferably with a For loop), and the extension HTML will all be changed to uppercase.
Answer:
The For Loop implementation script is as follows:
1234567 |
#!/bin/bash
#date=2017-8-3
cd
/oldboy
for
i
in
`
ls
|
grep
.*html`
do
mv $i `
echo
$i|
sed
-r
‘s#(.*)_oldboy.html#\1_oldgirl.HTML#‘
`
done
|
Description
The script execution results are as follows:
1234567891011 |
[[email protected] oldboy] # sh for2.sh ahqrvgmewi_oldgirl.HTML cekphjpxmf_oldgirl.HTML fodvkohejd_oldgirl.HTML llymsvwhim_oldgirl.HTML mejlzbfmna_oldgirl.HTML mxqrnbgmnt_oldgirl.HTML pkviyuhrum_oldgirl.HTML shmmlxzkdu_oldgirl.HTML wmgokfirna_oldgirl.HTML wmmknomcmy_oldgirl.HTML |
Other methods reference:http://wutengfei.blog.51cto.com/10942117/1951146
********************************************
Enterprise Shell face question 3: Batch Create special requirements user Stories *
********************************************
Batch Create 10 System account OLDBOY01-OLDBOY10 and set the password (password is random number, require characters and numbers and so on mixed).
Without a For loop implementation idea: http://user.qzone.qq.com/49000448/blog/1422183723
Answer:
1234567 |
#!/bin/bash #date =2018-8-3 for i in ' seq -w 1 10 ' Do useradd < Code class= "Bash plain" >oldboy$i -m echo Code class= "Bash plain" >| passwd --stdin oldboy$i Code class= "Bash keyword" >done |
Description
2017 the latest enterprise face Test Shell (ii)
Exercise 1: write a shell script that lists the IP of the 192.169.5.0/24 network segment online. (Locate the active IP)
Requirements are as follows:
1. The online IP and the online IP are placed in two files respectively to facilitate later inspection;
2. Do not affect the operation of the current terminal;
3. After the script has finished running, give the message that the script has finished running.
The script reads as follows:
Method One:
1234567891011121314151617 |
#!/bin/bash
[ -f
/etc/init
.d
/functions
] && .
/etc/init
.d
/functions
||exit1
# 验证系统函数文件是否存在,如存在则调用系统函数,否则退出!
ips=
"192.169.5."
for
i
in
$(
seq
254)
do
ping
-c 2 $ips$i >
/dev/null 2>
/dev/null
if
[
"$?"
==
"0"
]
then
echo
"echo $ips$i is online"
>>
/root/ip_online
.txt
else
echo
"echo $ips$i is not online"
>>
/root/ip_noline
.txt
fi
done
if
[
"$ips$i"
!=
"192.169.5.255"
];
then
action
"shell脚本执行完毕!"
/bin/true
fi
|
Method Two:
12345678910111213141516 |
#!/bin/bash
[ -f
/etc/init
.d
/functions
] && .
/etc/init
.d
/functions
||exit1
# 验证系统函数文件是否存在,如存在则调用系统函数,否则退出!
for
ip
in
$(
cat
/root/ip
.txt)
do
ping
-c 2 $ip >
/dev/null
2>
/dev/null
if [
"$?"
==
"0"
]
then
echo
"echo $ip is online"
>>
/root/ip_online
.txt
else
echo "echo $ip is not online"
>>
/root/ip_noline
.txt
fi
done
if
[
"$ip"
!=
"192.169.5.255"
];
then
action
"shell脚本执行完毕!"
/bin/true
fi
|
Description
(1) If the script name ip_online.sh, execute the script without affecting the use of the current terminal, using the SH ip_online.sh & command.
(2) It is not recommended to use method two because editing a/root/ip.txt file is a waste of time.
This article is from the "Hand of the Paladin Control" blog, please make sure to keep this source http://wutengfei.blog.51cto.com/10942117/1961226
(ext.) 2017 the shell of the newest enterprise face Test (I, II)