My coursework involves writing a linux script to 'restore' files from a pseudo trash folder. I've done this, and it looks like it should work, but it doesn't. Instead, I get an error that says:

sed: -e expression #1, char 2: extra characters after command

but I can't see why, since I don't use the -e expression anywhere in my code. However, my brain's fried from staring at code for so long that it might just be a silly mistake that I can't see. Any help would be appreciated greatly!

Could be wrong, but I think you may have to move your name=direname "$restore" in by 2 spaces to keep within the if/fi
–
JoshDMNov 28 '12 at 17:42

Using the example command you gave in a comment of one answer, the sed is called as sed "NewFile02" ~/my-documents/mydir01/locations whether if is true or not. In which N is a valid command, and it does not take argument, the following e confuses sed. That's where the error comes from. Based on that, I think you don't really understand what sed is, or you wouldn't give it such argument.
–
livibetterNov 28 '12 at 20:33

1 Answer
1

How exactly are you invoking your shell script? The values of $1 and $2 are used by sed, and it looks like that might be where the problem is occurring.

Based on the comments from the original poster, it appears that the desired end goal is to store the location of the original file in ~/my-documents/mydir01/locations, and later retrieve it from there when the file needs to be restored.

One way to do this would be by using a tuple (such as a key-value pair) to store and retrieve this information. For instance, if the original file name is X and the new file name is Y, at the time of the change, you would add "X Y" to the locations file. Later, when you want to restore the file, you would use either X (or perhaps Y) to pick this line from the location file (hint: use grep), and then use awk or perl (example: awk '{print $1}') to pick the first part of the line. Doing this gives you X, which is what you want, since X is the original name of the file.

I'm just invoking it with 'restore [filename]'
–
user1860676Nov 28 '12 at 17:43

Can you share the exact command you are using it, including a specific value for filename that is causing the problem? [Unlike as in python, spacing does not matter in shell scripts.] One other question. One uses sed to perform a pattern edit. What exactly are you accomplishing with <sed "$1" ~/my-documents/mydir01/locations>?
–
Ram RajamonyNov 28 '12 at 18:01

the command I was testing was: restore NewFile02 The sed lines are supposed to retrieve the file's previous filepath and name from 'location' (text file) I tried using grep instead, but I couldn't get that to work either.
–
user1860676Nov 28 '12 at 18:17

So I see several problems with the solution you're employing and the problem you are trying to solve.
–
Ram RajamonyNov 28 '12 at 18:27

So I see several problems with the solution you're employing and the problem you are trying to solve. When invoked with "-n", I think you are trying to record (in .... locations) the original name of the file that is being moved. Later, you restore the file from there. Can you elaborate on why you are using sed to do this?
–
Ram RajamonyNov 28 '12 at 18:39