<分区>
上一个主题 - https://stackoverflow.com/questions/40808975/overwrite-script-in-bash-shell
我正在做一个大学项目,我们必须创建一个带有删除和恢复选项的回收站。我已经完成了这两个部分,并且恢复选项有效(大部分)。它将已删除文件的位置保存在一个名为 FilePaths 的 txt 文件中,作为将文件恢复到其原始位置时的引用。
我知道将文件位置的副本保存到此文件中会导致问题,因此我必须在 bash 脚本中创建一行以从文本文件中删除特定文件位置字符串。
我知道 sed 可以提供帮助,我可以操纵它来做任何我想做的事情,但问题是它似乎根本没有做任何事情。我什至使用一个简单的“test2”文本文件对其进行了测试,其中包含单词 hello。
sed '/hello/d' test2
这绝对没有任何作用。我已经用双引号试过了,但仍然一无所获,它根本没有从我的 test2 文件中删除“hello”
就恢复功能而言,这是我目前拥有的
#!/bin/sh
#navigate to the Recycle_Bin directory
cd $HOME/Recycle_Bin
#take the string from FilePaths and save it to restore
restore="$(grep "$1" "$HOME/FilePaths")"
#take the string saved in restore and use it as the file's path
filename="$(basename "$restore")"
#remove the string matching restore within FilePaths
sed '/$restore/d' $Home/FilePaths
#set location
location="$(readlink -f "$location")"
#move file to original location
mv -i "$1" "$(grep "$1" "$HOME/FilePaths")"
谁能想出 sed 不起作用的任何原因?我已经搜索了几个小时但找不到任何答案,我可以使用 awk 但我觉得 sed 会使我的代码更简单