1. 程式人生 > >Linux定義變量的腳本

Linux定義變量的腳本

backup import ive 定義變量 conf 宋體 .gz strong --

現有兩段基本一樣的代碼,只是變量進行改變,其他都沒有變化,但是執行過程中出現了不一樣的結果

代碼一:

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/ rsync_backup@172.16.1.41::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

代碼二:

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/ rsync_backup@172.16.1.41::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

上面代碼只是修改了打包的情況,tar zcf /backup和定義一個變量tar $Path/結果就出現不一樣的情況,第一種執行結果是:

代碼一執行結果:

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

代碼二執行結果:

[root@nfs01 backup]# ls
172.16.1.31

代碼一和代碼二原理是一樣的,但是為什麽執行的結果卻有差別呢?個人覺得就是環境變量的目錄的開頭引起的,這樣讓前面的環境變量失效了,打包了兩次:

測試腳本

[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/ rsync_backup@172.16.1.41::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

測試腳本也沒有出現問題,但是不知道原因是什麽,求大神能夠解答,不勝感謝!!!!

Linux定義變量的腳本