Restore bash script
Hi im wondering why this script pumps out the error message : move cannot stat `(file being moved)`: no such file or directory
Here's the code im using: Code:
if [ $1 ] |
I see several issues:
1. No then keyword for if 2. I would hazard a guess that the space between ~/dustbin/ and "$1" would be a bit of an issue. 3. The following will rename everything to the string "restore" in the current directory: Code:
mv ~/dustbin/ "$1" "restore" |
Hi sorry i have just read my post and have not really explain it very well!
Here's a better try: I'm trying to create a restore script that restore any deleted files from the dustbin directory. here is a new code layout I think is better but still pumps out an error, could you point out whats wrong with my code and error please? Code:
#!/bin/sh error: Quote:
Am i missing code? Is the $location correct? thanks for your time anyway! :) |
Have a close look at your output ... you have not used any back ticks to do your process substitution, hence all variables are blank
|
I'm not very good at this but i think what your trying to say is there are no results happening for restore, filename and basename.
Sorry but if you could give me a few more pointers that would be great? |
Only changed a few things... removed whitespace and added back ticks.
Code:
#!/bin/sh + `[` alpha `]` + cd /root/dustbin grep $1 ~/fp.txt ++ grep alpha /root/fp.txt + location1=` ./alpha ./alpha` basename "$restore" ++ basename `./alpha ./alpha + filename=alpha + mv alpha alpha `./alpha ./alpha mv: target /alpha` is not a directory So they are returning the information thats needed i think but is it thinking that im trying to restore a directory, not really sure. Right this is what it says in the file called fp.txt ./alpha So what im thinking here is that when i delete the file, its storing the file path incorrectly in the fp.txt. Here is the code i use to delete the files: Code:
if [ -f $1 ] |
Quote:
If your file is stored in ~/dustbin then I assume what you want is: mv "~/dustbin/$1" "$restore" |
Well I see a few problems with the remove as well, so we will look at it first:
1. You call basename on the file name passed in, is this in case they use the path to the file as well as just the file name on its own? 2. echo not required as the output from find can be directly redirected into the file 3. you call find to be run from wherever you are ... what if you are at the root top level?? This means it will find all (assuming you have access, note you do not have any provision for if you do not) copies with the same name and echo them to fp.txt ... is this really what you want? can you guarantee that every single file on your system has a unique name?? 4. you move the file as it was named and passed to your script, but above we assume the user could pick a random file name so you may not be in the directory where the file is ... this will cause an error Back to restore script, maybe you can tell me what you think the following line would do? Code:
mv $1 $filename "$location1" $location |
Well, the find command requires a root directory, and you have not specified one.
Try find $HOME -name $file But what's the point of the find command? Before you invoked it, you already knew the $1 was a file, and that it existed, so what you want is, probably, something like this: Code:
#!/bin/bash Code:
$ ls # Current directory contents
|
Quick update!
Hi michaelk, yeah i had that in there before and have changed it back but thanks for the input buddy! :) Right grail, cheers for your help buddy so far and i have read what you posted and have taken it on board. What you have said makes a lot of sense but here's more for you! haha New delete code: Code:
if [ -f $1 ] The code above does work with no errors popping up and stores the full file path of a deleted file correctly. Answer to Q3. I cant guarantee these files will be unique but im just starting small for now and then moving on as i get a bit more knowledgeable about scripting, so its more of can i do it but simply. Hope that helps better understand my madness? Answer to Q4. Well i was trying to move the file back by calling each part, as in "file name" "it's stored location" send to "location". To be honest as i have been typing this out explaining what i wanted, it makes no sense so....Answered my own question of why it didn't work to start with. >_< So heres my new code: Code:
#!/bin/sh line 4: alpha: command not found usage: grep [option]... pattern [file].... line 4: grep `"$1" ~/fp.txt` Why is this happening? |
WOW please forget the last question about LINE 4: grep...
I removed it as it was not needed and feel a bit stupid after not just checking what was happening!:/ |
Hi PTrenholme, sorry didn't see your post till after I posted back.
Thanks for the input and i have read what you have done and sort of understand what you are trying to do but thats very complex for me right now, but may not be for you. Thanks very much and will revert back to it in the near future. :) |
Referring to your remove script, how would you call it? I am specifically concerned with the reference to the basename function:
See this example: Code:
./remove.sh /home/gasric/tmp_file Code:
$ readlink -f tmp_file If you are not root this will cause problems and it will be in the wrong place. wrt to restore script, once the file has been restored you do not remove its information from fp.txt. This means you could use the same file name again and will now get errors of no such file. Something to think about From a programming perspective, I try to do any new tasks in the shell first and then simply copy that exact line into a script. From there you can replace variables and create functions as you require. One thing to be wary of that most forget as you do it visually, is how to check a file exists both to be removed, be restored and is not already been restored. Keep going though, you are getting somewhere :) As for PT's version, this is a replacement for your remove option so if you take it a line at a time it is not complicated, the large amount of printf statements is to make it clear when you have success or failure (always a good habit) @PTrenholme - the below is not correct. You can call find without a root directory and it will simply default to your current location. Admittedly this is not a good habit :) Quote:
|
Hey grail, right i have read up on readlink -f and it does work as it saves the full path and i have adapted my code to amend the fp.txt file when a file is restored from it so there will be no duplications stored in the fp.txt file.
Now i need your help once again as i have now started to request the user to input a file location if the file with the same name already exists in the location its trying to restore to. Code:
if [ "$1" == ""] I will keep at it for now and i hope i can work it out but its beating me right now lmao! ;( |
Quote:
Code:
if [[ -z $1 ]]; |
All times are GMT -5. The time now is 12:05 AM. |