天天看點

編寫腳本實用工具

1、檢視哪個檔案占用最大

檢視前十名磁盤空間使用者,到第11行,sed會删除清單的剩餘部分,然後給清單中每行一個行号。要讓行号和磁盤空間文本

位于同一行,用N指令将文本行合并在一行。然後用gawk指令清理,在行号後,加一個冒号(:),還給每行文本的輸出行中的每個字段放了一個制表符。這樣就生成了一個格式精緻的前十名

磁盤空間使用者清單了

1

2

3

4

5

6

7

8

9

10

11

<code>[root@digitcube-test1 qingyun]# du -Sh /home/*| sort -rn | sed </code><code>'{11,$D;=}'</code> <code>| sed </code><code>'N;s/\n/ /'</code> <code>| gawk </code><code>'{print $1":""\t" $2"\t" $3"\n"}'</code>

<code>1</code><code>: 1020K /home/nexus/sonatype-work/nexus/storage/central/org/springframework/spring-context/</code><code>2.5</code><code>.</code><code>6</code>

<code>2</code><code>: 1020K /home/nexus/sonatype-work/nexus/storage/central/ant/ant/</code><code>1.6</code><code>.</code><code>5</code>

<code>3</code><code>: 1012K /home/nexus/sonatype-work/nexus/storage/central/org/springframework/spring-beans/</code><code>2.5</code><code>.</code><code>6</code>

<code>4</code><code>: 1012K /home/maven/.m2/repository/org/xerial/snappy/snappy-java/</code><code>1.0</code><code>.</code><code>4.1</code>

<code>5</code><code>: 1008K /home/home/hadoop/jstorm/dc_topology/tmp/org/apache/hadoop/hdfs/server/namenode</code>

<code>6</code><code>: 1008K /home/home/hadoop/hadoop-</code><code>1.0</code><code>.</code><code>4</code><code>/docs/api/org/apache/hadoop/mapreduce</code>

<code>7</code><code>: 1008K /home/hadoop/sam/datatask/doubixiyou_1290</code>

<code>8</code><code>: 1008K /home/hadoop/hadoop-</code><code>1.0</code><code>.</code><code>4</code><code>/docs/api/org/apache/hadoop/mapreduce</code>

<code>9</code><code>: 1004K /home/home/hadoop/jstorm/dc_topology/tmp/kafka/log</code>

<code>10</code><code>: 1000K /home/maven/.m2/repository/org/xerial/snappy/snappy-java/</code><code>1.0</code><code>.</code><code>3.2</code>

2、創造加了日期的前十名磁盤空間使用者報告的腳本

<code> </code><code>&lt;br&gt;</code>

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<code>[root@digitcube-test1 tmp]</code><code># vim file_siz.sh</code>

<code>#!/bin/bash</code>

<code>#Big_User - find big disk space users in various direcotries</code>

<code>#Parameters for Script</code>

<code>#</code>

<code>CHECK_DIRECTORIES=</code><code>"/var/log /home"</code> <code>#direcotries to check</code>

<code>######################Main Script###########################</code>

<code>DATE=`</code><code>date</code> <code>+%m%d%y`               </code><code>#Date for report file</code>

<code>exec</code> <code>&gt; space_file_$DATA.rpt</code>

<code>echo</code> <code>"Top Ten Disk Space Usage"</code>   <code>#Report header for whole report</code>

<code>echo</code> <code>"for $CHECK_DIRECTORIES Direcotries"</code>

<code>for</code> <code>DIR_CHECK </code><code>in</code> <code>$CHECK_DIRECTORIES </code><code>#loop to du directories</code>

<code>do</code>

<code>       </code><code>echo</code> <code>""</code>

<code>       </code><code>echo</code> <code>"The $DID_CHECK Directory:"</code> <code>#Title header for each direcotry</code>

<code>#Create a listing of top ten disk space users</code>

