shell脚本编写简单的实用工具

归档

标题按日归档

这里写图片描述

#/bin/bash

DATE=$(date +%y%m%d)

FILE=archive$DATE.tar.gz

CONFIG_FILE=/root/archive/Files_To_Backup
DESTINATION=/root/archive/$FILE

if [ -f $CONFIG_FILE ]
then 
    echo "$CONFIG_FILE is exist."

else
    echo "$CONFIG_FILE does not exist."
    exit
fi

FILE_NO=1
exec < $CONFIG_FILE

read FILE_NAME
while [ $? -eq 0 ]
do
     if [ -f $FILE_NAME -o -d $FILE_NAME ]
     then
          FILE_LIST="$FILE_LIST $FILE_NAME"
     else
          echo
          echo "$FILE_NAME,does not exist."
          echo "Obviously,I will not include it in this archive."
          echo "It is listed on line $FILE_NO of the config file."
          echo "Counting to build archive list..."
          echo
      fi
      
      FILE_NO=$[$FILE_NO + 1]
      read FILE_NAME
done

echo $FILE_LIST

echo "Starting archive..."
echo
tar -czf $DESTINATION $FILE_LIST 2> /dev/null

echo "Archive completed"
echo "Resulting archive file is: $DESTINATION"
echo

exit

标题按小时归档

#/bin/bash

CONFIG_FILE=/root/archive/hourly/Files_To_Backup

BASEDEST=/root/archive/hourly

DAY=$(date +%d)
MONTH=$(date +%m)
TIME=$(date +%k%M)

mkdir -p $BASEDEST/$MONTH/$DAY

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



if [ -f $CONFIG_FILE ]
then 
    echo "$CONFIG_FILE is exist."

else
    echo "$CONFIG_FILE does not exist."
    exit
fi

FILE_NO=1
exec < $CONFIG_FILE

read FILE_NAME
while [ $? -eq 0 ]
do
     if [ -f $FILE_NAME -o -d $FILE_NAME ]
     then
          FILE_LIST="$FILE_LIST $FILE_NAME"
     else
          echo
          echo "$FILE_NAME,does not exist."
          echo "Obviously,I will not include it in this archive."
          echo "It is listed on line $FILE_NO of the config file."
          echo "Counting to build archive list..."
          echo
      fi
      
      FILE_NO=$[$FILE_NO + 1]
      read FILE_NAME
done

echo $FILE_LIST

echo "Starting archive..."
echo
tar -czf $DESTINATION $FILE_LIST 2> /dev/null

echo "Archive completed"
echo "Resulting archive file is: $DESTINATION"
echo

exit

管理用户账户

24.2.1 需要的功能
删除账号时,需要4个步骤:
(1)获得正确的待删除用户账号名
(2)杀死正在系统上运行的属于该账户的进程
(3)确认系统上属于该账户的所有文件
(4)删除该用户账户

#!/bin/bash

function get_answer {
unset ANSWER
ASK_COUNT=0
while [ -z "$ANSWER" ]
do
    ASK_COUNT=$[ $ASK_COUNT + 1 ]
    case $ASK_COUNT in
    2)
         echo "please answer the question."
         ;;
    3)
         echo "One lase try...please answer the question."
	 ;;
    4)
         echo "Since you refuse to answer the question..."
	 echo "exiting program."
         exit
     esac
    
     echo
   
     if [ -n "$LINE2" ]
     then
   	 echo $LINE1
         echo -e $LINE2" \c"
     else
         echo -e $LINE1" \c"
     fi
     
     read -t 60  ANSWER
done

unset LINE1
unset LINE2

}

function process_answer {

case $ANSWER in
y|Y|YES|yes|Yes|yEs|YEs|yES)
;;
*)
echo
echo $EXIT_LINE1
echo $EXIT_LINE2
echo
exit
;;
esac

unset EXIT_LINE1
unset EXIT_LINE2

}

############ Main Script#############
echo "Step #1 - Determine 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
echo "$USER_ACCOUNT"

LINE1="Is $USER_ACCOUNT the user account "
LINE2="you wish to delete from the system?[y/n]"
get_answer

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 ]
then
    echo
    echo "Account,$USER_ACCOUNT, not found."
    echo "Leaving the script..."
    echo
    exit
fi

echo
echo "I found this record:"
echo $USER_ACCOUNT_RECORD

LINE1="Is this the correct User Account?[y/n]"
get_answer

EXIT_LINE1="Because the account,$USER_ACCOUNT,is not "
EXIT_LINE2="the one you wish to delete,we are leaving the script..."
process_answer

echo 
echo "Step #2 - Find process on system belonging to user account"
echo

ps -u $USER_ACCOUNT >/dev/null
case $? in
1)
    echo "There are no processed for this account currently running."
    echo
;;
0)
    echo "$USER_ACCOUNT has the following processes running:"
    echo
    ps -u $USER_ACCOUNT
    LINE1="Would you like me to kill the process(es)?[y/n]"
    get_answer

    case $ANSWER in
    y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)
    echo
    echo "Killing off process(es)..."

    COMMAND_1="ps -u $USER_ACCOUNT --no-heading"

    COMMAND_3="xargs -d \\n /usr/bin/sudo /bin/kill -9"

    $COMMAND_1 | gawk '{print $1}' | $COMMAND_3
  
    echo
    echo "Process(es) killed."
    ;;
*)
    echo
    echo "will not kill the process(es)"
    echo
    ;;
    esac
;;
esac

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..."
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 "Name of report:     $REPORT_FILE"
echo "Location of report: $(pwd)"
echo

echo 
echo "Step #4 - Remove user account"
echo

LINE1="Remove $USER_ACCOUNT's account from system?[y/n]"
get_answer

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
echo
echo "User account, $USER_ACCOUNT,has been removed"
echo

exit

这里写图片描述

监测磁盘空间

找出指定目录中磁盘空间使用量位居前十名的用户

#!/bin/bash

CHECK_DIRECTORIES="/var/log /home"

DATE=$(date '+%m%d%y')
exec > disk_space_$DATE.rpt

echo "Top Ten Disk Space Usage"
echo "for $CHECK_DIRECTORIES Directories"

for DIR_CHECK in $CHECK_DIRECTORIES 
do 
	echo ""
	echo "The $DIR_CHECK Directory:"

	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

exit

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值