在計算機科學中,for循環(英語:for loop)是一種編程語言的迭代陳述,可以讓程式碼反覆的執行。html 它跟其餘的循環,如while循環,最大的不一樣,是它擁有一個循環計數器,或是循環變數。這使得for循環可以知道在迭代過程當中的執行順序。linux 1.1.1 shell中的for循環shell中的for 循環與在c中不一樣,它包含三種形式:第一種結構是列表for 循環;第二種結構就是不帶列表的for循環;第三種就相似於C語言。nginx ① 列表for循環(經常使用)web #!/bin/bash for i in 取值列表 do 循環主體/命令 done
② 不帶列表for循環(示例)面試 #!/bin/absh echo "慘綠少年的博客是:" for i do echo "$i" done 腳本執行結果shell [root@clsn for]# sh for2.sh http://blog.znix.top 慘綠少年的博客是: http://blog.znix.top
|
③ 相似C語言的風格(這種用法常在C語語言中使用)數據庫
for((exp1;exp2;exp3)) do 指令... done
編寫相似C語言風格腳本編程
for((i=0;i<=3;i++)) do echo $i done
腳本執行過程swift
Shell中的兩種樣式vim
# 樣式一: for i in 1 2 3 do echo $i done # 樣式二: for i in 1 2 3;do echo $i;done
JAVA
for(int i = 0; i < 5; i++){ //循環語句; }
PHP
for ($i = 0; $i < 5; $i++) { # statements; }
VB
For i = 1 To 5 ===PASCAL=== for not i=1 do begin i=0; writeln('Go on!'); end. '循環語句 Next i
swift
var x = 0 for i in 1...100{ x += i } print(x) //5050 for _ in 1...100{ x += 1 } print(x) // 100 var box = [1,2,3,4,5] for i in box{ print(i) } /* 1 2 3 4 5 */ ---
使用for循環在/clsn目錄下批量建立10個html文件,其中每一個文件須要包含10個隨機小寫字母加固定字符串clsn,名稱示例以下:
[root@znix C19]# ls /clsn apquvdpqbk_clsn.html mpyogpsmwj_clsn.html txynzwofgg_clsn.html bmqiwhfpgv_clsn.html udrzobsprf_clsn.html vjxmlflawa_clsn.html jhjdcjnjxc_clsn.html qeztkkmewn_clsn.html jpvirsnjld_clsn.html ruscyxwxai_clsn.html
腳本內容
1 [root@clsn for]# cat make_file.sh 2 #!/bin/bash 3 ############################################################# 4 # File Name: make_file.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-08 11:01:19 9 # Description: 10 ############################################################# 11 12 [ -d /clsn ] || mkdir -p /clsn 13 rpm -qa |grep pwgen &>/dev/null 14 if [ $? -eq 1 ] 15 then 16 yum install pwgen -y &>/dev/null 17 fi 18 19 cd /clsn &&\ 20 for i in {1..10} 21 do 22 #File_Name=`uuidgen |tr "0-9-" "a-z"|cut -c 1-10` 23 File_Name2=`pwgen -1A0 10` 24 touch ${File_Name2}_clsn.html 25 done
腳本執行結果
[root@clsn for]# ls -l /clsn -rw-r--r-- 1 root root 0 12月 8 19:41 aegheiriek_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 aifohxique_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 caexahween_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 ciefaivaib_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 eixongooph_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 foozaivedo_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 ireeteethu_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 ohmeebivae_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 oiceehahth_clsn.html -rw-r--r-- 1 root root 0 12月 8 19:41 sheewaehoo_clsn.html
【練習題1】中結果文件名中的clsn字符串所有改爲znix(最好用for循環實現),而且將擴展名html所有改爲大寫。
jpvirsnjld_clsn.html ===> jpvirsnjld_znix.HTML
腳本內容:
1 [root@clsn for2]# cat rename_file.sh 2 #!/bin/bash 3 ############################################################# 4 # File Name: rename_file.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-08 11:31:56 9 # Description: 10 ############################################################# 11 12 cd /clsn &&\ 13 File_name=`ls |sed -r 's#(.*)_clsn.html#\1#g'` 14 15 for i in $File_name 16 do 17 if [ -f ${i}_clsn.html ] 18 then 19 mv ${i}_clsn.html ${i}_znix.HTML 20 else 21 echo "文件修改完成." 22 exit 23 fi 24 done
查看結果
[root@clsn for2]# ls /clsn/ aeyaesughi_znix.HTML caireasipi_znix.HTML uahahnieli_znix.HTML aifaepheeb_znix.HTML eathixoong_znix.HTML zalipageen_znix.HTML akuipheeye_znix.HTML ietoothaik_znix.HTML apheikieno_znix.HTML lachohtaif_znix.HTML
rename 方式(最方便,專業更名)
rename txt jpg *
非 for 循環方式批量更名(使用sed命令進行拼接,而後交給bash執行)
ls *jpg|sed -r 's#(.*).jpg#mv & \1.mp4#'|bash
批量建立10個系統賬號clsn01-clsn10並設置密碼(密碼爲隨機數,要求字符和數字等混合)。
腳本內容:
查當作的密碼文件
[root@clsn for2]# cat /tmp/3e5c18d9-f878-4d06-931e-5bbcc810c3dc.txt 用戶名:clsn01 密碼:3d4644d0-9cf4-49db-8928-1a8346972c32 用戶名:clsn02 密碼:70798c3a-c8e3-42a0-9942-d4011ce4b4b3 用戶名:clsn03 密碼:db2a0f1d-2e49-44f5-a5b2-69b352b30120 用戶名:clsn04 密碼:62d2e0c6-b755-4b00-ad2d-c98f9ca9f258 用戶名:clsn05 密碼:eaa3471b-d04f-4d7c-8b7e-3d75172a483b 用戶名:clsn06 密碼:fb260a11-cd47-4b97-ab49-0cae7a755262 用戶名:clsn07 密碼:16ee7a1f-8aac-4537-b1aa-7fc75beb8754 用戶名:clsn08 密碼:0dde8823-b97d-4c88-9258-3a68a3b53eba 用戶名:clsn09 密碼:daf14ec4-ba9f-4593-9773-1557fdf605dc 用戶名:clsn10 密碼:6f1b452c-00b2-44a1-9f43-5f658d3a9124
腳本執行過程:
方法一
echo user{1..20}|xargs -n1|sed -r 's#(.*)#useradd \1 \&\& echo \1 >>/tmp/passwd.txt \&\& echo $RANDOM |md5sum |cut -c 1-5>>/tmp/passwd.txt \&\& echo `tail -1 /tmp/passwd.txt`|passwd --stdin \1#g'|bash
方法二
echo user{1..20}|xargs -n1|sed -r 's#(.*)#useradd \1 \&\& pass=`echo $RANDOM |md5sum |cut -c 1-5` \&\& echo $pass |passwd --stdin \1 \&\& echo \1 $pass>>/tmp/user_passwd.txt#g'|bash
方法三
echo user{1..20}|xargs -n1|sed -r 's#(.*)#useradd \1 \&\& pass=`echo $RANDOM |md5sum |cut -c 1-5` \&\& echo \1:$pass>>/tmp/user_passwd.txt \&\& chpasswd</tmp/user_passwd.txt#g'|bash
寫一個Shell腳本,判斷10.0.0.0/24網絡裏,當前在線的IP有哪些?
腳本內容:
腳本執行結果
[root@clsn for]# time sh scan_ip2.sh 使用 cat /tmp/scan_ip.txt 查看掃描結果 real 0m0.290s user 0m0.001s sys 0m0.039s [root@clsn for]# cat /tmp/scan_ip.txt 存活主機: 10.0.0.180 存活主機: 10.0.0.254
利用bash for循環打印下面這句話中字母數不大於6的單詞(某企業面試真題)。
I am clsn Welcome to my blog http://blog.znix.top
腳本內容:
1 [root@clsn for]# vim changdu.sh 2 #!/bin/bash 3 ############################################################# 4 # File Name: changdu.sh 5 # Version: V1.0 6 # Author: clsn 7 # Organization: http://blog.znix.top 8 # Created Time : 2017-12-07 22:36:48 9 # Description: 10 ############################################################# 11 12 Word='I am clsn Welcome to my blog http://blog.znix.top' 13 14 for i in $Word 15 do 16 #[ ${#i} -le 6 ] && echo $i #子串方法 17 a=`echo $i |wc -L` 18 if [ $a -le 6 ] 19 then 20 echo $i 21 fi 22 done
腳本執行結果
[root@clsn for]# sh changdu.sh I am clsn to my blog
方法二:
read -p "請輸入要判斷的語句:" a set -- $a for i in "$@" do if [ ${#i} -le 6 ];then echo "$i" fi done
由 https://home.cnblogs.com/u/1233234 @貳佰 提供
使用expr 計算字符串長度
[root@clsn scripts]# expr length '111' 3
已知下面的字符串是經過RANDOM隨機數變量md5sum後,再截取一部分連續字符串的結果,請破解這些字符串對應的使用md5sum處理前的RANDOM對應的數字?
21029299 00205d1c a3da1677 1f6d12dd 890684b
腳本內容
腳本執行結果
[root@clsn for]# sh pojie.sh 2102929901ee1aa769d0f479d7d78b05 - 25667 00205d1cbbeb97738ad5bbdde2a6793d - 1346 a3da1677501d9e4700ed867c5f33538a - 25345 1f6d12dd61b5c7523f038a7b966413d9 - 7041 890684ba3685395c782547daf296935f - 10082
獲取博客園(慘綠少年)博客列表倒序排序考試題
需求以下:
請把https://www.cnblogs.com/clsn/地址中的全部博文,按照時間倒序列表以下:
2017年12月8日 Shell編程基礎篇-下
http://www.cnblogs.com/clsn/p/8006210.html
2017年12月7日 memcached 緩存數據庫應用實踐
http://www.cnblogs.com/clsn/p/7999510.html
高級要求:
生成html頁面,並設置超連接。
結果如改網頁展現:http://www.cnblogs.com/clsn/p/8007232.html
腳本內容:
腳本成網頁文件
腳本內容
附 文本編碼轉碼
[root@clsn for]# iconv --help 用法: iconv [選項...] [文件...] 轉換給定文件的編碼。 輸入/輸出格式規範: -f, --from-code=名稱 原始文本編碼 -t, --to-code=名稱 輸出編碼 信息: -l, --list 列舉全部已知的字符集 輸出控制: -c 從輸出中忽略無效的字符 -o, --output=FILE 輸出文件 -s, --silent 關閉警告 --verbose 打印進度信息 -?, --help 給出該系統求助列表 --usage 給出簡要的用法信息 -V, --version 打印程序版本號 長選項的強制或可選參數對對應的短選項也是強制或可選的
經常使用:
iconv -f gb2312 -t utf-8 -c clsn.html
在編程語言中,while循環(英語:while loop)是一種控制流程的陳述。利用一個返回結果爲布林值(Boolean)的表達式做爲循環條件,當這個表達式的返回值爲「真」(true)時,則反覆執行循環體內的程式碼;若表達式的返回值爲「假」(false),則再也不執行循環體內的代碼,繼續執行循環體下面的代碼。 由於while循環在區塊內代碼被執行以前,先檢查陳述是否成立,所以這種控制流程一般被稱爲是一種前測試循環(pre-test loop)。相對而言do while循環,是在循環區塊執行結束以後,再去檢查陳述是否成立,被稱爲是後測試循環。 |
|
while 條件 do 命令 done
多用於建立守護進程
【示例1】:while實現web服務器搭建
腳本代碼
[root@clsn scripts]# vim web_view.sh #!/bin/bash ############################################################# # File Name: web_view.sh # Version: V1.0 # Author: clsn # Organization: http://blog.znix.top # Created Time : 2017-12-11 10:07:24 # Description: ############################################################# while true do echo "ok" | nc -l 81 done
客戶端進行訪問測試
[root@clsn html]# curl 10.0.0.180:81 ok
服務端顯示結果:
[root@clsn scripts]# sh web_view.sh GET / HTTP/1.1 User-Agent: curl/7.29.0 Host: 10.0.0.180:81 Accept: */*
【示例2】:while建立定時任務
腳本內容:
#!/bin/bash while true do uptime sleep 0.6 done
腳本執行結果
[root@clsn while]# sh while1.sh 15:01:52 up 2 days, 6:02, 3 users, load average: 0.00, 0.01, 0.05 15:01:53 up 2 days, 6:02, 3 users, load average: 0.00, 0.01, 0.05 15:01:53 up 2 days, 6:02, 3 users, load average: 0.00, 0.01, 0.05 15:01:54 up 2 days, 6:02, 3 users, load average: 0.00, 0.01, 0.05 15:01:55 up 2 days, 6:02, 3 users, load average: 0.00, 0.01, 0.05 15:01:55 up 2 days, 6:02, 3 users, load average: 0.00, 0.01, 0.05
說明:
sleep 與 usleep
sleep 單位 秒 sleep 1 休息1秒
usleep 單位 微秒 usleep 1000000 休息1s
1微秒等於百萬分之一秒(10的負6次方秒)
時間測試
[root@clsn while]# time sleep 0.1 real 0m0.102s user 0m0.001s sys 0m0.000s
補充定時任務功能,執行小於1秒的定時任務
循環執行某些操做,例如水果菜單
示例1:使用while循環實現水果菜單的重複使用
腳本內容
腳本執行過程
示例2:計算1-100的和
方法一 (bc命令實現)
echo `seq -s + 1 100`|bc
方法二(while循環方法)
[root@clsn while]# cat jishan.sh #!/bin/bash ############################################################# # File Name: jishan.sh # Version: V1.0 # Author: clsn # Organization: http://blog.znix.top # Created Time : 2017-12-09 15:18:44 # Description: ############################################################# i=1 while [ "$i" -le 100 ] do ((b=b+i)) ((i++)) done echo $b
示例3:實現相似手機通信計費功能
腳本內容:
方法一
while read line; do echo $line; done < file.txt
方法二
cat file.txt|while read line do echo $line done
方法三
exec < file.txt while read line; do echo line; done
for word in $line; do echo $word; done
word=participate for ((i=0;i<${#word};i++)) do echo ${word:1:1}; done
腳本內容
#!/bin/bash n=1 while read i do echo "第${n}行 $i" m=1 for x in $i do echo "第${m}個單詞 $x" echo $x|grep -o . ((m++)) done ((n++)) done < $1
腳本執行結果:
[root@clsn ~]# clsn=6 [root@clsn ~]# echo {1..$clsn} {1..6} [root@clsn ~]# eval echo {1..$clsn} 1 2 3 4 5 6
eval 命令的說明
[root@clsn ~]# help eval eval: eval [參數 ...] 將參數做爲 shell 命令執行。 將 ARGs 合成一個字符串,用結果做爲 shell 的輸入, 而且執行獲得的命令。 退出狀態: 以命令的狀態退出,或者在命令爲空的狀況下返回成功。
條件與循環控制及程序返回值命令表
命令 |
說明 |
break n |
若是省略n,則表示跳出整個循環,n表示跳出循環的層數 |
continue n |
若是省略n,則表示跳過本次循環,忽略本次循環的剩餘代碼,進人循環的下一次循環。 n表示退到第n層繼續循環 |
exit n |
退出當前Shell程序,n爲上一次程序執行的狀態返回值。n也能夠省略,在下一個Shell裏可經過"$?"接收exit n的n值 |
return n |
用於在函數裏做爲函數的返回值,以判斷函數執行是否正確。在下一個Shell裏可經過"$?"接收exit n的n值 |
簡單來講即:
break 跳出循環 continue 跳出本次循環 exit 退出腳本 return 與 exit 相同,在函數中使用
[root@clsn scripts]# help break break: break [n] 退出 for、while、或 until 循環 退出一個 FOR、WHILE 或 UNTIL 循環。若是指定了N,則打破N重 循環 退出狀態: 退出狀態爲0除非 N 不大於或等於 1。
測試腳本內容
腳本執行結果
[root@clsn scripts]# sh break.sh 1 2 ok
[root@clsn scripts]# help continue continue: continue [n] 繼續 for、while、或 until 循環。 繼續當前 FOR、WHILE 或 UNTIL 循環的下一步。 若是指定了 N, 則繼續當前的第 N 重循環。 退出狀態: 退出狀態爲 0 除非 N 不大於或等於1。
測試腳本內容
腳本執行結果
[root@clsn scripts]# sh continue.sh 1 2 4 5 ok
[root@clsn scripts]# help exit exit: exit [n] 退出shell。 以狀態 N 退出 shell。 若是 N 被省略,則退出狀態 爲最後一個執行的命令的退出狀態。
測試腳本內容
腳本執行結果
[root@clsn scripts]# sh exit.sh 1 2
[root@clsn tuichu]# help return return: return [n] 從一個 shell 函數返回。 使一個函數或者被引用的腳本以指定的返回值 N 退出。 若是 N 被省略,則返回狀態就是 函數或腳本中的最後一個執行的命令的狀態。 退出狀態: 返回 N,或者若是 shell 不在執行一個函數或引用腳本時,失敗。
一般在開發Shell腳本時,定義變量採用的形式爲「a=l;b=2;C=3」,可若是有多個 變量呢?這時再逐個地定義就會很費勁,而且要是有多個不肯定的變量內容,也會難以 進行變量定義,此外,快速讀取不一樣變量的值也是一件很痛苦的事情,因而數組就誕生 了,它就是爲了解決上述問題而出現的。
Shell的數組就是一個元素集合,它把有限個元素(變量或字符內容)用一個名字來 命名,而後用編號對它們進行區分。這個名字就稱爲數組名,用於區分不一樣內容的編 號就稱爲數組下標。組成數組的各個元素(變量)稱爲數組的元素,有時也稱爲下標變量。
數組的定義
# 定義數組 [root@clsn scripts]# stu=(001 002 003) # 打印數組 [root@clsn scripts]# echo ${stu[@]} 001 002 003 # 顯示數組長度 [root@clsn scripts]# echo ${#stu} 3
查: 遍歷數組的內容
# 打印數組內容(經過數組下標或索引) [root@clsn scripts]# echo ${stu[0]} 001 [root@clsn scripts]# echo ${stu[1]} 002 [root@clsn scripts]# echo ${stu[2]} 003 [root@clsn scripts]# echo ${stu[3]} # 遍歷數組 方法一 [root@clsn scripts]# for i in ${stu[@]};do echo $i ;done 001 002 003 # 方法二 [root@clsn scripts]# array=(1 2 3 4 5) [root@clsn scripts]# for i in `eval echo {1..${#array[@]}}`;do echo ${array[i-1]};done 1 2 3 4 5
增:數組添加
[root@clsn scripts]# stu[3]=004 [root@clsn scripts]# echo ${stu[@]} 001 002 003 004
改:數組修改
[root@clsn scripts]# stu[2]=000 [root@clsn scripts]# echo ${stu[@]} 001 002 000 004
刪:數組刪除
[root@clsn scripts]# unset stu[2] [root@clsn scripts]# echo ${#stu[@]} 3 [root@clsn scripts]# echo ${stu[@]} 001 002 004
dir=(`ls`) dir=($(ls))
命令定義數組
[root@clsn scripts]# COM=(`ls`) [root@clsn scripts]# echo ${COM[@]} bianliang.sh case cfb.sh chanshu.sh check check_url.sh
clsn.sh clsn_test.sh daojishi.sh ddos_check.sh echo.sh for for2 fruits.sh
function fz.sh html jingdutiao.sh jishuanqi2.sh jishuanqi.sh lamp.sh lnmp.sh
memcache_check.sh menu.sh nginx.sh panduan panduan1 play quanju.sh rsync_check.sh
rsyncd system6 tuichu web_check.sh web_view.sh while xiugaizhuji.sh yhk.sh yunshuan.sh
zhuajiu.sh
[root@clsn scripts]# a=(1 2 3 ) [root@clsn scripts]# b=(1 > 2 > 3 > 4 > ) [root@clsn scripts]# echo ${a[@]} 1 2 3 [root@clsn scripts]# echo ${b[@]} 1 2 3 4
在shell 經常使用的功能是查
array=(valuel value2 value3 ...)
打印數組格式
${array[@]} 全部元素 ${#array[@]} 數組長度 ${array[i]} 單個元素,i是下標
要求:
一、使用shell數組方法實現,檢測策略儘可能模擬用戶訪問。
二、每10秒鐘作一次全部的檢測,沒法訪問的輸出報警。
三、待檢測的地址以下
http://www.cnblogs.com/clsn/
http://blog.znix.top
http://blog.nmtui.com
http://10.0.0.7
腳本內容:
shell一個很是重要的特性是它可做爲一種編程語言來使用。由於shell是一個解釋器,因此它不能對爲它編寫的程序進行編譯,而是在每次從磁盤加載這些程序時對它們進行解釋。而程序的加載和解釋都是很是耗時的。
針對此問題,許多shell(如BourneAgainShell)都包含shell函數,shell把這些函數放在內存中,這樣每次須要執行它們時就沒必要再從磁盤讀入。shell還以一種內部格式來存放這些函數,這樣就沒必要耗費大量的時間來解釋它們。
函數的做用就是把程序裏屢次調用相同代碼的部分定義成一份,而後起個名字,全部的調用都 只用這名字就能夠了,修改代碼時,只須要改變函數體內的代碼便可。
🐟 把相同的程序段定義成函數,能夠減小代碼量。
🐟 增長程序的可讀、易讀性
🐟 實現程序功能的模塊化
function clsn(){ echo "http://blog.znix.top" } znix(){ echo "http://www.znix.top " }
說明:
一、能夠帶function clsn() 定義,也能夠直接clsn() 定義,不帶任何參數。
二、參數返回,能夠顯示加:return 返回,若是不加,將以最後一條命令運行結果,做爲返回值。 return後跟數值n(0-255)
三、執行函數就是將函數名放到定義的函數以後便可
將函數加載到當前窗口執行:
[root@clsn function]# . fun1.sh [root@clsn function]# zn znew znix [root@clsn function]# znix test [root@clsn function]# clsn http://blog.znix.top
腳本內容
[root@clsn function]# cat fun2.sh #!/bin/bash ############################################################# # File Name: fun2.sh # Version: V1.0 # Author: clsn # Organization: http://blog.znix.top # Created Time : 2017-12-11 19:25:56 # Description: ############################################################# Fun_File=/server/scripts/function/fun1.sh [ -f $Fun_File ] && . $Fun_File clsn
腳本執行結果
[root@clsn function]# sh fun2.sh http://blog.znix.top
腳本內容:
[root@clsn function]# cat fun3.sh #!/bin/bash ############################################################# # File Name: fun3.sh # Version: V1.0 # Author: clsn # Organization: http://blog.znix.top # Created Time : 2017-12-12 09:38:48 # Description: ############################################################# function clsn(){ echo "Hi " } CLSN(){ echo "Hello " echo $0 echo $1 echo $2 } clsn CLSN xi xi
腳本執行結果
[root@clsn function]# sh fun3.sh Hi Hello fun3.sh xi xi
function clsn(){ echo "http://blog.znix.top $1 $2" echo $0 } znix(){ echo "test" } clsn $1 $2
執行結果
[root@clsn function]# sh fun1.sh http://blog.znix.top fun1.sh
腳本內容:
[root@clsn function]# cat fun3.sh #!/bin/bash ############################################################# # File Name: fun3.sh # Version: V1.0 # Author: clsn # Organization: http://blog.znix.top # Created Time : 2017-12-12 09:38:48 # Description: ############################################################# function clsn(){ echo "Hi " } CLSN(){ echo "Hello " echo $0 echo $1 echo $2 return 4 echo "xxixiixxiix" } clsn CLSN xi xi echo $?
腳本執行結果
[root@clsn function]# sh fun3.sh Hi Hello fun3.sh xi xi 4
return命令說明:
[root@clsn function]# help return return: return [n] 從一個 shell 函數返回。 使一個函數或者被引用的腳本以指定的返回值 N 退出。 若是 N 被省略,則返回狀態就是 函數或腳本中的最後一個執行的命令的狀態。 退出狀態: 返回 N,或者若是 shell 不在執行一個函數或引用腳本時,失敗。
basename命令
取出文件名稱
[root@clsn function]# basename /server/scripts/zhuajiu.sh zhuajiu.sh [root@clsn function]# basename /server/scripts/zhuajiu.sh .sh zhuajiu
$$ 參數
取出腳本運行時的pid, 腳本運行的當前進程ID號
[root@clsn function]# echo $$ 37208 [root@clsn function]# ps -ef |grep 37208 root 37208 37206 0 08:39 pts/0 00:00:00 -bash root 38578 37208 1 10:33 pts/0 00:00:00 ps -ef root 38579 37208 0 10:33 pts/0 00:00:00 grep --color=auto 37208
引用自定義函數庫示例:
[root@clsn function]# head -22 fun3.sh #!/bin/bash ############################################################# # File Name: fun3.sh # Version: V1.0 # Author: clsn # Organization: http://blog.znix.top # Created Time : 2017-12-12 09:38:48 # Description: ############################################################# . /server/scripts/userfun.sh scripts_init i=1 while ((i<=10)) do uptime ((i++)) sleep 1 done closeout
調試技巧1:使用dos2unix處理腳本
從windows編輯的腳本到Linux下須要使用這個命令 dos2unix windows.sh
調試技巧2:使用echo命令調試
在變量讀取或修改的先後加入echo $變量,也可在後面使用exit退出腳本,這樣能夠不用註釋後面的代碼
調試技巧3:sh -x 腳本 ==》全局調試
sh -x scripts.sh
調試技巧4:局部調試
set -x 要調試的腳本內容 set +x
①要記得首先用dos2unix對腳本格式化。
②直接執行腳本根據報錯來調試,有時報錯不許確。
③sh -x調試整個腳本,顯示執行過程。
④set -x和set +x調試部分腳本(在腳本中設置)
⑤echo輸出變量及相關內容,而後緊跟着exit退出,不執行後面程序的方式,一步步跟蹤腳本,對於邏輯錯誤比較好用。
寫法: echo $var;exit
⑥最關鍵的是語法熟練、編碼習慣、編程思想,將錯誤扼殺在萌芽之中,減輕調試負擔,提升效率。
須要使用shell編寫一個抓鬮的程序:
要求:
一、執行腳本後,輸入英文名字全拼,產生隨機數01-99之間的數字,數字越大評分就去高,前面已經抓到的數字,下次不能在出現相同數字。
二、第一個輸入名字後,屏幕輸出信息,並將名字和數字記錄到文件裏,程序不能退出繼續等待別人輸入。
腳本內容:
腳本執行過程
腳本內容:
執行結果
[root@clsn scripts]# sh cfb.sh 1 x 1 = 1 2 x 1 = 2 2 x 2 = 4 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81
寫一個Shell腳本解決DOS攻擊生產案例。
請根據web日誌或者或者網絡鏈接數,監控當某個IP併發鏈接數或者短時內PV達到100(讀者根據實際狀況設定),即調用防火牆命令封掉對應的IP。防火牆命令爲:iptables-I INPUT -s IP地址 -j DROP。
練習使用日誌下載地址:https://files.cnblogs.com/files/clsn/access-web-log.zip
腳本內容:
腳本執行結果:
[root@clsn while]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination DROP all -- 112.64.171.98 anywhere DROP all -- 58.220.223.62 anywhere Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
其餘方法進行日誌分析
elk日誌分析
http://blog.oldboyedu.com/elk/
nginx-WAF
http://blog.oldboyedu.com/nginx-waf/
監控web站點目錄(/var/html/www)下全部文件是否被惡意篡改(文件內容被改了),若是有就打印改動的文件名(發郵件),定時任務每3分鐘執行一次。
IDS是英文「Intrusion Detection Systems」的縮寫,中文意思是「入侵檢測系統」。
腳本內容
第一個里程碑:生成md5驗證文件
文件的校驗文件
find /var/html/www/* -type f |xargs md5sum >/tmp/check/web_file_check.md5sum
目錄的校驗文件
tree /var/html/www/ -d >/tmp/check/web_dir_check.txt md5sum /tmp/check/web_dir_check.txt md5sum /tmp/check/web_dir_check.txt >/tmp/check/web_dir_check.md5sum
腳本內容
修改文件目錄後執行結果
用shell處理如下內容
一、按單詞出現頻率降序排序!
二、按字母出現頻率降序排序!
the squid project provides a number ofresources to assist users design implement and support squid installations.Please browse the documentation and support sections for more infomation byoldboy training
腳本內容:
[root@clsn play]# cat abc.sh #!/bin/bash ############################################################# # File Name: abc.sh # Version: V1.0 # Author: clsn # Organization: http://blog.znix.top # Created Time : 2017-12-11 17:23:19 # Description: ############################################################# a="the squid project provides a number ofresources to assist users design implement and support squid installations.Please browse the documentation and support sections for more infomation byoldboy training" echo "按單詞出現頻率降序排序!" for i in $a do echo $i done|\ sort |uniq -c|sort -nk1 -r echo "按字母出現頻率降序排序!" echo $a |grep -o "[a-z]" |sort|uniq -c |sort -nk1 -r
請用shell或Python編寫一個正(或長)方形,接收用戶輸入的數字。
shell腳本內容
[root@clsn play]# cat zhengfangxing.sh #!/bin/bash ############################################################# # File Name: zhengfangxing.sh # Version: V1.0 # Author: clsn # Organization: http://blog.znix.top # Created Time : 2017-12-11 17:33:33 # Description: ############################################################# trap "echo 輸入exit退出" 2 while true do read -p "你想看多大的正方形:" a [ "$a" == "exit" ] && exit expr 1 + $a &>/dev/null [ $? -ne 0 ] && echo "請輸入一個數字!" && exit 2 b="■ " d=$(for i in `eval echo {1..$a}`;do echo -n $b; echo -n " ";done) for i in `eval echo {1..$a}` do echo "$d" done done
腳本執行效果
[root@clsn play]# sh zhengfangxing.sh 4 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ [root@clsn play]# sh zhengfangxing.sh 3 ■ ■ ■ ■ ■ ■ ■ ■ ■
🥇 while循環的特長是執行守護進程以及咱們但願循環不退出持續執行,用於頻率小於1分鐘循環處理(crond),其餘的while循環幾乎均可以被for循環替代。
🥇 case語句能夠被if語句替換,通常在系統啓動腳本傳入少許固定規則字符串用case語句,其餘普通判斷多用if。
🥇 一句話,if,for語句最經常使用,其次while(守護進程),case(服務啓動腳本)。
[root@clsn scripts]# column --help 用法: column [選項] [文件 ...] 選項: -c, --columns <寬度> 輸出寬度(字符數) -t, --table 建立表格 -s, --separator <字符串> 可用的表格分隔符 -o, --output-separator <字符串> 表格輸出列分隔符,默認爲兩個空格 -x, --fillrows 先填充行,再填充列 -h, --help 顯示此幫助並退出 -V, --version 輸出版本信息並退出 更多信息請參閱 column(1)。
經過進程找到pid號碼
[root@clsn proc]# ps -ef |grep sshd root 1294 1 0 09:18 ? 00:00:00 /usr/sbin/sshd -D
經過pid找到在proc目錄下的臨時文件夾
[root@clsn proc]# cd /proc/1294 [root@clsn 1294]#
查看其中的exe文件,便可發現程序文件的路徑
[root@clsn 1294]# ll exe lrwxrwxrwx 1 root root 0 12月 11 09:18 exe -> /usr/sbin/sshd
用操做,更多詳情參照 http://www.cnblogs.com/clsn/p/8022625.html
ctrl +a +d 退出當前終端,返回加載screen前的shell命令狀態 screen -ls 可看screen會話 screen -r id 指定進入哪一個screen會話
screen進程原理
[root@clsn ~]# ps -ef |grep ping root 30170 30153 0 11:57 pts/5 00:00:00 ping 10.0.0.254 root 30172 30078 0 11:58 pts/0 00:00:00 grep --color=auto ping [root@clsn ~]# ps -ef |grep 30170 root 30170 30153 0 11:57 pts/5 00:00:00 ping 10.0.0.254 root 30174 30078 0 11:59 pts/0 00:00:00 grep --color=auto 30170 [root@clsn ~]# ps -ef |grep 30153 root 30153 30119 0 11:57 pts/5 00:00:00 /bin/bash root 30170 30153 0 11:57 pts/5 00:00:00 ping 10.0.0.254 root 30176 30078 0 11:59 pts/0 00:00:00 grep --color=auto 30153 [root@clsn ~]# ps -ef |grep 30119 root 30119 1 0 11:56 ? 00:00:00 SCREEN root 30120 30119 0 11:56 pts/4 00:00:00 /bin/bash root 30153 30119 0 11:57 pts/5 00:00:00 /bin/bash root 30178 30078 0 11:59 pts/0 00:00:00 grep --color=auto 30119
腳本內容
[root@clsn scripts]# cat daojishi.sh #!/bin/bash ############################################################# # File Name: daojishi.sh # Version: V1.0 # Author: clsn # Organization: http://blog.znix.top # Created Time : 2017-12-12 08:49:11 # Description: ############################################################# for i in `seq -w 10 -1 1` do echo -ne "\b\b$i"; sleep 1; done
腳本執行效果
信號 |
說明 |
HUP(l) |
掛起,一般因終端掉線或用戶退出而引起 |
INT(2) |
中斷,一般因按下Ctrl+c組合鍵而引起 |
QUIT(3) |
退出,一般因按下Ctrl+\組合鍵而引起 |
ABRT(6) |
停止,一般因某些嚴重的執行錯誤而引起 |
ALRM(14) |
報警,一般用來處理超時 |
TERM(15) |
終止,一般在系統關機時發送 |
TSTP(20) |
中止進程的運行,但該信號能夠被處理和忽略,一般因按下Ctrl+z組合鍵而引起 |
使用trap控制信號一般須要忽略的信號包括HUP、INT、QUIT、TSTP、TERM等,對應的信號編號分別爲一、二、三、20、15。Shell腳本中既能夠用數字來表明信號,也可使用信號的名字來表明信號
1.12.2使用trap控制信號
trap命令用於指定在接收到信號後將要採起的行動,信號的相關說明前面已經提到 過。trap命令的一種常見用途是在腳本程序被中斷時完成清理工做,或者屏蔽用戶非法 使用的某些信號。在使用信號名時須要省略SIG前綴。能夠在命令提示符下輸人命令 trap -1來查看信號的編號及其關聯的名稱。
trap命令的參數分爲兩部分,前一部分是接收到指定信號時將要採起的行動,後一部分是要處理的信號名。
trap命令的使用語法以下:
trap command signal
signal是指接收到的信號,command是指接收到該信號應採起的行動。也就是:
trap ‘命令;命令’ 信號編號 或 trap ‘命令;命令’ 信號名
[root@clsn ~]# trap 'echo clsn' 2 [root@clsn ~]# ^Cclsn
http://blog.csdn.net/zhangna20151015/article/details/50293987 https://zh.wikipedia.org/wiki/For迴圈#傳統的_for_迴圈_for-loops https://zh.wikipedia.org/wiki/While迴圈 https://www.cnblogs.com/kerrycode/p/6537175.html (生產隨機數) http://blog.51cto.com/lidao/1936495 (不循環,批量建立用戶) http://www.runoob.com/linux/linux-shell-func.html 函數