编写脚本实用工具

简介:

1、查看哪个文件占用最大

查看前十名磁盘空间用户,到第11行,sed会删除列表的剩余部分,然后给列表中每行一个行号。要让行号和磁盘空间文本
位于同一行,用N命令将文本行合并在一行。然后用gawk命令清理,在行号后,加一个冒号(:),还给每行文本的输出行中的每个字段放了一个制表符。这样就生成了一个格式精致的前十名
磁盘空间用户列表了

1
2
3
4
5
6
7
8
9
10
11
[root@digitcube-test1 qingyun]# du -Sh /home/*| sort -rn | sed  '{11,$D;=}'  | sed  'N;s/\n/ /'  | gawk  '{print $1":""\t" $2"\t" $3"\n"}'
1 : 1020K /home/nexus/sonatype-work/nexus/storage/central/org/springframework/spring-context/ 2.5 . 6
2 : 1020K /home/nexus/sonatype-work/nexus/storage/central/ant/ant/ 1.6 . 5
3 : 1012K /home/nexus/sonatype-work/nexus/storage/central/org/springframework/spring-beans/ 2.5 . 6
4 : 1012K /home/maven/.m2/repository/org/xerial/snappy/snappy-java/ 1.0 . 4.1
5 : 1008K /home/home/hadoop/jstorm/dc_topology/tmp/org/apache/hadoop/hdfs/server/namenode
6 : 1008K /home/home/hadoop/hadoop- 1.0 . 4 /docs/api/org/apache/hadoop/mapreduce
7 : 1008K /home/hadoop/sam/datatask/doubixiyou_1290
8 : 1008K /home/hadoop/hadoop- 1.0 . 4 /docs/api/org/apache/hadoop/mapreduce
9 : 1004K /home/home/hadoop/jstorm/dc_topology/tmp/kafka/log
10 : 1000K /home/maven/.m2/repository/org/xerial/snappy/snappy-java/ 1.0 . 3.2

 

2、创造加了日期的前十名磁盘空间用户报告的脚本

1
  <br>
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
[root@digitcube-test1 tmp] # vim file_siz.sh
#!/bin/bash
#Big_User - find big disk space users in various direcotries
#Parameters for Script
#
CHECK_DIRECTORIES= "/var/log /home"  #direcotries to check
#
######################Main Script###########################
#
DATE=` date  +%m%d%y`                #Date for report file
exec  > space_file_$DATA.rpt
#
#
echo  "Top Ten Disk Space Usage"    #Report header for whole report
echo  "for $CHECK_DIRECTORIES Direcotries"
#
for  DIR_CHECK  in  $CHECK_DIRECTORIES  #loop to du directories
do
        echo  ""
        echo  "The $DID_CHECK Directory:"  #Title header for each direcotry
#
#Create a listing of top ten disk space users
        du  -S $DIR_CHECK 2> /dev/null | sort  -rn| sed  '{11,$D;=}' | sed  'N;s/\n/ /' | gawk  '{printf $1":""\t" $2"\t" $3"\n"}'
#
done
exec  /tmp/test .txt

 

2、创建按日期归档的脚本

归档文件,让脚本读取file_to_backup里面每个目录或文件,用到一个简单read命令,来读取该文件中的每一条记录。

exec<$CONFIG_FILE

read FILE_NAME

为归档配置文件以及从file_to_backup读取每条记录都用了变量.只要read命令在配置文件中发现还有记录要读,它就会在?变量中返回一退出状态码0表示成功,以while循环的测试条件来读取file_to_backup的所有记录

while [ $? -eq 0 ]

do

....

read FILE_NAME

done

一旦read命令到了末尾,返回一个非0状态码,脚本会退出while循环

1
2
3
4
5
[root@digitcube-test1 tmp]# cat /home/qingyun/file_to_backup 
/home/qingyun/test1
/home/qingyun/test2
/home/qingyun/test3
/home/qingyun/love
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
[root@digitcube-test1 tmp]# vim Daily_Archive.sh 
 
