Operation and maintenance Shell script master test (9): redirection operator ">" and double redirection ">>"


A little test of operation and maintenance shell script (1)

A little test of operation and maintenance shell script (2)

A quick test of operation and maintenance Shell script (3)::$(cd $(dirname $0); pwd) command detailed explanation

A little test of operation and maintenance Shell script (4): multi-layer nesting if...elif...elif...else fi_Snail Yang Ge's Blog-CSDN Blog

Cenos7 installation train program animation

A little test of the operation and maintenance shell script (5): until loop

A little test of operation and maintenance Shell script (6): Function recognition in Shell

A little test of operation and maintenance Shell script (7): Call a function in another script file from a function file

A little trial of operation and maintenance shell script (8): case mode ignores the case mode of command line parameters demonstration

Operation and maintenance Shell script master test (9): redirection operator ">" and double redirection ">>"

A little trial of operation and maintenance shell script (10) 


1: Redirect operator data to file

">"Single redirection operator, as in this example, loops through the files ending with ".tmp" in the given directory and deletes them in a loop. If an error occurs during the deletion process, the error information is entered into the errors.log file;

[root@www standandinout]# cat removetmp.sh 
#!/bin/bash -
#================================ ================================================== =================================
#
#
# FILE: removetmp.sh
# USAGE: ./removetmp.sh
# DESCRIPTION: Find the crisis ending in .tmp in the given directory and delete it in a loop. If the loop encounters an error, redirect to the errors.log file
# OPTIONS: -------
# REQUIREMENTS: ------ ---

