2014-12-04 207 views
0

我有一個shell腳本,我試圖每隔幾天運行一次複製.sql數據庫文件並將它們移動到/ $ now /所附的指定文件夾中。腳本執行完美,但我得到一個cp:無法創建常規文件'/ path/to/dir/$ now /':沒有這樣的文件或目錄。

我知道這些文件夾存在,因爲它出現在我'ls -ltr'目錄中。

我的所有權限都是可執行的和可寫的。這讓我困惑了大約一個星期,而我只是不能把它放在手指上。

這裏是我的代碼:

#!/bin/bash 
BACKUP_DIR="/backups/mysql/" 
FILE_DIR=/dbase/files/ 
now=$(date +"%Y_%m_%d") 

# setting the input field seperator to newline 
IFS=$'\n' 

# find db backups and loop over 
for file in $(find ${FILE_DIR} -maxdepth 1 -name "*.sql" -type f -exec basename {} \;); do 
    # create backup directory: 
    mkdir -p "${BACKUP_DIR}${file%.sql}/${now}" 

    # copy file over   
    cp "${FILE_DIR}${file}" "${BACUP_DIR}${file%.sql}/${now}/" 
done 

提前感謝!

更新:

錯誤我得到:

+ mkdir -pv /backups/mysql/health/2014_12_04 
+ cp /dbase/files/health.sql health/2014_12_04/ 
cp: cannot create regular file 'health/2014_12_04/': No such file or directory 

這是發生對已創建

+0

圍繞該行的代碼段是什麼? (包括任何相關變量的賦值行。) – 2014-12-04 14:29:18

+0

我已將代碼添加到原始問題中。 – Mark 2014-12-04 14:35:44

+0

如果你添加了'-v'到那個'mkdir'行,你會看到它輸出它正在創建目錄嗎?將'set -x'添加到腳本的頂部,看看當它失敗時顯示您正在運行的是什麼。 – 2014-12-04 14:41:13

回答

0

存在錯誤的所有9個目錄是一個錯字。我錯過了cp行上的$ BACKUP_DIR中的'K'。以下是正確的代碼:

#!/bin/bash 
BACKUP_DIR="/backups/mysql/" 
FILE_DIR=/dbase/files/ 
now=$(date +"%Y_%m_%d") 

# setting the input field seperator to newline 
IFS=$'\n' 

# find db backups and loop over 
for file in $(find ${FILE_DIR} -maxdepth 1 -name "*.sql" -type f -exec basename {} \;); do 
    # create backup directory: 
    mkdir -p "${BACKUP_DIR}${file%.sql}/${now}" 

    # copy file over   
    cp "${FILE_DIR}${file}" "${BACKUP_DIR}${file%.sql}/${now}/" 
done 
+0

是的,這是正確的。 – Mark 2014-12-05 15:16:08