尝试创建将重命名 txt 文件的后缀的 .txt 文件的备份



我也在尝试创建txt文件的备份,例如thisfileisabackup.txt.bak_(当前日期我还没有弄清楚如何打印)如何将当前日期和时间打印到txt文件的新后缀中?

我尝试更改 cp $myfile $place -S .bak_ 之间的后缀;日期和 cp $myfile $place -S .bak_$newextension,我在其中放置了 newextension=;日期。日期在终端中打印出来,而不是与.bak一起保存为新后缀

当前外壳脚本:

#!/bin/bash
echo "File to backup"
read myfile
#checks if $myfile exists
if [ -f "$myfile" ] ; then
echo "Where do you want the backup stored"
read place
newextension=;date
#stores the backup to x place (place is defined by read place)
cp $myfile $place -S .bak_$newextension
else echo "$myfile does not exist"

我希望输出类似 thisisabackup.txt.bak_tor 25 四月 2019 17:55:12 CEST

当前输出仅为thisisabackup.txt.bak_

使用命令替换,不要忘记引用变量。

newextension="$(date)"
cp -- "$myfile" "$place" -S ".bak_$newextension"

或只是:

cp -- "$myfile" "$place" -S ".bak_$(date)"

您没有正确使用命令替换。正确的格式是这样的:

newextension=$(date)

最新更新