# BUGS: ------
# NOTES: -------- 
# AUTHOR: ---------YangGe (TOM) ,[email protected]
# ORGANIZATION :
# CREATED: 2023-8-24 09:11:20    
# REVISION: --------
#
#
#
#
#
#============================================== ================================================== ====================
# If the number of parameters is less than 1, (no parameters are specified), execute
if [ $# -lt 1 ]; then

# Print the method information used by the script
   echo "Usage: $0 DIRECTORY............."
   # Exit the script
   exit;

fi

# Use for loop variables to specify all parameters for the script on the command line

for dir in $@
  do
     
    # Find the file with .tmp suffix in the specified directory and delete it
    find $dir -name "*.tmp" -exec rm -f {} \;
    # Replace the error message generated by the for loop Write to file errors.log
   done 2 > errors.log
 

2: Output test


[root@www standandinout]# ll
耻扫8
-rw-r--r-- 1 root root 61 Sep 11 19:54 date.txt
-rw-r--r-- 1 root root 0 Sep 11 21 :22 errors.log
-rwxr-xr-x 1 root root 1255 Sep 11 21:17 removetmp.sh

创built date.tmp errors.tmp first.tmp 文数
[root@www standandinout]# touch date.tmp errors.tmp first.tmp
[root@www standandinout]# ll   
总数 8
-rw-r--r-- 1 root root 0 Sep 11 21:32 date.tmp
-rw-r--r-- 1 root root 61 Sep 11 19:54 date.txt
-rw-r--r-- 1 root root 0 Sep 11 21 :22 errors.log
-rw-r--r-- 1 root root 0 Sep 11 21:32 errors.tmp
-rw-r--r-- 1 root root 0 Sep 11 21:32 first.tmp
- rwxr-xr-x 1 root root 1255 Sep 11 21:17 removetmp.sh


Excuting an order:

[root@www standandinout]# ./removetmp.sh 
Usage: ./removetmp.sh DIRECTORY.............
[root@www standandinout]# pwd
/usr/local/example/standandinout
[root@www standandinout]# ./removetmp.sh  /usr/local/example/standandinout

#After executing this command, the files ending with "*.tmp" are deleted
[root@www standandinout]# ll
total usage 8
-rw-r--r-- 1 root root 61 September 11 19:54 date.txt
- rw-r--r-- 1 root root 0 September 11 21:34 errors.log
-rwxr-xr-x 1 root root 1255 September 11 21:17 removetmp.sh


[ root @ www standandinout ] # touch first . tmp second . tmp three . tmp 
[ root @ www standandinout ] # ll
总用量 8
- rw - r - - r -- 1 root root 0 9月 11 21:46 date . tmp
-rw-r--r-- 1 root root 61 Mar 11 19:54 date.txt
-rw-r--r-- 1 root root 0 Mar 11 21:34 errors.log
-rw-r--; r-- 1 root root 0 9/11 21:47 first.tmp
-rwxr-xr-x 1 root root 1255 9/11 21:17 removetmp.sh
-rw-r--r-- 1 root root 0 9/9 11 21:47 second.tmp
-rw-r--r-- 1 root root 0 9月 11 21:47 three.tmp
[root@www standandinout]# ./removetmp.sh /usr/local/example/standandinout/; usr / local / example /
[ root@www standandinout ]# ll
总用量 8
-rw-r--r-- 1 root root 61 9月 11 19:54 date.txt
-rw-r--r-- 1 root root    0 9月  11 21:48 errors.log
-rwxr-xr-x 1 root root 1255 9月  11 21:17 removetmp.sh
 

Three: Constructing exception execution conditions 


Since the current operating user is root, the exception information data is redirected to the errors.log file. Now add a yangge user and group.

Execute respectively:


useradd yangge; #Add user yangge

groupadd yangge; # Add yangge group

Use root user to create touch first.tmp second.tmp date.tmp file,

Change the user and user group to which errors.log and removetmp.sh belong


[yangge@www standandinout] $chown yangge:yannge removetmp.sh errors.log 
[yangge@www standandinout]$ll
总用量 12
-rw-r--r-- 1 root root 0 9月 11 22:35 date.tmp
-rw-r--r-- 1 root root 61 Mar 11 19:54 date.txt
-rw-r--r-- 1 young young 149 Mar 11 22:35 errors.log
-rw-r--; r-- 1 root root 0 Mar 11 22:35 first.tmp
-rwxr-xr-x 1 young young 1255 Mar 11 21:17 removetmp.sh
 

Four: Switch to the yangge account and execute the following command 


-rwxr-xr-x 1 yangge yangge 1255 9月 11 21:17 removetmp.sh
[yangge@www defaultinout]$ ./removetmp.sh /usr/local/example/standandinout
[yangge@wwwdefaultinout]$ ll
总用量 12
-rw-r--r-- 1 root root 0 9 Apr 11 22:35 date.tmp
-rw-r--r-- 1 root root 61 9 Mar 11 19:54 date.txt
-rw-r--; r-- 1 young youngge 149 Mar 11 22:35 errors.log
-rw-r--r-- 1 root root 0 Mar 11 10:35 pm first.tmp
-rwxr-xr-x 1 young youngge 1255 9/9 11 21:17 removetmp.sh

# Check the error log file, which records the exceptions of the total operation parameters. You can see single redirection">"data to error file
[yangge@www standandinout]$ cat errors.log 
rm: cannot be deleted"/usr/local/example/standandinout/ date.tmp": Insufficient permissions
rm: Unable to delete "/usr/local/example/standandinout/first.tmp": Insufficient permissions


Execute the script command again:

[yangge@www standandinout]$ ./removetmp.sh /usr/local/example/standandinout
[yangge@www standandinout]$ llTotal
usage 12
-rw-r--r-- 1 root root 0 September 11 22:35 date.tmp
-rw-r--r-- 1 root root 61 September 11 19:54 date.txt
-rw-r--r-- 1 yangge yangge 149 September 11 22:47 errors.log
-rw- r--r-- 1 root root 0 September 11 22:35 first.tmp
-rwxr-xr-x 1 yangge yangge 1255 September 11 21:17 removetmp.sh
[yangge@www standandinout]$ cat errors.log 
rm : Unable to delete "/usr/local/example/standandinout/date.tmp": Insufficient permissions
rm: Unable to delete "/usr/local/example/standandinout/first.tmp": Insufficient permissions

Lead to questions:

The error log still has two lines, which means that the log will be cleared every time it is executed and redirected to the error log file; when we need to append the error log to the file every time, is there a solution? Of course, use the double redirection operator"> >" That is, each execution will not clear the previous log content, and the latest log will be downloaded to the error file.

 

Five: Double-direction operator implements appending data to file

#!/bin/bash -
#========================================== ================================================== ======================
#
#
# FILE: removetmp.sh
# USAGE: ./removetmp.sh
# DESCRIPTION: Find the end of .tmp in the given directory crisis, and delete it in a loop. If the loop encounters an error, it will be redirected to the errors.log file
# OPTIONS: -------
# REQUIREMENTS: ---------

# BUGS: ---- --
# NOTES: -------- 
# AUTHOR: ---------YangGe (TOM) ,[email protected]
# ORGANIZATION:
# CREATED: 2023-8-24 09: 11:20    
# REVISION: --------
#
#
#
#
#
#================================================== ================================================== =================
# If the number of parameters is less than 1, (no parameters are specified), execute
if [ $# -lt 1 ]; then

# Print the method information used by the script
   echo "Usage: $0 DIRECTORY............."
   # Exit the script
   exit;

fi

# Use for loop variables to specify all parameters for the script on the command line

for dir in $@
  do

    # Find the files with the .tmp suffix in the specified directory and delete them
    find $dir -name "*.tmp" -exec rm -f {} \;
    # Write the error information generated by the for loop to the file errors. log
   done 2 >> errors.log

~                                                          

 Six: Test the execution results of the double-direction operator

[yangge@www standandinout]$ ./removetmp.sh /usr/local/example/standandinout
[yangge@www standandinout]$ cat errors.log 
rm: Unable to delete "/usr/local/example/standandinout/date.tmp": Insufficient permissions
rm: cannot delete "/usr/local/example/standandinout/first.tmp": Insufficient permissions
rm: cannot delete "/usr/local/example/standandinout/date.tmp": Insufficient permissions
rm: cannot delete"/ usr/local/example/standandinout/first.tmp": Insufficient permissions
 


It can be seen that there are two more pieces of data in the log file. . . . . . . .

Guess you like

Origin blog.csdn.net/u014635374/article/details/132817952