<code>       </code><code>du</code> <code>-S $DIR_CHECK 2&gt;</code><code>/dev/null</code><code>|</code><code>sort</code> <code>-rn|</code><code>sed</code> <code>'{11,$D;=}'</code><code>|</code><code>sed</code> <code>'N;s/\n/ /'</code><code>|</code><code>gawk</code> <code>'{printf $1":""\t" $2"\t" $3"\n"}'</code>

<code>done</code>

<code>exec</code> <code>&gt; </code><code>/tmp/test</code><code>.txt</code>

2、建立按日期歸檔的腳本

歸檔檔案,讓腳本讀取file_to_backup裡面每個目錄或檔案,用到一個簡單read指令,來讀取該檔案中的每一條記錄。

exec&lt;$CONFIG_FILE

read FILE_NAME

為歸檔配置檔案以及從file_to_backup讀取每條記錄都用了變量.隻要read指令在配置檔案中發現還有記錄要讀,它就會在?變量中傳回一退出狀态碼0表示成功,以while循環的測試條件來讀取file_to_backup的所有記錄

while [ $? -eq 0 ]

do

....

done

一旦read指令到了末尾,傳回一個非0狀态碼,腳本會退出while循環

<code>[root@digitcube-test1 tmp]# cat /home/qingyun/file_to_backup </code>

<code>/home/qingyun/test1</code>

<code>/home/qingyun/test2</code>

<code>/home/qingyun/test3</code>

<code>/home/qingyun/love</code>

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

<code>[root@digitcube-test1 tmp]# vim Daily_Archive.sh </code>

<code>#Set Configuration and Destination File</code>

<code>CONFIG_FILE=/home/qingyun/file_to_backup</code>

<code>DESTINATION=/home/qingyun/$FILE</code>

<code>##############Main Script######################</code>

<code>#Check Backup Config file exists</code>

<code>if</code> <code>[ -f $CONFIG_FILE ] #Make sure the config file still exits</code>

<code>then</code>

<code>        </code><code>echo </code>

<code>else</code>

<code>        </code><code>echo</code>

<code>        </code><code>echo </code><code>"$CONFIG_FILE does not exist"</code>

<code>        </code><code>echo </code><code>"Backup not completed due to  missting Configuration file"</code>

<code>        </code><code>exit</code>

<code>fi</code>

<code>#Build the name of all the files to backup</code>

<code>FILE_NO=</code><code>1</code>       <code>#Start on line </code><code>1</code> <code>of Config File</code>

<code>exec &lt; $CONFIG_FILE     #Redirect Std Input to name of Config File</code>

<code>read FILE_NAME  #Read 1st record</code>

<code>while</code> <code>[ $? -eq </code><code>0</code> <code>]      #Create list of files to backup</code>

<code>        </code><code>#Make sure the file or directory exists</code>

<code>        </code><code>if</code> <code>[ -f $FILE_NAME ]</code>

<code>        </code><code>then</code>

<code>                </code><code>#If file exists.add its name to the list</code>

<code>                </code><code>echo $FILE_NAME</code>

<code>                </code><code>FILE_LIST=</code><code>"$FILE_LIST $FILE_NAME"</code>

<code>        </code><code>else</code>

<code>                </code><code>#If file doesn't exist.issue warning</code>

<code>                </code><code>echo</code>

<code>                </code><code>echo </code><code>"$FILE_NAME,does not exist"</code>

<code>                </code><code>echo </code><code>"Obviously,I will not include i in this archive"</code>

<code>                </code><code>echo </code><code>"It is listed on line $FILE_NO of the config file."</code>

<code>                </code><code>echo </code><code>"Continuing to build archive list...."</code>

<code>        </code><code>fi</code>

<code>        </code><code>FILE_NO=$[$FILE_NO + </code><code>1</code><code>] #Increase Line /File number by on</code>

<code>        </code><code>read FILE_NAME          #Read next record</code>

<code>############################################################</code>

<code>#Backup the files and Compress Archive</code>

