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

问题描述 投票:0回答:2

我正在尝试创建一个txt文件的备份,例如thisfileisabackup.txt.bak_(当前日期我还没弄明白如何打印)如何将当前日期和时间打印到txt文件的新后缀?

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

Current shell script:

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

我希望输出类似于thisababackup.txt.bak_tor 25 apr 2019 17:55:12 CEST

目前的输出只是thisababackup.txt.bak_

bash shell ubuntu
2个回答
1
投票

利用command substitution,不要忘记引用你的变量。

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

要不就:

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

0
投票

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

newextension=$(date)
© www.soinside.com 2019 - 2024. All rights reserved.