shell programming problem (XXIII)

topic:

Design a shell program, the first day of each month and backup compression / etc directory of all content stored in the current directory / root / bak (root directory needs to exist) directory and file name for the form yymmdd_etc, yy is year, mm for the month, dd for the day. Shell program fileback stored in the / usr / bin directory.

 

answer:

#!/bin/bash

DIRNAME=`ls ./root | grep bak`

if [ -z "$DIRNAME" ]; then
    echo $DIRNAME
    mkdir ./root/bak
    cd ./root/bak
fi

BACKETC=$(date +%Y%m$d)_etc.tar.gz
tar zcvf $BACKETC /etc
echo $BACKETC
echo "fileback finished"

 

Writing task timer:

echo "0 0 1 * * /bin/sh /usr/bin/fileback" >; /root/etcbakcron
crontab /root/etcbakcron

 

Or use the crontab -e command to add a scheduled task:

0 1 * * * /bin/sh /usr/bin/fileback

 

 

When the shell script if judged -a - -z

[-A FILE] True if FILE exists.  
[-B FILE] If FILE exists and is a block special file True.  
[-C FILE] If FILE exists and is a character special file True.  
[-D FILE] If FILE exists and is a directory True.  
[-E FILE] True if FILE exists.  
[-F FILE] If FILE exists and is a regular file True.  
[-G FILE] If FILE exists and has been set SGID True. [-H FILE] If FILE exists and is a symbolic link True.  
[-K FILE] If FILE exists and made sticky bit has been set True.  
[-P FILE] If FILE exists and is a named pipe (F if O) True.  
[-R & lt FILE] If FILE exists and is readable True.  
[-S FILE] FILE exists and if the size is not 0 True.  
[-T FD] If the file descriptor FD is open and refers to a terminal True.  
[-U FILE] FILE exists and if the SUID (set user ID) True.  
[-W FILE] If FILE if FILE exists and is writable True.  
[-X FILE] If FILE exists and is executable True.  
[-O FILE] FILE exists and if the user ID is valid True.  
[-G FILE] If FILE exists and is a valid user group True.  
[-L FILE] If FILE exists and is a symbolic link True.  
[-N FILE] If FILE exists and has been mod if ied since it was last read was true.  
[-S FILE] If FILE exists and is a socket True.  
[FILE1 -nt FILE2] if FILE1 has been changed more recently than FILE2 , or if FILE1 exists and FILE2 does not True.  
[FILE1 -ot FILE2] True if FILE1 is older than FILE2, or FILE2 exists and FILE1 True does not exist.  
[FILE1 -ef FILE2] If the same FILE1 and FILE2 pointing device and inode numbers True.  
[-O OPTIONNAME] If the shell option "OPTIONNAME" True open.  
[-Z STRING] length "STRING" True zero.  
Length [-n STRING] or [STRING] "STRING" is non-zero non-zero True.  
[STRING1 == STRING2] If the same two strings. "=" May be used instead of "==" for strict POSIX compliance True.  
[ STRING1 != STRING2 ]  如果字符串不相等则为真。 
[ STRING1 < STRING2 ]  如果 “STRING1” sorts before “STRING2” lexicographically in the current locale则为真。  
[ STRING1 > STRING2 ]  如果 “STRING1” sorts after “STRING2” lexicographically in the current locale则为真。  
[ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.

 

=====================================================================

Basically and other scripting languages. There is not much difference. But it is worth noting Yes. [] Which is conditional.

1, the string is determined

str1 = str2 when two strings with the same contents, the length of real 
str1! = str2 str1 and str2 string when unequal true when 
-n str1 string when the length is greater than 0 is true (non-empty string) 
the -Z When str1 -length string is true (the empty string) is 0 
str1 str1 is true when the string is non-empty

2, the digital determination

int1 -eq int2 two equal number of true 
int1 -ne int2 two numbers are not equal to true 
int1 -gt int2 int1 is greater than int2 true 
int1 -ge int2 int1 int2 greater than or equal to true 
int1 -lt int2 int1 int2 less than true 
int1 -le int2 int2 less true INT1

3, the file is determined

-r file true user-readable 
-w file writable is true 
-x file users can perform true 
-f file file as a regular file is true 
-d file file is a directory true 
-c file file is a character special file true 
-b file file block special file is true 
-s file when the file size is non-zero is true 
is true -t file if the file descriptor (default 1) specifies the terminal device

4, complex logic determines

-a and 
-o or 
! non

 

Reprinted: https://www.cnblogs.com/new-journey/p/11017659.html

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/12171060.html