使用路径将日志文件恢复到原始位置(restoring a file to their original location from a log file with paths)

编程入门 行业动态 更新时间:2024-10-25 03:23:16
使用路径将日志文件恢复到原始位置(restoring a file to their original location from a log file with paths)

我已经制作了一个脚本来将用户指定的文件移动到垃圾箱中,并使用它们的原始路径创建日志文件。 现在我想创建一个脚本,用户只需输入文件的名称即可将其恢复到之前的位置,我无法弄清楚。 以下是目前的代码:

删除脚本:

#!/bin/sh #checks if the user has entered anything. #if not displays message. if [ $# -eq 0 ]; then #reads the number of characters echo "Usage: del <pathname>" >&2 exit 2; fi #location of the dustbin dustbin="$HOME/dustbin" paths="$HOME/Paths" if [[ ! -d $dustbin ]]; then #checks if the dustbin exists mkdir $dustbin else [[ -p $dustbin ]] #if dustbin exists does nothing fi #creates a Paths folder to store the original location of file(s) if [[ ! -d $paths ]]; then #checks if the Paths folder exists mkdir $paths else [[ -p $paths ]] #if Paths folder exists does nothing fi #gets just the file name for file in "$@"; do #reads all the arguments if [[ -e $file ]]; then #checks if the file name exists #moves the file(s) to the dustbin and writes the orginal file path to the paths.txt file. find $file >> $HOME/Paths/paths.txt && mv "$file" "$dustbin" echo "Deleting file(s) $file" else echo "The file $file doesn't exist." fi done

恢复脚本:使用此功能,我需要搜索垃圾箱中的文件,将文件名与具有文件原始路径的路径文本文件进行匹配并移至所述路径。

#!/bin/sh #checks if the user has entered anything. #if not displays message. if [ $# -eq 0 ]; then echo "Usage: restore <File name>" >&2 exit 2; fi #checks if the file paths.txt exist paths="$HOME/Paths/paths.txt" if [[ ! -f $paths ]]; then #checks if the Paths file exists echo "The log file paths.txt doesn't exist. Nothing to restore" fi #takes the user input checks if the dustbin exists. for file in "$@"; do if [[ ! -d dustbin ]]; then echo "dustbin doesn't exist" else cd $HOME/dustbin fi #looks for the user specified file. if [[ ! -e $file ]]; then echo "File $file doesn't exist" else #restores the file to the original location restore="grep -n '$file' $paths" #no idea how to do it mv $file $restore fi done

这部分我不知道该怎么做。 我需要它从paths.txt中读取$ file中的用户输入,并使用该存储路径将$文件从垃圾箱移动到存储在paths.txt文件中的文件路径。

#restores the file to the original location restore="grep -n '$file' $paths" #no idea how to do it mv $file $restore

I have made 1 script to move user specified files to the dustbin and create a log file with their original paths. Now I want to create a script that the user would only have to input the name of the file to restore it to where it was before and I cannot figure that out. Here is the code so far:

delete script:

#!/bin/sh #checks if the user has entered anything. #if not displays message. if [ $# -eq 0 ]; then #reads the number of characters echo "Usage: del <pathname>" >&2 exit 2; fi #location of the dustbin dustbin="$HOME/dustbin" paths="$HOME/Paths" if [[ ! -d $dustbin ]]; then #checks if the dustbin exists mkdir $dustbin else [[ -p $dustbin ]] #if dustbin exists does nothing fi #creates a Paths folder to store the original location of file(s) if [[ ! -d $paths ]]; then #checks if the Paths folder exists mkdir $paths else [[ -p $paths ]] #if Paths folder exists does nothing fi #gets just the file name for file in "$@"; do #reads all the arguments if [[ -e $file ]]; then #checks if the file name exists #moves the file(s) to the dustbin and writes the orginal file path to the paths.txt file. find $file >> $HOME/Paths/paths.txt && mv "$file" "$dustbin" echo "Deleting file(s) $file" else echo "The file $file doesn't exist." fi done

restore script: With this I need to search for the file in the dustbin, match the file name to the paths text file that has the files original path and move to the said path.

#!/bin/sh #checks if the user has entered anything. #if not displays message. if [ $# -eq 0 ]; then echo "Usage: restore <File name>" >&2 exit 2; fi #checks if the file paths.txt exist paths="$HOME/Paths/paths.txt" if [[ ! -f $paths ]]; then #checks if the Paths file exists echo "The log file paths.txt doesn't exist. Nothing to restore" fi #takes the user input checks if the dustbin exists. for file in "$@"; do if [[ ! -d dustbin ]]; then echo "dustbin doesn't exist" else cd $HOME/dustbin fi #looks for the user specified file. if [[ ! -e $file ]]; then echo "File $file doesn't exist" else #restores the file to the original location restore="grep -n '$file' $paths" #no idea how to do it mv $file $restore fi done

this part I have no idea how to do. I need it to read the user input in $file from the paths.txt and use that stored path to move the $file from the dustbin to the file path stored in the paths.txt file.

#restores the file to the original location restore="grep -n '$file' $paths" #no idea how to do it mv $file $restore

最满意答案

所以,我想你会想把文件移回原来使用mv的地方 。

mv "dustbinPath/$file" "orginalPath/$file"

这会将其从垃圾箱路径移到原始路径。

编辑:

如果你想grep的路径文件,你可以设置一个变量的命令的输出,如:

originalPath=$(grep 'what_to_grep' file_to_grep.txt)

在你这样做之后,在上面的mv中适当地使用它(不管文本文件是否包含该文件名)以将其移出。

你可以在这里阅读更多关于设置变量到命令输出的信息 。 但是如果有多条线路可能会遇到问题。

So, I think you will want to move the file back to where it originally was using mv.

mv "dustbinPath/$file" "orginalPath/$file"

This will move it from the dustbin path to the originalPath.

EDIT:

If you want to grep the path file for it, you can set a variable to the output of a command like:

originalPath=$(grep 'what_to_grep' file_to_grep.txt)

After you do that that use it in the mv above appropriately (whether the text file contains that file name or not) to move it out.

You can read more about setting a variable to the output from a command here. You might have problems if there are multiple lines that have it however.

更多推荐

file,paths,fi,dustbin,$file,电脑培训,计算机培训,IT培训"/> <meta name="d

本文发布于:2023-08-07 00:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   原始   位置   文件   日志

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!