shell练习
Posted Howhy Blogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell练习相关的知识,希望对你有一定的参考价值。
目录:
1、监控mysql主从同步
企业面试题1:监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)
模拟文本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
[[email protected]~] # mysql -uroot-p‘oldboy‘ -S /data/3307/mysql.sock -e "show slavestatus\G;" *************************** 1.row *************************** Slave_IO_State:Waiting formaster to send event Master_Host:10.0.0.179 #当前的mysql master服务器主机 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File:mysql-bin.000013 Read_Master_Log_Pos: 502547 Relay_Log_File:relay-bin.000013 Relay_Log_Pos:251 Relay_Master_Log_File:mysql-bin.000013 Slave_IO_Running:Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 502547 Relay_Log_Space:502986 Until_Condition:None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 #和主库比同步延迟的秒数,这个参数很重要 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 脚本 [[email protected] scripts] # vicheck_mysql_slave.sh #!/bin/sh #oldboy linux training #2015-05-17 #说明:本脚本来自老男孩linux21期学员张耀开发! # Source function library. . /etc/init .d /functions # Defined variables MysqlUser=root MysqlPass=oldboy123 MysqlPort=3307 Mysqlsock= /data/ $MysqlPort /mysql .sock ErrorNo=(1158 1159 1008 10071062) errorlog= /tmp/error_skip .log MysqlCmd= "/application/mysql/bin/mysql-u$MysqlUser -p$MysqlPass -S $Mysqlsock" # Judge mysql server is ok? [ -S $Mysqlsock ] ||{ echo "Maybe MySQL have sometingwrong" exit 1 } # Defined skip error Functions function error_skip(){ local flag flag=0 for num in ${ErrorNo[@]} do if [ "$1" == "$num" ]; then $MysqlCmd -e ‘stop slave;set globalsql_slave_skip_counter=1;start slave;‘ echo "$(date +%F_%R) $1" >>$errorlog else echo "$(date +%F_%R) $1" >>$errorlog ((flag++)) fi done [ "$flag" == "${#ErrorNo[@]}" ] &&{ action "MySQL Slave" /bin/false } } # Defined check slave Functions function check_slave(){ MyResult=`$MysqlCmd -e ‘show slavestatus\G‘ | egrep ‘_Running|Behind_Master|SQL_Errno‘ | awk ‘{print $NF}‘ ` array=($MyResult) if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[2]}" == "0" ] then action "MySQL Slave" /bin/true else error_skip ${array[3]} fi } # Defined main Functions function main(){ while true do check_slave sleep 60 done } main |
2、批量创建文件及改名
企业面试题2:使用for循环在/oldboy目录下通过随机小写10个字母,批量创建10个html文件,名称例如为:
[[email protected] oldboy]# sh/server/scripts/oldboy.sh
[[email protected] oldboy]# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 1511:34 coaolvajcq_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 gmkhrancxh_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 jdxexendbe_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 qcawgsrtkp_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 qnvuxvicni_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 tmdjormaxr_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 ugaywanjlm_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 vfrphtqjpc_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 vioesjmcbu_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 wzewnojiwe_oldboy.html
-rw-r--r-- 1 root root 0 Apr 1511:34 xzzruhdzda_oldboy.html
请用至少两种方法实现,将以上文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
2.1批量创建脚本 [[email protected] scripts] # vitouch2.sh #!/bin/bash Path= /oldboy [ -d $Path ] || mkdir $Path for i in ` seq 10` do char=` echo $RANDOM|md5sum| cut -c 2-11| tr [0-9] [a-j]` touch $Path/${char}_oldboy.html done [[email protected] scripts] # shtouch2.sh [[email protected] scripts] # ll -h/oldboy/ total 0 -rw-r--r-- 1 root root 0 Apr 1107:49 aeddbiacdj_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 bccbacghba_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 bdhijefefb_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 bhfghjcgaa_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 ddaacedijc_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 ebcbbfabaf_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 ecafccebbi_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 egcdafafad_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 iehfcgfaef_oldboy.html -rw-r--r-- 1 root root 0 Apr 1107:49 ifddfaaicd_oldboy.html 2.2批量改名 [[email protected] scripts] # vimv2.sh #!/bin/bash Path= /oldboy [ -d $Path ] && cd $Path for file in ` ls ` do mv $ file ` echo $ file | sed -e "s#oldboy#oldgirl#g" -e "s#html#HTML#g" ` done [[email protected] scripts] # sh mv2.sh [[email protected] scripts] # ll -h/oldboy/ total 0 -rw-r--r-- 1 root root 0 Apr 1107:49 aeddbiacdj_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 bccbacghba_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 bdhijefefb_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 bhfghjcgaa_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 ddaacedijc_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 ebcbbfabaf_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 ecafccebbi_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 egcdafafad_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 iehfcgfaef_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1107:49 ifddfaaicd_oldgirl.HTML [[email protected] scripts] # vimv3.sh #!/bin/bash Path= /oldboy [ -d $Path ] && cd $Path for file in ` ls ` do mv $ file ` echo ${ file /oldboy .html /oldgirl .HTML}` done [[email protected] scripts] # sh mv3.sh [[email protected] scripts] # ll -h/oldboy/ total 0 -rw-r--r-- 1 root root 0 Apr 1108:12 abcaeacdbe_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 affabgbccg_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 badbifffbg_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 ccbcifibbe_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 cchbacgegb_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 cdfbjfdiib_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 chjechdgab_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 ghibcfcbee_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 icafaafbdb_oldgirl.HTML -rw-r--r-- 1 root root 0 Apr 1108:12 igiijhbebj_oldgirl.HTML |
3、批量创建用户随机密码
企业面试题3:批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
[[email protected] scripts] # viuseradd.sh #! /bin/bash . /etc/init .d /functions Path= /server/scripts UserDb=$Path /user .db FailDb=$Path /fail_user .db [ -d "$Path" ] || mkdir -p $Path [ -f "$UserDb" ] || touch $UserDb [ -f "$FailDb" ] || touch $FailDb for n in $( seq -w 10) do passwd =` echo $( date +%t%N)$RANDOM|md5sum| cut -c 2-9` useradd oldboy$n >& /dev/null && user_status=$? echo "$passwd" | passwd --stdinoldboy$n >& /dev/null && pass_status=$? if [ $user_status - eq 0 -a $pass_status -eq0 ]; then action "adduser oldboy$n" /bin/true echo -e "user:\toldboy$npass:$passwd" >>$UserDb else action "adduser oldboy$n" /bin/false echo -e "user:\toldboy$npass:$passwd" >>$FailDb fi done [[email protected] scripts] # shuseradd.sh adduser oldboy01 [ OK ] adduser oldboy02 [ OK ] adduser oldboy03 [ OK ] adduser oldboy04 [ OK ] adduser oldboy05 [ OK ] adduser oldboy06 [ OK ] adduser oldboy07 [ OK ] adduser oldboy08 [ OK ] adduser oldboy09 [ OK ] adduser oldboy10 [ OK ] [[email protected] scripts] # cat user.db user: oldboy01 pass:f3291720 user: oldboy02 pass:457a9f30 user: oldboy03 pass:ff186389 user: oldboy04 pass:8f884b7c user: oldboy05 pass:26f831b4 user: oldboy06 pass:344e2300 user: oldboy07 pass:0736b278 user: oldboy08 pass:67c1fa76 user: oldboy09 pass:b11e7aa9 user: oldboy10 pass:9e0c3673 |
4、判断网络主机存活
企业面试题4:写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多)
1
2
3
4
5
6
7
8
9
10
11
|
#!/bin/sh for n in ` seq 254` do ping -c1 10.0.0.$n &> /dev/null if [ $? - eq 0 ] then echo "10.0.0.$n is up " >> /tmp/uplist .log else echo "10.0.0.$n is down " >> /tmp/downlist .log fi done |
5、解决DOS攻击生产案例
企业实战题5:请用至少两种方法实现!写一个脚本解决DOS攻击生产案例。
提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables-AINPUT -s 10.0.1.10 -j DROP。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
[[email protected] scripts] # vi dos.sh #!/bin/bash log= /tmp/tmp .log [ -f $log ] || touch $log function add_iptables(){ whileread line do ip=` echo $line| awk ‘{print $2}‘ ` count=` echo $line| awk ‘{print $1}‘ ` if [ $count -gt 100 ] && [`iptables -L -n| grep "$ip" | wc -l` -lt 1 ] then iptables -I INPUT -s $ip -jDROP echo "$line isdropped" >> /tmp/droplist .log fi done <$log } function main(){ whiletrue do #awk ‘{print $1}‘ access.log|grep-v "^$"|sort|uniq -c >$log netstat -an| grep EST| awk -F ‘[:]+‘ ‘{print $6}‘ | sort | uniq -c >$log add_iptables sleep 180 done } main |
6、MySQL启动脚本
企业实战题6:开发mysql多实例启动脚本:
已知mysql多实例启动命令为:mysqld_safe--defaults-file=/data/3306/my.cnf&
停止命令为:mysqladmin -u root -poldboy123 -S /data/3306/mysql.sockshutdown
请完成mysql多实例启动启动脚本的编写
要求:用函数,case语句、if语句等实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
[[email protected] scripts] # vi mysqld1.sh #!/bin/bash # chkconfig: 2345 65 37 # description: manager multiplemysqld server. # Warning: This script uses the/etc/init.d/functions system function. # System kernel version isCentOS6.6,2.6.32-504.el6.x86_64. # I‘m not sure whether other systemscan be used normally. # Source function library. . /etc/init .d /functions Port=$2 datadir= /data/ $2 Path= "/application/mysql" CmdPath= "$Path/bin" MysqlCmd= "$CmdPath/mysqld_safe" prog=mysqld Port=$2 datadir= /data/ $2 pidfile=${PIDFILE-$datadir /mysqld .pid} lockfile=${LOCKFILE- /var/lock/subsys/mysqld $Port} MysqlConf= "$datadir/my.cnf" RETVAL=0 Usage1(){ echo -e "\033[33m USAGE: $0 {startport|stop port|status port|restart port} \033[0m" exit 6 } Usage2(){ echo -e "\033[33m MySQL DaemonProgram Need Configuration File,like $datadir/my.cnf \033[0m" exit 6 } [ -n "$Port" -a -z "`echo" ${Port // [0-9]/} "`" ] || Usage1 start(){ [ -x $MysqlCmd ] || exit 5 [ -f $MysqlConf ] || Usage2 echo -n $ "Starting $prog: " daemon --pidfile=${pidfile} "$MysqlCmd --defaults-file=$MysqlConf &>/dev/null &" sleep 1 RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } stop(){ echo -n $ "Stopping $prog: " killproc -p $pidfile retval=$? echo [ $retval - eq 0 ] && rm -f${lockfile} return $retval } restart() { stop sleep 1 start } rh_status() { status -p $pidfile } rh_status_q() { rh_status > /dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; status) rh_status ;; *) Usage1 exit 2 esac |
7、分库备份
企业实战题7:如何实现对MySQL数据库进行分库备份,请用脚本实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[[email protected] scripts] # vifenku_backup.sh #!/bin/bash MysqlUser=root PassWord=oldboy123 Port=3306 Socket= "/data/$Port/mysql.sock" MysqlCmd= "mysql -u$MysqlUser-p$PassWord -S $Socket" Database=`$MysqlCmd -e "showdatabases;" | egrep - v "Database|_schema|mysql" ` MysqlDump= "mysqldump-u$MysqlUser -p$PassWord -S $Socket" IP=` ifconfig eth0| awk -F "[:]+" ‘NR==2 {print $4}‘ ` BackupDir= /backup/ $IP [ -d $BackupDir ] || mkdir -p$BackupDir for dbname in $Database do $MysqlDump --events -B $dbname| gzip >/$BackupDir/${dbname}_$( date +%F)_bak.sql.gz done [[email protected] 10.0.0.3] # ls chongtu_2015-04-11_bak.sql.gz oldboy_2015-04-11_bak.sql.gz test_2015-04-11_bak.sql.gz mysql_2015-04-11_bak.sql.gz qqqqqq_2015-04-11_bak.sql.gz |
8、分库分表备份
企业实战题8:如何实现对MySQL数据库进行分库加分表备份,请用脚本实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[[email protected] scripts] # vifenbiao_backup.sh #!/bin/bash MysqlUser=root PassWord=oldboy123 Port=3306 Socket= "/data/$Port/mysql.sock" MysqlCmd= "mysql -u$MysqlUser-p$PassWord -S $Socket" Database=`$MysqlCmd -e "showdatabases;" | egrep - v "Database|_schema|mysql" ` MysqlDump= "mysqldump-u$MysqlUser -p$PassWord -S $Socket" IP=` ifconfig eth0| awk -F "[:]+" ‘NR==2 {print $4}‘ ` BackupDir= /backup/ $IP [ -d $BackupDir ] || mkdir -p$BackupDir for dbname in $Database do [ ! -d /$BackupDir/$dbname ] && mkdir -p /$BackupDir/$dbname TABLE=`$MysqlCmd -e "show tables from$dbname;" | sed ‘1d‘ ` for table in $TABLE do $MysqlDump $dbname $table| gzip >/$BackupDir/$dbname/${dbname}_${table}_$( date +%F).sql.gz done done |
9、打印字母数不大于6的单词
企业面试题9: bash for循环打印下面这句话中字母数不大于6的单词(昆仑万维面试题)。
Iam oldboy teacher welcome to oldboy training class.
请用至少两种方法实现!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
[[email protected] scripts] # vi length.sh #!/bin/bash echo "-------wc -L----" for word in I am oldboy teacherwelcome to oldboy training class. do if [ ` echo ${word}| wc -L` - le 6 ] then echo $word fi done echo "-----{#word}-----" for word in I am oldboy teacherwelcome to oldboy training class. do if [ ${ #word} -le 6 ] then echo $word fi done echo "-----exprlength-----" for word in I am oldboy teacherwelcome to oldboy training class. do if [ ` expr length "$word" ` - le 6] then echo $word fi done echo "-----{word:0:6}-----" for word in I am oldboy teacherwelcome to oldboy training class. do if [ "$word" == "${word:0:6}" ] then echo $word fi done echo "-----awk-----" echo "I am oldboy teacherwelcome to oldboy training class" | awk ‘{for(i=1;i<=NF;i++)if(length($i)<=6)print $i}‘ echo "-----awk2-----" echo -n "I am oldboy teacherwelcome to oldboy training class." | awk ‘BEGIN {RS=FS} length($0)<=6{print $0}‘ echo "-----数组-----" arr=(I am oldboy teacher welcometo oldboy training class.) for word in ${arr[@]} do if [ ${ #word} -le 6 ] then echo $word fi done echo ----------------------------- for ((i=0;i<${ #arr[*]};i++)) do if [ ${ #arr[$i]} -le 6 ] then echo ${arr[$i]} fi done [[email protected] scripts] # shlength.sh ------- wc -L---- I am oldboy to oldboy class. -----{ #word}----- I am oldboy to oldboy class. ----- expr length----- I am oldboy to oldboy class. -----{word:0:6}----- I am oldboy to oldboy class. ----- awk ----- I am oldboy to oldboy class -----awk2----- I am oldboy to oldboy class. -----数组----- I am oldboy to oldboy class. ----------------------------- I am oldboy to oldboy class. |
以上是关于shell练习的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段
Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段