从printf

时间:2018-02-19 20:11:49

标签: bash

我经常擦除RaspberryPI的SD卡,并从最新的图片开始。为了使初始配置过程更快,我创建了一个bash脚本,可用于初始化具有许多默认值的PI。我在启动bash脚本中创建backup-database.sh bash脚本时遇到了麻烦。

此步骤应创建一个名为backup-database.sh的新文件。

printf "
#calculate the backup filename
backupFile=`date '+%Y-%m-%d--%H-%M-%S'`.sql
echo `date '+%Y-%m-%d--%H-%M-%S'`: \"Creating database backup\"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/\$backupFile

#upload the backup to dropbox
echo `date '+%Y-%m-%d--%H-%M-%S'`: \"Uploading backup to dropbox\"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/\$backupFile db/\$backupFile
" > /var/db-backups/backup-database.sh

问题是,运行上述命令后,backup-database.sh的内容如下所示:

#calculate the backup filename
backupFile=2018-02-19--19-59-11.sql
echo 2018-02-19--19-59-11: "Creating database backup"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/$backupFile

#upload the backup to dropbox
echo 2018-02-19--19-59-11: "Uploading backup to dropbox"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/$backupFile db/$backupFile

我希望backup-database.sh看起来像这样:

#calculate the backup filename
backupFile=`date '+%Y-%m-%d--%H-%M-%S'`.sql
echo `date '+%Y-%m-%d--%H-%M-%S'`: "Creating database backup"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/$backupFile
#upload the backup to dropbox
echo `date '+%Y-%m-%d--%H-%M-%S'`: "Uploading backup to dropbox"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/$backupFile db/$backupFile

我在创建backup-database.sh文件时插入了所有日期,但我需要按字面插入这些日期行,以便在执行backup-database.sh时计算日期。我试图逃避反引号,插入双倍百分比%%等等。

如何在printf命令中正确转义date命令,以便将其写入backup-database.sh文件?

2 个答案:

答案 0 :(得分:2)

你必须逃避反引号并在所有格式字符之前添加%。

printf "
#calculate the backup filename
backupFile=\`date '+%%Y-%%m-%%d--%%H-%%M-%%S'\`.sql
echo \`date '+%%Y-%%m-%%d--%%H-%%M-%%S'\`: \"Creating database backup\"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/\$backupFile

#upload the backup to dropbox
echo \`date '+%%Y-%%m-%%d--%%H-%%M-%%S'\`: \"Uploading backup to dropbox\"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/\$backupFile db/\$backupFile
" > /var/db-backups/backup-database.sh

需要使用反斜杠来逃避`字符的特殊含义,%%在printf中打印文字%,而不是将其用作格式字符串

答案 1 :(得分:2)

我建议使用here-document

cat > /var/db-backups/backup-database.sh <<'EOF'
#calculate the backup filename
backupFile=`date '+%Y-%m-%d--%H-%M-%S'`.sql
echo `date '+%Y-%m-%d--%H-%M-%S'`: "Creating database backup"
#create full backup of entire database, give it today's date
mysqldump -uroot -pNOT_MY_ACTUAL_PASSWORD --all-databases > /var/db-backups/$backupFile
#upload the backup to dropbox
echo `date '+%Y-%m-%d--%H-%M-%S'`: "Uploading backup to dropbox"
/var/db-backups/dropbox_uploader.sh upload /var/db-backups/$backupFile db/$backupFile
EOF

引用标记(EOF)可确保在实际重定向之前不会扩展行,因此无需引用它们。