Linux Definition Variable Script Sharing

Time:2019-8-5

In this paper, we share Linux variable definition script for your reference. The details are as follows.

There are two pieces of code that are basically the same, but the variables are changed, and the others are unchanged, but the results are different in the execution process.

Code 1:


vi back.sh
#backup import file,such as /etc/rc.local /var/spool/cron/root
IP=$(ifconfig eth1|sed -nr '2s#.*addr:(.*) B.*##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 2:


vi back.sh
#backup import file,such as /etc/rc.local /var/spool/cron/root
IP=$(ifconfig eth1|sed -nr '2s#.*addr:(.*) B.*##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 tar $Path/ result, which is different from the definition of a variable. The first execution result is:

Code 1 execution results:

[[email protected] backup]# ls
172.16.1.31 backup_2017-12-23-6.tar.gz flag_2017-12-23-6.log

Code 2 execution results:

[[email protected] backup]# ls
172.16.1.31

Code one and code two are the same, but why are the results different? Personally, I think it is caused by the beginning of the directory of environmental variables, which invalidates the previous environmental variables and packages them twice:

Test script


[[email protected] scripts]# sh -x back.sh 
++ sed -nr '2s#.*addr:(.*) B.*##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

There are no problems with the test script, but I don’t know why. I would like to ask God to answer them. Thank you very much.

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.

Recommended Today

Method of DOS intercepting string

In order to illustrate the problem of intercepting strings in variables, the following example has been given: Copy codeThe code is as follows: @echo offset str=123456789Echo first character is:% STR: ~ 0,1%The first two characters of echo are:% STR: ~ 0,2%The first 5 characters of echo are:% STR: ~ 0,5%Echo the string after the last […]