bash
2013-10-30 237 views 1 likes 
1

我想寫,做以下簡單的備份腳本:Bash腳本說找不到命令?

#!/bin/bash 
#backupScript 

mysqldump mysql -uxxxxx -pxxxxx Database1 Database2 | gzip > "MainWordPress.gz" 

time_stamp = `date +%Y%m%d.%H%M` 
file_name = 'backup-{$time_stamp}.tar.gz' 
sources = 'DIR/ MainWordPress.gz' 
tar -cvf file_name sources 

rm MainWordPress.gz 

cp file_name some/destination 

但問題是,它不喜歡:

./runBackup.sh: line 6: time_stamp: command not found 
./runBackup.sh: line 7: file_name: command not found 
./runBackup.sh: line 8: sources: command not found 
tar: sources: Cannot stat: No such file or directory 
tar: Exiting with failure status due to previous errors 

我深感困惑......

+0

應該的'tar -cvf FILE_NAME sources'也許是的'tar -cvf 「$ FILE_NAME」 $ sources'? (類似於'cp'。)'$ time_stamp'不會在''backup - {$ time_stamp} .tar.gz''(單引號)中展開。 Bash/shell腳本教程的時間? – Biffen

回答

6

刪除等號周圍的空間:

#!/bin/bash 
#backupScript 

mysqldump mysql -uxxxxx -pxxxxx Database1 Database2 | gzip > "MainWordPress.gz" 

time_stamp=$(date +%Y%m%d.%H%M) 
file_name="backup-${time_stamp}.tar.gz" 
sources='DIR/MainWordPress.gz' 
tar -cvf file_name sources 

rm MainWordPress.gz 

cp file_name some/destination 
+0

你更快^^ – Asenar

+0

嘿嘿......我!也爲你+1。 – opalenzuela

1

刪除空間wetween變量和=

#!/bin/bash 
#backupScript 

mysqldump mysql -uxxxxx -pxxxxx Database1 Database2 | gzip > "MainWordPress.gz" 

time_stamp=`date +%Y%m%d.%H%M` 
file_name='backup-{$time_stamp}.tar.gz' 
sources='DIR/ MainWordPress.gz' 
tar -cvf file_name sources 

rm MainWordPress.gz 

cp file_name some/destination 
相關問題