#
#Set Configuration and Destination File
#
CONFIG_FILE=/home/qingyun/file_to_backup
DESTINATION=/home/qingyun/$FILE
#
##############Main Script######################
#
#Check Backup Config file exists
#
if  [ -f $CONFIG_FILE ] #Make sure the config file still exits
then
         echo 
else
         echo
         echo  "$CONFIG_FILE does not exist"
         echo  "Backup not completed due to  missting Configuration file"
         echo
         exit
fi
#
#Build the name of all the files to backup
#
FILE_NO= 1        #Start on line  1  of Config File
exec < $CONFIG_FILE     #Redirect Std Input to name of Config File
#
read FILE_NAME  #Read 1st record
#
while  [ $? -eq  0  ]      #Create list of files to backup
do
         #Make sure the file or directory exists
         if  [ -f $FILE_NAME ]
         then
                 #If file exists.add its name to the list
                 echo $FILE_NAME
                 FILE_LIST= "$FILE_LIST $FILE_NAME"
 
         else
                 #If file doesn't exist.issue warning
                 echo
                 echo  "$FILE_NAME,does not exist"
                 echo  "Obviously,I will not include i in this archive"
                 echo  "It is listed on line $FILE_NO of the config file."
                 echo  "Continuing to build archive list...."
                 echo
         fi
#
         FILE_NO=$[$FILE_NO +  1 ] #Increase Line /File number by on
         read FILE_NAME          #Read next record
done
############################################################
#
#Backup the files and Compress Archive
#
tar -czf $DESTINATION $FILE_LIST  2 >/dev/ null


按小时归档的脚本

归档目录包含了跟一年中的各个月份对应的目录,将月的序号作为目录名。而每月的目录中又包含跟一个月中的各天对应的目录(用天序号来作为目录)。这样只用给每个归档文件加时间戳然后将它他们放到跟日和月份对应的目录就行了。

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
[root@digitcube-test1 tmp]# vim Hourly_Archive.sh 
 
#!/bin/bash
#
 
#Hourly_Archive - Every hour create an arhive
##############################################
#
#Set Configureation File
#
CONFIG_FILE=/home/qingyun/hourly/file_to_backup
#
#Set Base Archive Destination Location
#
BASEDEST=/home/qingyun/hourly
#
#Gather Current Day.Month & Time
#
DAY=`date +%d`
MONTH=`date +%m`
TIME=`date +%k%M`
#
#Create Archive Destination Directory
#
mkdir -p $BASEDEST/$MONTH/$DAY
DESTINATION=$BASEDEST/$MONTH/$DAY/archive.$TIME.tar.gz
#
#Build Archvie Destination file Name
#
###############MAIN Script#####################################
 
#Check Backup Config file exists
#
if  [ -f $CONFIG_FILE ] #Make sure the config file still exits
then
         echo 
else
         echo
         echo  "$CONFIG_FILE does not exist"
         echo  "Backup not completed due to  missting Configuration file"
         echo
         exit
fi
#
#Build the name of all the files to backup
#
FILE_NO= 1        #Start on line  1  of Config File
exec < $CONFIG_FILE     #Redirect Std Input to name of Config File
#
read FILE_NAME  #Read 1st record
#
while  [ $? -eq  0  ]      #Create list of files to backup
do
         #Make sure the file or directory exists
         if  [ -f $FILE_NAME ]
         then
                 #If file exists.add its name to the list
                 echo $FILE_NAME
                 FILE_LIST= "$FILE_LIST $FILE_NAME"
 
         else
                 #If file doesn't exist.issue warning
                 echo
                 echo  "$FILE_NAME,does not exist"
                 echo  "Obviously,I will not include i in this archive"
                 echo  "It is listed on line $FILE_NO of the config file."
                 echo  "Continuing to build archive list...."
                 echo
         fi
#
         FILE_NO=$[$FILE_NO +  1 ] #Increase Line /File number by on
         read FILE_NAME          #Read next record
done
############################################################
#
#Backup the files and Compress Archive
#
tar -czf $DESTINATION $FILE_LIST  2 >/dev/ null