<code>tar -czf $DESTINATION $FILE_LIST </code><code>2</code><code>&gt;/dev/</code><code>null</code>

按小時歸檔的腳本

歸檔目錄包含了跟一年中的各個月份對應的目錄,将月的序号作為目錄名。而每月的目錄中又包含跟一個月中的各天對應的目錄(用天序号來作為目錄)。這樣隻用給每個歸檔檔案加時間戳然後将它他們放到跟日和月份對應的目錄就行了。

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

<code>[root@digitcube-test1 tmp]# vim Hourly_Archive.sh </code>

<code>#Hourly_Archive - Every hour create an arhive</code>

<code>##############################################</code>

<code>#Set Configureation File</code>

<code>CONFIG_FILE=/home/qingyun/hourly/file_to_backup</code>

<code>#Set Base Archive Destination Location</code>

<code>BASEDEST=/home/qingyun/hourly</code>

<code>#Gather Current Day.Month &amp; Time</code>

<code>DAY=`date +%d`</code>

<code>MONTH=`date +%m`</code>

<code>TIME=`date +%k%M`</code>

<code>#Create Archive Destination Directory</code>

<code>mkdir -p $BASEDEST/$MONTH/$DAY</code>

<code>DESTINATION=$BASEDEST/$MONTH/$DAY/archive.$TIME.tar.gz</code>

<code>#Build Archvie Destination file Name</code>

<code>###############MAIN Script#####################################</code>

3、管理使用者賬号

腳本進入删除使用者4個步聚:

1、獲得并确認使用者賬戶名,

2、查找和終止使用者的程序,

3、建立一份屬于該使用者賬号的所有檔案報告,

4、最終删除使用者賬号

用到判斷參數

-z:字元長度0,為真

-n:字元長度非0,為真

unset:删除變量和函數

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

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

<code>[root@logicserver tmp]# vim Delte_user.sh</code>

<code>#Delte_User - Automates the  </code><code>4</code> <code>step to remove an account</code>

<code>#Defin Functions</code>

<code>##########################################################</code>

<code>function</code> <code>get_answer {</code>

<code>unset ANSWER</code>

<code>ASK_COUNT=</code><code>0</code>

<code>while</code> <code>[ -z </code><code>"$ANSWER"</code> <code>] # </code><code>while</code> <code>no anwser </code><code>is</code> <code>given.keeip asking</code>

<code>        </code><code>ASK_COUNT=$[ $ASK_COUNT + </code><code>1</code> <code>]</code>

<code>        </code><code>case</code> <code>$ASK_COUNT </code><code>in</code>      <code>#If user gives no answer </code><code>in</code> <code>time allotted</code>

<code>        </code><code>2</code><code>)</code>

<code>                </code><code>echo </code><code>"Please answer the question"</code>

<code>                </code><code>;;</code>

<code>        </code><code>3</code><code>)</code>

<code>                </code><code>echo </code><code>"One last try.....please answer the question."</code>

<code>        </code><code>4</code><code>)</code>

<code>                </code><code>echo </code><code>"Since you refuse to answer the question.."</code>

<code>                </code><code>echo </code><code>"exiting program."</code>

<code>                </code><code>#</code>

<code>                </code><code>exit</code>

<code>        </code><code>esac</code>

<code>        </code><code>if</code> <code>[ -n </code><code>"$LINE2"</code> <code>]</code>

<code>        </code><code>then            #print </code><code>2</code> <code>lines</code>

<code>                </code><code>echo $LINE1</code>

<code>                </code><code>echo -e $LINE2</code><code>" \c"</code>

<code>                </code><code>echo -e $LINE1</code><code>"\c"</code>

<code>#       Allow </code><code>60</code> <code>second to answer befor time-out</code>

<code>        </code><code>read -t </code><code>60</code> <code>ANSWER</code>

<code>#Do a littel </code><code>var</code><code>iable clean-up</code>

<code>unset LINE1</code>

