修改文件名的Linux脚本cp

时间:2021-12-18 16:25:50

I'm trying to make a sort of recycling bin. I have a delete function which sends the selected file to the recycling bin and adds the location of the directory it was stored in to the file. The problem is when I obtain the location from the script using tail. Although the script works, it renames the file to tail. Could anyone explain why the cp is renaming the file? Here is a snippet from where I believe the problem is:

我想做一个回收箱。我有一个delete函数,它将选择的文件发送到回收桶,并将它存储的目录的位置添加到文件中。问题是当我使用tail从脚本获取位置时。虽然脚本可以工作,但它将文件重命名为tail。有人能解释一下cp为什么要重命名文件吗?以下是我认为问题所在的一个片段:

destination=(tail $1 -n 1)
cp ~/Recycling/$1 $destination 
rm ~/Recycling/$1

Thanks

谢谢

3 个解决方案

#1


2  

You need a $ before the parenthesis:

在括号之前你需要一个$:

destination=$(tail $1 -n 1)
cp ~/Recycling/$1 $destination 
rm ~/Recycling/$1
sed -i '$d' $destination # this removes the last line from the file

#2


1  

You're missing a $ before the parens:

你少了一美元

destination=$(tail $1 -n 1)

#3


0  

You would want

你希望

$(tail $1 -n 1)

or

`tail $1 -n 1`

#1


2  

You need a $ before the parenthesis:

在括号之前你需要一个$:

destination=$(tail $1 -n 1)
cp ~/Recycling/$1 $destination 
rm ~/Recycling/$1
sed -i '$d' $destination # this removes the last line from the file

#2


1  

You're missing a $ before the parens:

你少了一美元

destination=$(tail $1 -n 1)

#3


0  

You would want

你希望

$(tail $1 -n 1)

or

`tail $1 -n 1`