溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何編寫(xiě)腳本實(shí)用工具

發(fā)布時(shí)間:2021-12-20 09:21:02 來(lái)源:億速云 閱讀:177 作者:小新 欄目:大數(shù)據(jù)

這篇文章給大家分享的是有關(guān)如何編寫(xiě)腳本實(shí)用工具的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

1、查看哪個(gè)文件占用最大

查看前十名磁盤(pán)空間用戶,到第11行,sed會(huì)刪除列表的剩余部分,然后給列表中每行一個(gè)行號(hào)。要讓行號(hào)和磁盤(pán)空間文本
位于同一行,用N命令將文本行合并在一行。然后用gawk命令清理,在行號(hào)后,加一個(gè)冒號(hào)(:),還給每行文本的輸出行中的每個(gè)字段放了一個(gè)制表符。這樣就生成了一個(gè)格式精致的前十名
磁盤(pán)空間用戶列表了

[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、創(chuàng)造加了日期的前十名磁盤(pán)空間用戶報(bào)告的腳本

 
[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、創(chuàng)建按日期歸檔的腳本

歸檔文件,讓腳本讀取file_to_backup里面每個(gè)目錄或文件,用到一個(gè)簡(jiǎn)單read命令,來(lái)讀取該文件中的每一條記錄。

exec<$CONFIG_FILE

read FILE_NAME

為歸檔配置文件以及從file_to_backup讀取每條記錄都用了變量.只要read命令在配置文件中發(fā)現(xiàn)還有記錄要讀,它就會(huì)在?變量中返回一退出狀態(tài)碼0表示成功,以while循環(huán)的測(cè)試條件來(lái)讀取file_to_backup的所有記錄

while [ $? -eq 0 ]

do

....

read FILE_NAME

done

一旦read命令到了末尾,返回一個(gè)非0狀態(tài)碼,腳本會(huì)退出while循環(huán)

[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

按小時(shí)歸檔的腳本

歸檔目錄包含了跟一年中的各個(gè)月份對(duì)應(yīng)的目錄,將月的序號(hào)作為目錄名。而每月的目錄中又包含跟一個(gè)月中的各天對(duì)應(yīng)的目錄(用天序號(hào)來(lái)作為目錄)。這樣只用給每個(gè)歸檔文件加時(shí)間戳然后將它他們放到跟日和月份對(duì)應(yīng)的目錄就行了。

[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、管理用戶賬號(hào)

腳本進(jìn)入刪除用戶4個(gè)步聚:

1、獲得并確認(rèn)用戶賬戶名,

2、查找和終止用戶的進(jìn)程,

3、創(chuàng)建一份屬于該用戶賬號(hào)的所有文件報(bào)告,

4、最終刪除用戶賬號(hào)

用到判斷參數(shù)

-z:字符長(zhǎng)度0,為真

-n:字符長(zhǎng)度非0,為真

unset:刪除變量和函數(shù)

[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


感謝各位的閱讀!關(guān)于“如何編寫(xiě)腳本實(shí)用工具”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI