Linux定义变量脚本分享
本文实例为大家分享了Linux定义变量脚本,供大家参考,具体内容如下
现有两段基本一样的代码,只是变量进行改变,其他都没有变化,但是执行过程中出现了不一样的结果
代码一:
viback.sh #backupimportfile,suchas/etc/rc.local/var/spool/cron/root IP=$(ifconfigeth1|sed-nr'2s#.*addr:(.*)B.*#\1#gp') Path=/backup if[$(date+%w)-eq0] then Time=$(date+%F-%w-d"-1day") else Time=$(date+%F"-1day") fi mkdir$Path/$IP-p cd/&&\ tarzcfh$Path/$IP/backup_$Time.tar.gzvar/spool/cron/rootetc/rc.localetc/sysconfig/iptablesvar/www/htmlapp/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/-typef\(-name"*.log"-o-name"*.tar.gz"\)-mtime+7|xargsrm–f "back.sh"15L,628Cwritten
代码二:
viback.sh #backupimportfile,suchas/etc/rc.local/var/spool/cron/root IP=$(ifconfigeth1|sed-nr'2s#.*addr:(.*)B.*#\1#gp') Path=/backup if[$(date+%w)-eq0] then Time=$(date+%F-%w-d"-1day") else Time=$(date+%F"-1day") fi mkdir$Path/$IP-p cd/&&\ tarzcfh/backup/$IP/backup_$Time.tar.gzvar/spool/cron/rootetc/rc.localetc/sysconfig/iptablesvar/www/htmlapp/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/-typef\(-name"*.log"-o-name"*.tar.gz"\)-mtime+7|xargsrm–f "back.sh"15L,628Cwritten
上面代码只是修改了打包的情况,tarzcf/backup和定义一个变量tar$Path/结果就出现不一样的情况,第一种执行结果是:
代码一执行结果:
[root@nfs01backup]#ls
172.16.1.31backup_2017-12-23-6.tar.gzflag_2017-12-23-6.log
代码二执行结果:
[root@nfs01backup]#ls
172.16.1.31
代码一和代码二原理是一样的,但是为什么执行的结果却有差别呢?个人觉得就是环境变量的目录的开头引起的,这样让前面的环境变量失效了,打包了两次:
测试脚本
[root@nfs01scripts]#sh-xback.sh ++sed-nr'2s#.*addr:(.*)B.*#\1#gp' ++ifconfigeth1 +IP=172.16.1.31 +Path=/backup ++date+%w +'['4-eq0']' ++date+%F-d'-1day' +Time=2017-12-20 +mkdir/backup/172.16.1.31-p +cd/ +tarzcfh/backup/172.16.1.31/backup_2017-12-20.tar.gzvar/spool/cron/rootetc/rc.localetc/sysconfig/iptablesvar/www/htmlapp/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 sendingincrementalfilelist 172.16.1.31/backup_2017-12-20.tar.gz 172.16.1.31/flag_2017-12-20.log sent1128bytesreceived65bytes2386.00bytes/sec totalsizeis2960speedupis2.48 +xargsrm-f +find/backup/-typef'('-name'*.log'-o-name'*.tar.gz'')'-mtime+7
测试脚本也没有出现问题,但是不知道原因是什么,求大神能够解答,不胜感谢!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。