您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關如何編寫腳本實用工具的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1、查看哪個文件占用最大
查看前十名磁盤空間用戶,到第11行,sed會刪除列表的剩余部分,然后給列表中每行一個行號。要讓行號和磁盤空間文本
位于同一行,用N命令將文本行合并在一行。然后用gawk命令清理,在行號后,加一個冒號(:),還給每行文本的輸出行中的每個字段放了一個制表符。這樣就生成了一個格式精致的前十名
磁盤空間用戶列表了
[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、創造加了日期的前十名磁盤空間用戶報告的腳本
[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循環
[root@digitcube-test1 tmp]# cat /home/qingyun/file_to_backup /home/qingyun/test1 /home/qingyun/test2 /home/qingyun/test3 /home/qingyun/love
[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
按小時歸檔的腳本
歸檔目錄包含了跟一年中的各個月份對應的目錄,將月的序號作為目錄名。而每月的目錄中又包含跟一個月中的各天對應的目錄(用天序號來作為目錄)。這樣只用給每個歸檔文件加時間戳然后將它他們放到跟日和月份對應的目錄就行了。
[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:刪除變量和函數
[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 variable 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 variable 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
感謝各位的閱讀!關于“如何編寫腳本實用工具”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。