<code>unset LINE2</code>

<code>}       #End of get_answer </code><code>function</code>

<code>#####################################################################</code>

<code>function</code> <code>process_answer {</code>

<code>case</code> <code>$ANSWER </code><code>in</code>

<code>y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)</code>

<code>#If user answer </code><code>"yes"</code><code>.</code><code>do</code> <code>noting</code>

<code>        </code><code>;;</code>

<code>*)</code>

<code>#If user answer anything but </code><code>"yes"</code><code>.exit script</code>

<code>        </code><code>echo $EXIT_LINE1</code>

<code>        </code><code>echo $EXIT_LINE2</code>

<code>esac</code>

<code>#Do a little </code><code>var</code><code>iable clean-up</code>

<code>unset EXIT_LINE1</code>

<code>unset EXIT_LINE2</code>

<code>}       #End of process_answer funtion</code>

<code>###################################################################</code>

<code>#End of Function Definitions</code>

<code>######################Mian Script#################################</code>

<code>#Get name of User Account to check</code>

<code>echo </code><code>"Step $1 - Determin User Account name to Delete "</code>

<code>echo</code>

<code>LINE1=</code><code>"please enter the username of the user "</code>

<code>LINE2=</code><code>"Account you wish to delete from system:"</code>

<code>get_answer</code>

<code>USER_ACCOUNT=$ANSWER</code>

<code>#Double check </code><code>with</code> <code>script user that </code><code>this</code> <code>is</code> <code>the coreect User Account</code>

<code>LINE1=</code><code>"Is $USER_ACCOUNT the user account "</code>

<code>LINE2=</code><code>"You wish to delete from the system?[y/n]"</code>

<code>#Call process_answer funtion:</code>

<code>#       If user answer anything but </code><code>"yes"</code><code>.exit script</code>

<code>EXIT_LINE1=</code><code>"Because the account,$USER_ACCOUNT,is not"</code>

<code>EXIT_LINE2=</code><code>"The one you wish to delete.we are leaving the script..."</code>

<code>process_answer</code>

<code>############################################################################</code>

<code>USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT)</code>

<code>if</code> <code>[ $? -eq </code><code>1</code><code>]          #If the account </code><code>is</code> <code>not found.exit script</code>

<code>        </code><code>echo </code><code>"Account,$USER_ACCOUNT.not found"</code>

<code>        </code><code>echo </code><code>"Leaving the script..."</code>

<code>echo </code><code>"I found this record:"</code>

<code>echo $USER_ACCOUNT_RECORD</code>

<code>LINE1=</code><code>"Is this the correct User Account?[y/n]"</code>

<code>#Call process_answer </code><code>function</code><code>:</code>

<code>#       If user answers anything but </code><code>"yes"</code><code>,exit script</code>

<code>EXIT_LINE2=</code><code>"The one you wish to delete.we are leaving the script...."</code>

<code>#Search </code><code>for</code> <code>any running processes that belong to the User Account</code>

<code>echo </code><code>"Step #2 - Find process on system beloging to user account"</code>

<code>echo </code><code>"$USER_ACCOUNT has the following process running:"</code>

<code>ps -u $USER_ACCOUNT     #List user processes running.</code>

<code>case</code> <code>$? </code><code>in</code>

<code>1</code><code>)      #No processes running </code><code>for</code> <code>this</code> <code>User Account</code>

<code>        </code><code>#</code>

<code>        </code><code>echo </code><code>"There are no processes for this account currently running."</code>

<code>0</code><code>)      #Processes running </code><code>for</code> <code>this</code> <code>User Account.</code>

<code>        </code><code>#Ask Script User </code><code>if</code> <code>wants us to kill the processes.</code>

<code>        </code><code>unset ANSWER</code>

<code>        </code><code>LINE1=</code><code>"Would you like me to kill me process(es)?[y/n]"</code>

<code>        </code><code>get_answer</code>

