36. A number of numbers
Demand:
- Use shell to implement it, print only a number of numbers in a text document
Core points:
- Use SED to replace all non -digital characters as empty, and the rest is the number
[[email protected] ~]# cat 36.sh
#!/bin/bash
while read line
do
n=`echo $line |sed 's/[^0-9]'//g|wc -L`
if [ $n -eq 1 ]
then
echo $line
fi
done < test.txt
[[email protected] ~]# sh 36.sh
1jieljinve
37. Log cutting archive
Demand:
- system logrotate tools can complete log cutting and archiving. Write a shell script to achieve a similar archived palace 鞥 you
- Join the server output log is 1. Log, requires a daily archive of one, 1. Log becomes 1.log.1 the second day, and the third day becomes 1.log.2, on the fourth day 1. Log.3 , Until 1. Log.5
Core points:
- MV rename
- After changing the name, re -load the service to generate a new log
[[email protected] ~]# cat 37.sh
#!/bin/bash
cd /data/logs
log =1.log
mv_log()
{
[ -f $1 ] && mv $1 $2
}
[ -f 1.log5 ] && rm -f 1.log.5
for i in `seq 4 -1 1 `
do
j=$[$i+1]
mv_log $log.$i $log.$j
done
mv 1.log 1.log.1
[[email protected] ~]#
38. Find online IP
Demand:
- Write a shell script and list the online IP of 192.168.0.0/24
Core points:
- for cycle all the IP
- See if IP can ping through
################Q[email protected]~]# Cat 38.SH
#!/bin/basn
for I in` SEQ 1 254`
DO
m = `ping -c 4" 192.168.0. $ i "| GREP 'Packets' | AWK -F ',' '{Print $ 3}' | AWK '{Print $ 1}' | AWK -F '%' { Print $ 1} '`
if ["$ M" == 0]
then
echo "192.168.0. $ i online"
FI
Done
################## 实 #################
++ SEQ 1 254
+ for I in '`SEQ 1 254`'
++ AWK '{Print $ 1}'
++ AWK -F % '{PRINT $ 1}'
++ AWK -F, '{Print $ 3}'
++ GREP PACKETS
++ ping -c 4 192.168.0.1
+ M = 100
+ '[' 100 == 0 ']'
+ for I in '`SEQ 1 254`'
++ AWK '{Print $ 1}'
++ AWK -F % '{PRINT $ 1}'
++ AWK -F, '{Print $ 3}'
++ GREP PACKETS
++ PING -C 4 192.168.0.2
+ M = 100
+ '[' 100 == 0 ']'
+ for I in '`SEQ 1 254`'
++ AWK '{Print $ 1}'
++ AWK -F % '{PRINT $ 1}'
++ AWK -F, '{Print $ 3}'
++ GREP PACKETS
++ ping -c 4 192.168.0.3
....
########################## ########[email protected]~]# cat 38_1.SH
#!/bin/bash
for I in` SEQ 1 254`
DO
if Ping -C 2 -W 2 192.168.0. $ 1>/dev/null 2>/dev/null ### -W timeout
then
echo "192.168.0. $ i is common"
else
echo "192.168.0. $ i not access"
Done
39. Check the script syntax error
Demand:
- Write a script to check whether the specified shell script has grammatical errors. If there is an error, first display the error message, and then prompt the user to enter the Q or Q to exit the script.
Core points:
- sh -n
###################Q[email protected]~]# Cat 39.SH
#!/bin/bash
sh -n $ 1 2> /tmp/sh.err
n = `cat sh.err | GREP 'error'`
if [-z $ n]
then
Echo "script correct"
else
cat /tmp/sh.err
Whether the read -p "is launched (q | q):" i
if [$ i "==" q "] || [" $ i "==" q "]
then
exit
else
vim $ 1
FI
FI
#################### ###################[email protected]~]# sh -x 39.SH 38.SH
+ sh -n 38.sh
++ GREP ERROR
++ cat /tmp/sh.err
+ n = '38 .sh: Line 12: Syntax error nearxpected token `done '\' '
+ '[' -z 38.SH: Line 12: Syntax error near unexpected token '`done' \ '' ']'
39.SH: Line 4: [: Too Many Arguments
+ cat /tmp/sh.err
38.SH: Line 12: Syntax error near unexpected token `done '
38.SH: Line 12: `Done '
+ whether to release the script (q | q): 'i
Whether to launch a script (q | Q):
+ '[' '' == q ']'
+ '[' '' == q ']'
+ vim 38.SH[email protected]~]# SH 39.SH 37.SH
"Correct script"
##################### Reference ##################
#!/bin/bash
sh -n $ 1 2> /tmp/sh.err
if [$? -NE 0]
then
cat /tmp/sh.err
Read -P "Please enter the Q/Q exit script." C
if [-z "$ c"]]
then
vim $ 1
exit 0
FI
if [$ c == q] || [$ c == 0]
then
exit 0
else
vim $ 1
exit 0
FI
else
echo "script $ 1 No grammar error"
FI
- 40. Formatal number
Demand:
- Input a string of random numbers, and then output by thousands of points
- For example, the input number string is “123456789” and the output is 123,456,789.
Core points:
- Like all the numbers, each number is stored in temporary files
[[email protected]~]# Cat 40.SH
#!/bin/bash
n =` Echo $ 1 | WC -L`
For d IN `echo $ 1 | SED 'S /./&//G' #. Represents numbers, & indicate the front. After adding spaces, you can output space between characters
DO
n2 = $ [$ n%3]
if [$ n2 -eq 0]
then
echo -n ", $ d" #echo -n, not printing and changing lines
else
echo -n "$ d"
FI
n = $ [$ n-1]
Done | SED 'S/^, //'
echo