注:初學shell,以下為本人自己寫的答案,如果有更好的,請指教!
1. 求2個數之和:
2. 計算1-100的和
3. 將一目錄下所有的文件的擴展名改為bak
4.編譯並執行當前目錄下的所有.c文件
5.打印本機的交換分區大小,處理結果: Swap:1024M
6. 文本分析,取出/etc/password中shell出現的次數
第一種方法結果:
4 /bin/bash
1 /sbin/halt
2 /sbin/nologin
7. 文件整理,employee文件中記錄了工號和姓名
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
bonus文件中記錄工號和工資:
100 $5,000
200 $500
300 $3,000
400 $1,250
要求把兩個文件合並並輸出如下,處理結果:
400 ashok sharma $1,250
100 jason smith $5,000
200 john doe $500
300 sanjay gupta $3,000
sort employee.txt sort salary.txt join employee.txt salary.txt | sort -k2
8. 寫一個shell腳本來得到當前的日期,時間,用戶名和當前工作目錄。
1 #!/bin/bash 2 echo "the present date is : `date` " 3 echo "the present user is : `whoami`" 4 echo "the present dir is : `pwd`"
9. 編寫shell腳本獲取本機的網絡地址。
1 #!/bin/bash 2 echo "IP=$(ifconfig eth3| sed -n '/inet addr:/p'|awk '{print $2}'|awk -F: '{print $2}')"
10. 編寫個shell腳本將當前目錄下大於10K的文件轉移到/tmp目錄下
1 #!/bin/bash 2 for file in $(ls -l | awk '$5 >100 {print $9}') 3 do 4 mv $file /home/chenjj/shell/temp 5 done
11. 編寫一個名為myfirstshell.sh的腳本,它包括以下內容。
a) 包含一段注釋,列出您的姓名、腳本的名稱和編寫這個腳本的目的。
b) 問候用戶。
c) 顯示日期和時間。
d) 顯示這個月的日歷。
e) 顯示您的機器名。
f) 顯示當前這個操作系統的名稱和版本。
g) 顯示父目錄中的所有文件的列表。
h) 顯示root正在運行的所有進程。
i) 顯示變量TERM、PATH和HOME的值。
j) 顯示磁盤使用情況。
k) 用id命令打印出您的組ID。
m) 跟用戶說“Good bye”
1 #!/bin/bash 2 echo "hello!" 3 echo "Today is : $(date)" 4 #echo $(date) 5 echo "this month is :" 6 cal 7 echo "the hostname is : $(hostname)" 8 echo "the name of OS is $(uname -r) and the verion is $(uname -r) " 9 echo "the files are :$(ls -l ../)" 10 echo "the running process of root is $(ps -u root)" 11 echo "TERM=$TERM" 12 echo "PATH=$PATH" 13 echo "HOME=$HOME" 14 echo "the use of disk is :$(df -lh)" 15 echo "the id of the group is $(id -g)" 16 echo "Goodbye Sir!"
12. 文件移動拷貝,有m1.txt m2.txt m3.txt m4.txt,分別創建出對應的目錄,m1 m2 m3 m4 並把文件移動到對應的目錄下
1 #!/bin/bash 2 touch m1.txt m2.txt m3.txt m4.txt 3 for((i=1;i<=4;i++)) 4 do 5 mkdir m$i 6 mv m$i.txt m$i 7 done
13. 終端輸入一個文件名,判斷是否是設備文件
1 #!/bin/bash 2 #coding=utf-8 3 echo "please input a filename:" 4 read fileName 5 if [ -c $fileName -o -b $fileName ] 6 then 7 echo "$fileName 是設備文件!" 8 else 9 echo "$fileName 不是設備文件!" 10 fi
14. 統計IP訪問:要求分析apache訪問日志,找出訪問頁面數量在前100位的IP數。日志大小在78M左右。以下是apache的訪問日志節選:
202.101.129.218 - - [26/Mar/2006:23:59:55 +0800] "GET /online/stat_inst.php?pid=d065 HTTP/1.1" 302 20-"-" "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
關於apache日志的了解請見https://blog.csdn.net/xingxiupaioxue/article/details/70153166
日志記錄的第六項信息是狀態代碼。它告訴我們請求是否成功,或者遇到了什么樣的錯誤。大多數時候,這項值是200,它表示服務器已經成功地響應瀏覽器的請求,一切正常。此處不准備給出狀態代碼的完整清單以及解釋它們的含義,請參考相關資料了解這方面的信息。但一般地說,以2開頭的狀態代碼表示成功,以3開頭的狀態代碼表示由於各種不同的原因用戶請求被重定向到了其他位置,以4開頭的狀態代碼表示客戶端存在某種錯誤,以5開頭的狀態代碼表示服務器遇到了某個錯誤。
1 #!/bin/bash 2 awk '{print $1}' $1 |sort |uniq -c |sort -k1nr | head -n 100
執行腳本:source apache.sh apache(文件)
15. 設計一個Shell程序,在/userdata目錄下建立50個目錄,即user1~user50,並設置每個目錄的權限,其中其他用戶的權限為:讀;文件所有者的權限為:讀、寫、執行;文件所有者所在組的權限為:讀、執行。
1 #!/bin/bash 2 cd /Userdata 3 for ((i=1;i<=50;i++)) 4 do 5 mkdir user$i 6 chmod 754 user$i 7 done
16. 設計一個shell程序,添加一個新組為class1,然后添加屬於這個組的30個用戶,用戶名的形式為stdxx,其中xx從01到30,並設置密碼為對應的stdxx。
1 #!/bin/bash 2 #coding=utf-8 3 #設計一個shell程序,添加一個新組為class1,然后添加屬於這個組的30個用戶,用戶名的形式為stdxx,其中xx從01到30,並設置密碼為對應的stdxx。 4 groupadd class1 5 i=1 6 while [ $i -le 30 ] 7 do 8 if [ $i -lt 10 ] 9 then 10 useradd -g class1 std0$i 11 passwd std0$i std0$i 12 else 13 useradd -g class1 std$i 14 passwd std$i std$i 15 fi 16 let "i++" 17 done
17.編寫shell程序,實現自動刪除30個賬號的功能。賬號名為std01至std30。
1 #!/bin/bash 2 i=1 3 while [ $i -le 30 ] 4 do 5 if [ $i -lt 10 ] 6 then 7 userdel std0$i 8 else 9 userdel std$i 10 fi 11 let "i++" 12 done
18. 用戶清理,清除本機除了當前登陸用戶以外的所有用戶
kill $(who -u | grep -v `whoami`| awk '{print $6}'|sort -u )
19.寫腳本實現,可以用shell、perl等。在目錄/tmp下找到100個以abc開頭的文件,然后把這些文件的第一行保存到文件new中。
1 #!/bin/bash 2 #coding=utf-8 3 #在目錄/tmp下找到100個以abc開頭的文件,然后把這些文件的第一行保存到文件new中。 4 files=`find /tmp -type f -name "abc*" | head -n 100` 5 for file in $files 6 do 7 sed -n '1p' $file >> new 8 done
20. 把文件b中有的,但是文件a中沒有的所有行,保存為文件c,並統計c的行數。
21. 查看TCP連接狀態
netstat -ant | awk '{print $6}'|sort |uniq -c|sort -k1nr
22. 查找請求數為20個IP(常用於查找攻來源)
netstat -atn | grep 80 |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c |sort
23. 查找較多time_wait連接
netstat -n | grep 'TIME_WAIT' | awk '{print $5}'|sort | uniq -c |sort -k1nr
24. 獲得訪問前10位的ip地址,access.log日志記錄
61.155.149.20 - - [13/Jan/2017:15:42:47 +0800] "GET /category/db/ HTTP/1.1" 200 23225
cat access.log | awk '{print $1}' | sort |uniq -c |sort -nr | head -n10
Linux常用:
1. 查看服務器空間:
df -h
2. 查看當前目錄下哪個文件占用最大:
du -h --max-depth=1
3. 查看指定目錄下文件大小:
du -sh ${dir} du -sh * # 查看當前目錄下各文件及文件夾占用大小
4. 查看服務器端口是否被占用:
lsof -i:8081
5.查看服務器所有端口:
netstat -ntlp