2013-07-11 47 views
1
#!/bin/bash 
while read server <&3; do #read server names into the while loop  
    if [[ ! $server =~ [^[:space:]] ]] ; then #empty line exception 
    continue 
    fi 
    echo "Connecting to - $server" 
    #ssh "$server" #SSH login 
    while read updatedfile <&3 && read oldfile <&4; do  
    echo Comparing $updatedfile with $oldfile 
    if diff "$updatedfile" "$oldfile" >/dev/null ; then 
     echo The files compared are the same. No changes were made. 
    else 
     echo The files compared are different. 
     # copy the new file and put it in the right location 
     # make a back up of the old file and put in right location (time stamp) 
     # rm the old file (not the back up) 
     #cp -f -v $newfile 

     # **************************** 
     mv $oldfile /home/u0146121/backupfiles/$oldfile_$(date +%F-%T) 
     # **************************** 
    fi 
    done 3</home/u0146121/test/newfiles.txt 4</home/u0146121/test/oldfiles.txt 
done 3</home/u0146121/test/servers.txt 

*之间的行是我遇到脚本时遇到的问题。它会输出文件的日期和文件名。它只是使用日期。我希望它能同时做到这一点。移动文件并将日期添加到文件名

回答

4

变量名可能包含下划线,所以在裸露变量名后面不能有下划线。在你的情况下,你实际上在目标文件名中使用了一个(未定义的)变量$oldfile_,这样新的名字被构造为“空字符串+日期”。将变量名称放在大括号内

mv $oldfile /home/u0146121/backupfiles/${oldfile}_$(date +%F-%T)

并且重命名应该按预期工作。

相关问题