4
#!/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

1 に答える 1

7

変数名にはアンダースコアが含まれる場合があるため、そのままの変数名の直後にアンダースコアを使用することはできません。あなたの場合、実際$oldfile_には宛先ファイル名に(未定義の)変数を使用しているため、新しい名前は「空の文字列+日付」として構築されます。変数名を中括弧で囲みます

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

名前の変更は期待どおりに機能するはずです。

于 2013-07-11T15:54:36.090 に答える