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

Linux Variable Definition Script Sharing

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

There are two basic identical codes, but only the variables are changed, and everything else remains the same. However, 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 results are 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 is caused by the directory of the environment variables at the beginning, which makes the previous environment variables invalid, and it has been 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 either, 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 also hope everyone will support the Yelling Tutorial more.

Statement: The content of this article is from the Internet, 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. It has not been manually edited and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)

You May Also Like