本文实例为大家分享了Linux定义变量脚本,供大家参考,具体内容如下
现有两段基本一样的代码,只是变量进行改变,其他都没有变化,但是执行过程中出现了不一样的结果
代码一:
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
测试脚本也没有出现问题,但是不知道原因是什么,求大神能够解答,不胜感谢!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持免费资源网。