新西兰服务器

如何编写脚本实用工具


如何编写脚本实用工具

发布时间:2021-12-20 09:21:02 来源:高防服务器网 阅读:87 作者:小新 栏目:大数据

这篇文章给大家分享的是有关如何编写脚本实用工具的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

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

感谢各位的阅读!关于“如何编写脚本实用工具”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[