3、管理用户账号

脚本进入删除用户4个步聚:

1、获得并确认用户账户名,

2、查找和终止用户的进程,

3、创建一份属于该用户账号的所有文件报告,

4、最终删除用户账号

用到判断参数

-z:字符长度0,为真

-n:字符长度非0,为真

 

unset:删除变量和函数

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
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
[root@logicserver tmp]# vim Delte_user.sh
#!/bin/bash
#
#Delte_User - Automates the   4  step to remove an account
#
#Defin Functions
#
##########################################################
function  get_answer {
unset ANSWER
ASK_COUNT= 0
#
while  [ -z  "$ANSWER"  ] #  while  no anwser  is  given.keeip asking
do
         ASK_COUNT=$[ $ASK_COUNT +  1  ]
#
         case  $ASK_COUNT  in       #If user gives no answer  in  time allotted
         2 )
                 echo
                 echo  "Please answer the question"
                 echo
                 ;;
         3 )
                 echo
                 echo  "One last try.....please answer the question."
                 echo
                 ;;
         4 )
                 echo
                 echo  "Since you refuse to answer the question.."
                 echo  "exiting program."
                 #
                 exit
                 ;;
         esac
#
         echo
#
         if  [ -n  "$LINE2"  ]
         then            #print  2  lines
                 echo $LINE1
                 echo -e $LINE2 " \c"
         else
                 echo -e $LINE1 "\c"
         fi
#
#       Allow  60  second to answer befor time-out
         read -t  60  ANSWER
done
#Do a littel  var iable clean-up
unset LINE1
unset LINE2
#
}       #End of get_answer  function
#
#####################################################################
function  process_answer {
#
case  $ANSWER  in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
#If user answer  "yes" . do  noting
         ;;
*)
#If user answer anything but  "yes" .exit script
         echo 
         echo $EXIT_LINE1
         echo $EXIT_LINE2
         echo
         exit
         ;;
esac
#
#Do a little  var iable clean-up
#
unset EXIT_LINE1
unset EXIT_LINE2
#
}       #End of process_answer funtion
#
###################################################################
#End of Function Definitions
#
######################Mian Script#################################
#Get name of User Account to check
#
echo  "Step $1 - Determin User Account name to Delete "
echo
LINE1= "please enter the username of the user "
LINE2= "Account you wish to delete from system:"
get_answer
USER_ACCOUNT=$ANSWER
#
#Double check  with  script user that  this  is  the coreect User Account
#
LINE1= "Is $USER_ACCOUNT the user account "
LINE2= "You wish to delete from the system?[y/n]"
get_answer
#
#Call process_answer funtion:
#       If user answer anything but  "yes" .exit script
#
EXIT_LINE1= "Because the account,$USER_ACCOUNT,is not"
EXIT_LINE2= "The one you wish to delete.we are leaving the script..."
process_answer
#
############################################################################
#
USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT)
#
if  [ $? -eq  1 ]          #If the account  is  not found.exit script
then
         echo
         echo  "Account,$USER_ACCOUNT.not found"
         echo  "Leaving the script..."
         echo
         exit
fi
#
echo  "I found this record:"
echo $USER_ACCOUNT_RECORD
echo
#
LINE1= "Is this the correct User Account?[y/n]"
get_answer
#
#
#Call process_answer  function :
#       If user answers anything but  "yes" ,exit script
#
EXIT_LINE1= "Because the account,$USER_ACCOUNT,is not"
EXIT_LINE2= "The one you wish to delete.we are leaving the script...."
process_answer
#
#####################################################################
#Search  for  any running processes that belong to the User Account
#
echo
echo  "Step #2 - Find process on system beloging to user account"
echo
echo  "$USER_ACCOUNT has the following process running:"
echo
#
ps -u $USER_ACCOUNT     #List user processes running.
case  $?  in
1 )      #No processes running  for  this  User Account
         #
         echo  "There are no processes for this account currently running."
         echo
         ;;
