码迷,mamicode.com
首页 > 其他好文 > 详细

编写简单的脚本使用工具

时间:2019-07-17 11:06:50      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:nload   解压   inf   backup   determine   follow   moved   style   删除文件   

#归档(自动备份)
    归档数据文件
        1. 需要的功能
            $ tar -zcf archive.tar.gz /home/Christine/Project/*.* 2>/dev/null    -- -z进行压缩,重定向错误日志清空掉(警告提示是去掉了开头的斜杠,以便于使用的时候可以解压到任意路径)
            $
            $ ls -l archive.tar.gz
            -rw-rw-r--. 1 Christine Christine 3331 Aug 27 10:53 archive.tar.gz
            不需要为待备份的新目录或文件修改或编写新的归档脚本,而是可以借助于配置文件存放。
            $ cat Files_To_Backup
            /home/Christine/Project
            /home/Christine/Downloads
            /home/Does_not_exist
            /home/Christine/Documents
            #
            exec < $CONFIG_FILE
            read FILE_NAME
            #
            while [ $? -eq 0 ]
            do
            [...]
            read FILE_NAME
            done
            #
            if [ -f $FILE_NAME -o -d $FILE_NAME ]    -- 文件或目录执行
            then
            # If file exists, add its name to the list.
            FILE_LIST="$FILE_LIST $FILE_NAME"
            else
                echo "$FILE_NAME, does not exist."
                echo
            fi
            #
            FILE_NO=$[$FILE_NO + 1] # Increase Line/File number by one.    -- 添加了变量 FILE_NO ,脚本可以告诉你在归档配置文件中哪行中含有不正确或缺失的文件或目录
        2. 创建逐日归档文件的存放位置
            $ sudo mkdir /archive    -- 创建归档仓库
            #创建用户组,并给用户赋权。用户组中的成员可以添加和删除文件
            $ sudo groupadd Archivers
            $
            $ sudo chgrp Archivers /archive
            $
            $ ls -ld /archive
            drwxr-xr-x. 2 root Archivers 4096 Aug 27 14:10 /archive
            $
            $ sudo usermod -aG Archivers Christine
            [sudo] password for Christine:
            $
            $ sudo chmod 775 /archive
            $
            $ ls -ld /archive
            drwxrwxr-x. 2 root Archivers 4096 Aug 27 14:10 /archive
            $ mv Files_To_Backup /archive/
            $
            $ ls /archive
            Files_To_Backup
        3. 创建按日归档的脚本
            #!/bin/bash
            #
            # Daily_Archive - Archive designated files & directories
            ########################################################
            #
            # Gather Current Date
            #
            DATE=$(date +%y%m%d)
            #
            # Set Archive File Name
            #
            FILE=archive$DATE.tar.gz
            #
            # Set Configuration and Destination File
            #
            CONFIG_FILE=/archive/Files_To_Backup
            DESTINATION=/archive/$FILE    -- DESTINATION 变量会将归档文件的全路径名加上去。 CONFIG_FILE 变量指向含有待归档目录信息的归档配置文件
            #
            ######### Main Script #########################
            #
            # Check Backup Config file exists
            #
            if [ -f $CONFIG_FILE ] # Make sure the config file still exists.
            then # If it exists, do nothing but continue on.
            echo
            else # If it doesn‘t exist, issue error & exit script.
            echo
            echo "$CONFIG_FILE does not exist."
            echo "Backup not completed due to missing Configuration File"
            echo
            exit
            fi
            #
            # Build the names 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 -o -d $FILE_NAME ]
            then
            # If file exists, add its name to the list.
            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 it 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 one.
            read FILE_NAME # Read next record.
            done
            #
            #######################################
            #
            # Backup the files and Compress Archive
            #
            echo "Starting archive..."
            echo
            #
            tar -czf $DESTINATION $FILE_LIST 2> /dev/null
            #
            echo "Archive completed"
            echo "Resulting archive file is: $DESTINATION"
            echo
            #
            exit
        4. 运行按日归档的脚本
            $ ls -l Daily_Archive.sh
            -rw-rw-r--. 1 Christine Christine 1994 Aug 28 15:58 Daily_Archive.sh
            $
            $ chmod u+x Daily_Archive.sh  -- 先赋予文件执行权限
            $ sh Daily_Archive.sh  -- 执行文件
            /home/Does_not_exist, does not exist.  -- 你会看到这个脚本发现了一个不存在的目录:/home/Does_not_exist。脚本能够告诉你这个错误的行在配置文件中的行号,然后继续创建列表和归档数据
            Obviously, I will not include it in this archive.
            It is listed on line 3 of the config file.
            Continuing to build archive list...
            Starting archive...
            Archive completed
            Resulting archive file is: /archive/archive140828.tar.gz
            $ ls /archive
            archive140828.tar.gz Files_To_Backup  -- 日文件已经备份了
        5. 创建按小时归档的脚本
            为归档文件创建一个目录层级
                $ sudo mkdir /archive/hourly
                [sudo] password for Christine:
                $
                $ sudo chgrp Archivers /archive/hourly
                $
                $ ls -ld /archive/hourly/
                drwxr-xr-x. 2 root Archivers 4096 Sep 2 09:24 /archive/hourly/
                $
                $ sudo chmod 775 /archive/hourly
                $
                $ ls -ld /archive/hourly
                drwxrwxr-x. 2 root Archivers 4096 Sep 2 09:24 /archive/hourly
            新目录设置好之后,将按小时归档的配置文件File_To_Backup移动到该目录中            
                $ cat Files_To_Backup
                /usr/local/Production/Machine_Errors
                /home/Development/Simulation_Logs
                $
                $ mv Files_To_Backup /archive/hourly/
                $
            创建Hourly_Archive.sh脚本
                #!/bin/bash
                #
                # Hourly_Archive - Every hour create an archive
                #########################################################
                #
                # Set Configuration File
                #
                CONFIG_FILE=/archive/hourly/Files_To_Backup
                #
                # Set Base Archive Destination Location
                #
                BASEDEST=/archive/hourly
                #
                # Gather Current Day, Month & Time
                #
                DAY=$(date +%d)
                MONTH=$(date +%m)
                TIME=$(date +%k0%M)    -- 在 %k 后加入数字0后,所有的单数字小时数都会被加入一个前导数字0,填充成两位数字。保证文件名中总是保留4位数字
                #
                # Create Archive Destination Directory
                #
                mkdir -p $BASEDEST/$MONTH/$DAY    --  -p 命令行选项。这个选项允许在单个命令中创建目录和子目录。另外,就算目录已经存在,它也不会产生错误消息
                #
                # Build Archive Destination File Name
                #
                DESTINATION=$BASEDEST/$MONTH/$DAY/archive$TIME.tar.gz
                #
                ########## Main Script ####################
                [...]    -- 和 编号3 的Main Script一致
        6. 运行按小时归档的脚本(测试成功后可以放入cron表)
            $ chmod u+x Hourly_Archive.sh
            $
            $ date +%k%M  -- 检查当前时间
            1011
            $
            $ sh Hourly_Archive.sh
            Starting archive...
            Archive completed
            Resulting archive file is: /archive/hourly/09/02/archive1011.tar.gz
            $
            $ ls /archive/hourly/09/02/
            archive1011.tar.gz  -- 归档文件名archive1011.tar.gz中包含了对应的小时(10)和分钟(11)
#管理用户账户
    需要的功能(1-4 确认正确的账户)
        1. 获取正确的账户名
        2. 创建函数获取正确的账户名
            function get_answer {
            #
            unset ANSWER    -- 清除脚本用户之前给出的答案
            ASK_COUNT=0
            #
            while [ -z "$ANSWER" ]
            do
            ASK_COUNT=$[ $ASK_COUNT + 1 ]    -- 通过给 ASK_COUNT变量增值,可以设定不同的消息来回应脚本用户
            #
            case $ASK_COUNT in    -- 
            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."
            echo
            #
            exit
            ;;
            esac
            #
            echo
            if [ -n "$LINE2" ]
            then #Print 2 lines
            echo $LINE1
            echo -e $LINE2" \c"
            else #Print 1 line
            echo -e $LINE1" \c"
            fi
            #
            read -t 60 ANSWER
            done
            #
            unset LINE1
            unset LINE2
            #
            } #End of get_answer function
            LINE1="Please enter the username of the user "
            LINE2="account you wish to delete from system:"
            get_answer
            USER_ACCOUNT=$ANSWER
        3. 验证输入的用户名
            function process_answer {
            #
            case $ANSWER in
            y|Y|YES|yes|Yes|yEs|yeS|YEs|yES )
            ;;
            *)
            echo
            echo $EXIT_LINE1
            echo $EXIT_LINE2
            echo
            exit
            ;;
            esac
            #
            unset EXIT_LINE1
            unset EXIT_LINE2
            #
            } #End of process_answer function
            EXIT_LINE1="Because the account, $USER_ACCOUNT, is not "
            EXIT_LINE2="the one you wish to delete, we are leaving the script..."
            process_answer
        4. 确定账户是否存在
            USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT)    --  -w 选项允许你对这个特定用户账户进行精确匹配
            if [ $? -eq 1 ]
            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
            #
            EXIT_LINE1="Because the account, $USER_ACCOUNT, is not"
            EXIT_LINE2="the one you wish to delete, we are leaving the script..."
            process_answer
        5. 删除属于账户的进程
            ps -u $USER_ACCOUNT >/dev/null #Are 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.
                #
                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
                #
                [...]
            esac
            case $ANSWER in
                y|Y|YES|yes|Yes|yEs|yeS|YEs|yES ) # If user answers "yes",
                #kill User Account processes.
                echo
                echo "Killing off process(es)..."
                #
                # List user processes running code in variable, COMMAND_1
                COMMAND_1="ps -u $USER_ACCOUNT --no-heading"
                #
                # Create command to kill proccess in variable, COMMAND_3
                COMMAND_3="xargs -d \\n /usr/bin/sudo /bin/kill -9"
                #
                # Kill processes via piping commands together
                $COMMAND_1 | gawk ‘{print $1}‘ | $COMMAND_3
                #
                echo
                echo "Process(es) killed."
                ;;
            esac
        6. 查找属于账户的文件
            find / -user $USER_ACCOUNT > $REPORT_FILE  --  find 命令用 -u 选项查找整个文件系统,它能够准确查找到属于该用户的所有文件
        7. 删除账户
            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
    创建脚本
        完整的Delete_User.sh脚本:
            #!/bin/bash
            #
            #Delete_User - Automates the 4 steps to remove an account
            #
            ###############################################################
            # Define Functions
            #
            #####################################################
            function get_answer {
            #
            unset ANSWER
            ASK_COUNT=0
            #
            while [ -z "$ANSWER" ] #While no answer is given, keep 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."
            echo
            #
            exit
            ;;
            esac
            #
            echo
            #
            if [ -n "$LINE2" ]
            then #Print 2 lines
            echo $LINE1
            echo -e $LINE2" \c"
            else #Print 1 line
            echo -e $LINE1" \c"
            fi
            #
            # Allow 60 seconds to answer before time-out
            read -t 60 ANSWER
            done
            # Do a little 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 answers "yes", do nothing.
            ;;
            *)
            # If user answers 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 function
            #
            ##############################################
            # End of Function Definitions
            #
            ############# Main Script ####################
            # Get name of User Account to check
            #
            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
            #
            # Double check with script user that this is the correct 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 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
            #
            ################################################################
            # Check that USER_ACCOUNT is really an account on the system
            #
            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
            echo "I found this record:"
            echo $USER_ACCOUNT_RECORD
            #
            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 belonging to user account"
            echo
            #
            ps -u $USER_ACCOUNT >/dev/null #Are 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.
            #
            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 ) # If user answers "yes",
            # kill User Account processes.
            #
            echo
            echo "Killing off process(es)..."
            #
            # List user processes running code in variable, COMMAND_1
            COMMAND_1="ps -u $USER_ACCOUNT --no-heading"
            #
            # Create command to kill proccess in variable, COMMAND_3
            COMMAND_3="xargs -d \\n /usr/bin/sudo /bin/kill -9"
            #
            # Kill processes via piping commands together
            $COMMAND_1 | gawk ‘{print $1}‘ | $COMMAND_3
            #
            echo
            echo "Process(es) killed."
            ;;
            *) # If user answers 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..."
            #
            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 complete."
            echo "Name of report: $REPORT_FILE"
            echo "Location of report: $(pwd)"
            echo
            ####################################
            # Remove User Account
            echo
            echo "Step #4 - Remove user account"
            echo
            #
            LINE1="Remove $USER_ACCOUNT‘s account from system? [y/n]"
            get_answer
            #
            # Call process_answer function:
            # if user answers anything 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
            #
            exit
    运行脚本
        $ chmod u+x Delete_User.sh    -- 赋权
        $
        $ ls -l Delete_User.sh
        -rwxr--r--. 1 Christine Christine 6413 Sep 2 14:20 Delete_User.sh
        $ sudo ./Delete_User.sh
        [sudo] password for Christine:
        Step #1 - Determine User Account name to Delete
        Please enter the username of the user
        account you wish to delete from system: Consultant
        Is Consultant the user account
        you wish to delete from the system? [y/n]
        Please answer the question.
        Is Consultant the user account
        you wish to delete from the system? [y/n] y
        I found this record:
        Consultant:x:504:506::/home/Consultant:/bin/bash
        Is this the correct User Account? [y/n] yes
        Step #2 - Find process on system belonging to user account
        Consultant has the following processes running:
        PID TTY TIME CMD
        5443 pts/0 00:00:00 bash
        5444 pts/0 00:00:00 sleep
        Would you like me to kill the process(es)? [y/n] Yes
        Killing off process(es)...
        Process(es) killed.
        Step #3 - Find files on system belonging to user account
        Creating a report of all files owned by Consultant.
        It is recommended that you backup/archive these files,
        and then do one of two things:
        1) Delete the files
        2) Change the files‘ ownership to a current user account.
        Please wait. This may take a while...
        Report is complete.
        Name of report: Consultant_Files_140902.rpt
        Location of report: /home/Christine
        Step #4 - Remove user account
        Remove Consultant‘s account from system? [y/n] y
        User account, Consultant, has been removed
        $
        $ ls Consultant*.rpt
        Consultant_Files_140902.rpt
        $
        $ cat Consultant_Files_140902.rpt
        /home/Consultant
        /home/Consultant/Project_393
        /home/Consultant/Project_393/393_revisionQ.py
        /home/Consultant/Project_393/393_Final.py
        [...]
        /home/Consultant/.bashrc
        /var/spool/mail/Consultant
        $
        $ grep Consultant /etc/passwd
        $    
#监测磁盘空间
    需要的功能
        $ sudo du -s /home/*    -- 使用 du 命令总结/home目录下每个用户的$HOME目录的磁盘占用情况
        [sudo] password for Christine:
        4204 /home/Christine
        56 /home/Consultant
        52 /home/Development
        4 /home/NoSuchUser
        96 /home/Samantha
        36 /home/Timothy
        1024 /home/user1    
        #查看系统目录(比如/var/log)的磁盘使用情况
        $ sudo du -s /var/log/*    -- 可以使用大写S,会为每个目录或子目录统计磁盘使用情况
        4 /var/log/anaconda.ifcfg.log
        20 /var/log/anaconda.log
        32 /var/log/anaconda.program.log
        108 /var/log/anaconda.storage.log
        40 /var/log/anaconda.syslog
        56 /var/log/anaconda.xlog
        116 /var/log/anaconda.yum.log
        4392 /var/log/audit
        4 /var/log/boot.log
        [...]
        #进行磁盘用量排序,-n 选项允许按数字排序, -r 选项会先列出最大数字(逆序)。查看占用磁盘空间最多的目录
        $ sudo du -S /var/log/ | sort -rn
        4392 /var/log/audit
        3020 /var/log/sa
        2976 /var/log/
        420 /var/log/gdm
        152 /var/log/ConsoleKit
        80 /var/log/prelink
        4 /var/log/sssd
        4 /var/log/samba/old
        4 /var/log/samba
        4 /var/log/ppp
        4 /var/log/ntpstats
        4 /var/log/httpd
        4 /var/log/cups
        #磁盘空间使用排名前十的用户
        $ sudo du -S /var/log/ |
        > sort -rn |
        > sed ‘{11,$D; =}‘ |
        > sed ‘N; s/\n/ /‘ |
        > gawk ‘{printf $1 ":" "\t" $2 "\t" $3 "\n"}‘
        [sudo] password for Christine:
        1: 4396 /var/log/audit
        2: 3024 /var/log/sa
        3: 2976 /var/log/
        4: 420 /var/log/gdm
        5: 152 /var/log/ConsoleKit
        6: 80 /var/log/prelink
        7: 4 /var/log/sssd
        8: 4 /var/log/samba/old
        9: 4 /var/log/samba
        10: 4 /var/log/ppp
    创建脚本
        #!/bin/bash
        #
        # Big_Users - Find big disk space users in various directories
        ###############################################################
        # Parameters for Script
        #
        CHECK_DIRECTORIES=" /var/log /home" #Directories to check
        #
        ############## Main Script #################################
        #
        DATE=$(date ‘+%m%d%y‘) #Date for report file
        #
        exec > disk_space_$DATE.rpt #Make report file STDOUT
        #
        echo "Top Ten Disk Space Usage" #Report header
        echo "for $CHECK_DIRECTORIES Directories"
        #
        for DIR_CHECK in $CHECK_DIRECTORIES #Loop to du directories
        do
        echo ""
        echo "The $DIR_CHECK Directory:" #Directory header
        #
        # Create a listing of top ten disk space users in this dir
        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 #End of loop
        #
        exit
    运行脚本
        $ ls -l Big_Users.sh
        -rw-r--r--. 1 Christine Christine 910 Sep 3 08:43 Big_Users.sh
        $
        $ sudo bash Big_Users.sh
        [sudo] password for Christine:
        $
        $ ls disk_space*.rpt
        disk_space_090314.rpt
        $
        $ cat disk_space_090314.rpt
        Top Ten Disk Space Usage
        for /var/log /home Directories
        The /var/log Directory:
        1: 4496 /var/log/audit
        2: 3056 /var/log
        3: 3032 /var/log/sa
        4: 480 /var/log/gdm
        5: 152 /var/log/ConsoleKit
        6: 80 /var/log/prelink
        7: 4 /var/log/sssd
        8: 4 /var/log/samba/old
        9: 4 /var/log/samba
        10: 4 /var/log/ppp
        The /home Directory:
        1: 34084 /home/Christine/Documents/temp/reports/archive
        2: 14372 /home/Christine/Documents/temp/reports
        3: 4440 /home/Timothy/Project__42/log/universe
        4: 4440 /home/Timothy/Project_254/Old_Data/revision.56
        5: 4440 /home/Christine/Documents/temp/reports/report.txt
        6: 3012 /home/Timothy/Project__42/log
        7: 3012 /home/Timothy/Project_254/Old_Data/data2039432
        8: 2968 /home/Timothy/Project__42/log/answer
        9: 2968 /home/Timothy/Project_254/Old_Data/data2039432/answer
        10: 2968 /home/Christine/Documents/temp/reports/answer
发送消息
    功能分析:
        1. 确定系统中都有谁(who):
            whoami  -- 确定当前账号
            who  -- 用户名,用户所在终端,用户登入系统的时间
        2. 启用消息功能(mesg):
            mesg  -- 查看当前用户是否开启信息接收
            mesg y  -- 开启信息接收
            mesg n  -- 关闭信息接收
            who -T  -- 查看用户信息是否开启。(+)表示开启,(-)表示关闭。
        3. 向其他用户发送消息(write):
            $ write xh_sw pts/37
            Hello Tim!
    创建脚本:
        #!/bin/bash
        #mu.sh - Send a Message to a particular user
        #############################################
        #
        # Save the username parameter
        #
        muser=$1
        #
        # Determine if user is logged on:
        #
        logged_on=$(who | grep -i -m 1 $muser | gawk ‘{print $1}‘)
        #
        if [ -z $logged_on ]
        then
        echo "$muser is not logged on."
        echo "Exiting script..."
        exit
        fi
        #
        # Determine if user allows messaging:
        #
        allowed=$(who -T | grep -i -m 1 $muser | gawk ‘{print $2}‘)
        #
        if [ $allowed != "+" ]
        then
        echo "$muser does not allowing messaging."
        echo "Exiting script..."
        exit
        fi
        #
        # Determine if a message was included:
        #
        if [ -z $2 ]
        then
        echo "No message parameter included."
        echo "Exiting script..."
        exit
        fi
        #
        # Determine if there is more to the message:
        #
        shift
        #
        while [ -n "$1" ]
        do
        whole_message=$whole_message‘ ‘$1
        shift
        done
        #
        # Send message to user:
        #
        uterminal=$(who | grep -i -m 1 $muser | gawk ‘{print $2}‘)
        #
        echo $whole_message | write $logged_on $uterminal
        #
        exit
获取格言
    #!/bin/bash
    #
    # Get a Daily Inspirational Quote
    #####################################
    #
    # Script Variables ####
    #
    quote_url=www.quotationspage.com/qotd.html
    #
    # Check url validity ###
    #
    check_url=$(wget -nv --spider $quote_url 2>&1)
    #
    if [[ $check_url == *error404* ]]
    then
    echo "Bad web address"
    echo "$quote_url invalid"
    echo "Exiting script..."
    exit
    fi
    #
    # Download Web Site‘s Information
    #
    wget -o /tmp/quote.log -O /tmp/quote.html $quote_url
    #
    # Extract the Desired Data
    #
    sed ‘s/<[^>]*//g‘ /tmp/quote.html |
    grep "$(date +%B‘ ‘%-d,‘ ‘%Y)" -A2 |
    sed ‘s/>//g‘ |
    sed ‘/&nbsp;/{n ; d}‘ |
    gawk ‘BEGIN{FS="&nbsp;"} {print $1}‘ |
    tee /tmp/daily_quote.txt > /dev/null
    #
    exit
编造借口
    #!/bin/bash
    #
    # Send a Text Message
    ################################
    #
    # Script Variables ####
    #
    phone="3173334444"
    SMSrelay_url=http://textbelt.com/text
    text_message="System Code Red"
    #
    # Send text ###########
    #
    curl -s $SMSrelay_url -d     number=$phone     -d "message=$text_message" > /dev/null
    #
    exit

 

编写简单的脚本使用工具

标签:nload   解压   inf   backup   determine   follow   moved   style   删除文件   

原文地址:https://www.cnblogs.com/TianMu/p/11199460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!