English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Compartilhamento de script de definição de variáveis no Linux

This article shares the Linux variable definition script for everyone's reference, the specific content is as follows

There are two basic identical codes, but only the variables are changed, and the rest are unchanged, but different results appeared during the execution process

Code one:

vi back.sh
#backup import file,such as /etc/rc.local /var/spool/cron/root
IP=$(ifconfig eth1|sed -nr '2s#.*addr:(.*) B.*#\1#gp')
Path=/backup
if [ $(date +%w) -eq 0 ]
then
  Time=$(date +%F-%w -d "-1 day")
else
  Time=$(date +%F "-1 day")
fi
mkdir $Path/$IP -p
cd / &&\
tar zcfh $Path/$IP/backup_$Time.tar.gz var/spool/cron/root etc/rc.local etc/sysconfig/iptables var/www/html app/logs &&\
md5sum $Path/$IP/backup_$Time.tar.gz >$Path/$IP/flag_$Time.log &&\
rsync -azv $Path/ [email protected]::backup --password-file=/etc/rsyncd.password &&\
find $Path/ -type f \( -name "*.log" -o -name "*.tar.gz" \) -mtime +7 |xargs rm –f
"back.sh" 15L, 628C written

Code two:

vi back.sh
#backup import file,such as /etc/rc.local /var/spool/cron/root
IP=$(ifconfig eth1|sed -nr '2s#.*addr:(.*) B.*#\1#gp')
Path=/backup
if [ $(date +%w) -eq 0 ]
then
  Time=$(date +%F-%w -d "-1 day")
else
  Time=$(date +%F "-1 day")
fi
mkdir $Path/$IP -p
cd / &&\
tar zcfh /backup/$IP/backup_$Time.tar.gz var/spool/cron/root etc/rc.local etc/sysconfig/iptables var/www/html app/logs &&\
md5sum $Path/$IP/backup_$Time.tar.gz >$Path/$IP/flag_$Time.log &&\
rsync -azv $Path/ [email protected]::backup --password-file=/etc/rsyncd.password &&\
find $Path/ -type f \( -name "*.log" -o -name "*.tar.gz" \) -mtime +7 |xargs rm –f
"back.sh" 15L, 628C written

The above code only modifies the packaging situation, tar zcf /backup and define a variable tar $Path/The result is different, the first execution result is:

Execution result of code one:

[root@nfs01 backup]# ls
172.16.1.31 backup_2017-12-23-6.tar.gz flag_2017-12-23-6.log

Execution result of code two:

[root@nfs01 backup]# ls
172.16.1.31

The principles of code one and code two are the same, but why do the execution results differ? I think it's because of the directory at the beginning of the environment variable, which makes the previous environment variable invalid, packaged twice:

Test script

[root@nfs01 scripts]# sh -x back.sh 
++ sed -nr '2s#.*addr:(.*) B.*#\1#gp'
++ ifconfig eth1
+ IP=172.16.1.31
+ Path=/backup
++ date +%w
+ '[' 4 -eq 0 ']'
++ date +%F -d '-1 day'
+ Time=2017-12-20
+ mkdir /backup/172.16.1.31 -p
+ cd /
+ tar zcfh /backup/172.16.1.31/backup_2017-12-20.tar.gz var/spool/cron/root etc/rc.local etc/sysconfig/iptables var/www/html app/logs
+ md5sum /backup/172.16.1.31/backup_2017-12-20.tar.gz
+ rsync -azv /backup/ [email protected]::backup --password-file=/etc/rsyncd.password
sending incremental file list
172.16.1.31/backup_2017-12-20.tar.gz
172.16.1.31/flag_2017-12-20.log
sent 1128 bytes received 65 bytes 2386.00 bytes/sec
total size is 2960 speedup is 2.48
+ xargs rm -f
+ find /backup/ -type f '(' -name '*.log' -o -name '*.tar.gz' ') -mtime +7

The test script did not have any problems, but I don't know what the cause is. I ask the great gods to answer, thank you very much!

 That's all for this article. I hope it will be helpful to everyone's learning, and I also hope everyone will support the Shouting Tutorial more.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, nor has it been manually edited, nor does it assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

Você também pode gostar