0 )      #Processes running  for  this  User Account.
         #Ask Script User  if  wants us to kill the processes.
         #
         unset ANSWER
         LINE1= "Would you like me to kill me process(es)?[y/n]"
         get_answer
         #
         case  $ANSWER  in
         y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)        #If user answers  "yes"
                                         #Kill User Account processes.
                 #
                 echo
                 #
                 #Clean-up temp file upon signals
                 trap  "rm $USER_ACCOUNT_Running_Process.rpt"  SIGTERM SIGINT SIGQUIT
                 #
                 #List user processes running
                 ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt
                 #
                 exec < $USER_ACCOUNT_Running_Process.rpt        #Make report Std Input
                 read USER_PROCESS_REC   #First record will be blank
                 read USER_PROCESS_REC
                 #
                 while  [ $? -eq  0  ]
                 do
                         #obtain PID
                         USER_PID=`echo $USER_PROCESS_REC|cut -d " "  -f1`
                         kill - 9  $USER_PID
                         echo  "Killed process $USER_PID"
                         read USER_PROCESS_REC
                 done
                 #
                 echo
                 rm $USER_ACCOUNT_Running_Process.rpt    #Remove temp report.
                 ;;
         *)      #If user answer anything but  "yes" , do  not kill
                 echo
                 echo  "Will not kill the process(es)"
                 echo
                 ;;
         esac
         ;;
esac
##########################################################################
#Create a report of all files owned by User Account
#
echo
echo  "Step #3 - Find files on system belonging to user account"
echo
echo  "Creating a report of all files owned by $USER_ACCOUNT."
echo
echo  "It is recommended that you backup/archive these files."
echo  "and then do one of two things;"
echo  " 1)Delete the files"
echo  " 2) Change the files' ownership to a current user account."
echo
echo  "Please wait.This may take a while...."
echo
echo  "Please wait.This may take a while...."
#
REPORT_DATE=`date +%y%m%d`
REPORT_FILE=$USER_ACCOUNT "_files_" $REPORT_DATE ".RPT"
#
find / -user $USER_ACCOUNT > $REPORT_FILE  2 > /dev/ null
#
echo
echo  "Report is commlete."
echo  "Name of report: $REPORT_FILE"
echo  "Location of report: `pwd`"
echo
############################################
#Remove User Account
echo
echo  "Step #4 - Remove user account"
echo
#
LINE1= "Do you wish to remove $USER_ACCOUNT account from system?[y/n]"
get_answer
#
#Call process_answer  function :
#        if  user answer anythin but  "yes" .exit script
#
EXIT_LINE1= "Since you do not wish to remove the user account."
EXIT_LINE2= "$USER_ACCOUNT at this time.exiting the script... "
process_answer
#
userdel $USER_ACCOUNT   # delete  user account
echo
echo  "User account.$USER_ACCOUNT.has been removed"
echo









本文转自 zouqingyun 51CTO博客,原文链接:http://blog.51cto.com/zouqingyun/1696340,如需转载请自行联系原作者
目录
相关文章
|
8月前
|
Shell
Shell VSCode 基本开发插件(语法提示、错误检测、格式化、运行代码)
Shell VSCode 基本开发插件(语法提示、错误检测、格式化、运行代码)
430 0
|
测试技术
loadrunner 运行脚本-命令行运行脚本
loadrunner 运行脚本-命令行运行脚本
85 0
VBScript脚本运用(脚本程序与宿主程序的交互)
做过工控的人都知道,对脚本语言的支持是标准组态软件应具备的一个基本功能(如iFix,组态王等)。如果我们用VB开发类似的功能,能实现吗
722 0
|
Python
Python编写简单的通知栏脚本启动工具
随着自己编写的脚本与安装的工具越来越多,电脑的桌面和文件夹也越来越乱了。 就在前几天因为一个不小心,我把自己的一个项目给删了,所以决定将电脑整理一下。 不过这一整理我很多脚本和工具的位置我就忘记了,所以决定写个小工具来快速启动一些常用的脚本或工具 这里选择Python是因为Python跨平台,这样我的Linux电脑也可以用了,而且简单。
1470 0