<code>        </code><code>case</code> <code>$ANSWER </code><code>in</code>

<code>        </code><code>y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)        #If user answers </code><code>"yes"</code>

<code>                                        </code><code>#Kill User Account processes.</code>

<code>                </code><code>#Clean-up temp file upon signals</code>

<code>                </code><code>trap </code><code>"rm $USER_ACCOUNT_Running_Process.rpt"</code> <code>SIGTERM SIGINT SIGQUIT</code>

<code>                </code><code>#List user processes running</code>

<code>                </code><code>ps -u $USER_ACCOUNT &gt; $USER_ACCOUNT_Running_Process.rpt</code>

<code>                </code><code>exec &lt; $USER_ACCOUNT_Running_Process.rpt        #Make report Std Input</code>

<code>                </code><code>read USER_PROCESS_REC   #First record will be blank</code>

<code>                </code><code>read USER_PROCESS_REC</code>

<code>                </code><code>while</code> <code>[ $? -eq </code><code>0</code> <code>]</code>

<code>                </code><code>do</code>

<code>                        </code><code>#obtain PID</code>

<code>                        </code><code>USER_PID=`echo $USER_PROCESS_REC|cut -d</code><code>" "</code> <code>-f1`</code>

<code>                        </code><code>kill -</code><code>9</code> <code>$USER_PID</code>

<code>                        </code><code>echo </code><code>"Killed process $USER_PID"</code>

<code>                        </code><code>read USER_PROCESS_REC</code>

<code>                </code><code>done</code>

<code>                </code><code>rm $USER_ACCOUNT_Running_Process.rpt    #Remove temp report.</code>

<code>        </code><code>*)      #If user answer anything but </code><code>"yes"</code><code>,</code><code>do</code> <code>not kill</code>

<code>                </code><code>echo </code><code>"Will not kill the process(es)"</code>

<code>##########################################################################</code>

<code>#Create a report of all files owned by User Account</code>

<code>echo </code><code>"Step #3 - Find files on system belonging to user account"</code>

<code>echo </code><code>"Creating a report of all files owned by $USER_ACCOUNT."</code>

<code>echo </code><code>"It is recommended that you backup/archive these files."</code>

<code>echo </code><code>"and then do one of two things;"</code>

<code>echo </code><code>" 1)Delete the files"</code>

<code>echo </code><code>" 2) Change the files' ownership to a current user account."</code>

<code>echo </code><code>"Please wait.This may take a while...."</code>

<code>REPORT_DATE=`date +%y%m%d`</code>

<code>REPORT_FILE=$USER_ACCOUNT</code><code>"_files_"</code><code>$REPORT_DATE</code><code>".RPT"</code>

<code>find / -user $USER_ACCOUNT &gt; $REPORT_FILE </code><code>2</code><code>&gt; /dev/</code><code>null</code>

<code>echo </code><code>"Report is commlete."</code>

<code>echo </code><code>"Name of report: $REPORT_FILE"</code>

<code>echo </code><code>"Location of report: `pwd`"</code>

<code>############################################</code>

<code>#Remove User Account</code>

<code>echo </code><code>"Step #4 - Remove user account"</code>

<code>LINE1=</code><code>"Do you wish to remove $USER_ACCOUNT account from system?[y/n]"</code>

<code>#       </code><code>if</code> <code>user answer anythin but </code><code>"yes"</code><code>.exit script</code>

<code>EXIT_LINE1=</code><code>"Since you do not wish to remove the user account."</code>

<code>EXIT_LINE2=</code><code>"$USER_ACCOUNT at this time.exiting the script... "</code>

<code>userdel $USER_ACCOUNT   #</code><code>delete</code> <code>user account</code>

<code>echo </code><code>"User account.$USER_ACCOUNT.has been removed"</code>

本文轉自 zouqingyun 51CTO部落格,原文連結:http://blog.51cto.com/zouqingyun/1696340,如需轉載請自行聯系原作者

下一篇: gawk進階使用