1. #1

    help with linux restore script

    ok what i need to do is

    restore <filename> - This script should move the file called <filename> back to its original directory without requiring any further user input.
    If a file of that name already exists at the restore location, the script prompts the user to select an appropriate alternative action.

    heres what i have so far

    Code:
    #!/bin/sh
    if [ "$1" == "-n" ]
    then
      cd /root/michael/trash
      restore`grep "$2" /root/michael/store`
      filename=`basename "$restore"`
      echo "Where would you like to save the file?"
      read location
      location1=`readlink -f "$location"`
      mv -i $filename "location1"/filename
    else
      cd /root/michael/trash
      restore=`grep "$2" /root/michael/store`
      filename=`basename "$restore"`
      mv -i $filename "location1" $location
    fi
    so when i do restorn -n it ask me where i want to restore the file to, which works fine

    and if i do restore without -n it should restore the file to the original place but when i do for example
    restore test1.txt i get the error
    Code:
    mv: missing destination file operand after `'
    any idea what i need to fix it

  2. #2
    I am Murloc! Cyanotical's Avatar
    Join Date
    Feb 2011
    Location
    Colorado
    Posts
    5,286
    the problem is in this line:

    mv -i $filename "location1" $location

    it might be that it's trying to read location1 from your input which should only come up under condition -n

    i think you need to redefine location1 under the else conditions

    Synthaxx would be better at this than me

    i7-4790K | Z97 Class. | 8GB DDR3-2133 | GTX-690 Quad SLI | RAIDR | 512GB Samsung 830 | AX1200 | RV05
    Dell U2711 | Ducky Shine3 YoS | Steelseries Sensei | Xonar Essence One | KRK RP8G2s

  3. #3
    The problem is that your variables are not being created.

    Also why are you writing this in SH when you can write it in BASH or K-Shell with a lot easier attributes and variables.

    Also I'm not the best at SH but in BASH you don't need a ` in before GREP.

    I think what your trying to do is this (in psudeo)

    If 1 is entered, then save a file in a location that you specify, else restore / go to another location.

    Where I have a problem with your code is :
    echo "Where would you like to save the file?"
    read location
    If your variable is not defined how is it going to read the location. The only place your define a location is cd /root/michael/trash which is not entered correctly. It should be this (again in BASH): alias proj="cd /home/tree/projects/java"

    To a script that is considered definable and can be used in a "location" variable.

  4. #4
    Quote Originally Posted by Moonsorow View Post
    The problem is that your variables are not being created.

    Also why are you writing this in SH when you can write it in BASH or K-Shell with a lot easier attributes and variables.

    Also I'm not the best at SH but in BASH you don't need a ` in before GREP.

    I think what your trying to do is this (in psudeo)

    If 1 is entered, then save a file in a location that you specify, else restore / go to another location.

    Where I have a problem with your code is :


    If your variable is not defined how is it going to read the location. The only place your define a location is cd /root/michael/trash which is not entered correctly. It should be this (again in BASH): alias proj="cd /home/tree/projects/java"

    To a script that is considered definable and can be used in a "location" variable.
    im only started learning how to script, and your post kinda confuses me,

    how would i fix it using SH?

  5. #5
    Titan
    Join Date
    Feb 2008
    Location
    Rotherham, England/UK
    Posts
    13,162
    I generally don't deal with scripting outside of Batch (and even that's a little hazy after all these years) and i've never dealt with Linux. Luckily, it looks very similar to batch, so i'll give it a shot, but you'll potentially need to correct folder locations and check the syntax.

    This is what i assume is the problem. As others said, you've not created the variable "location1". Typically in 'system scripting' language, variables are assigned like this;
    Code:
    variablename=variable_data
    To use the variable, something like this;
    Code:
    echo "$variablename"
    For example, if you wanted to set a location variable called 'location1' to '/root/michael/store' (this is just an assumption), you'd have this;
    Code:
    location1=/root/michael/store
    and to echo that to screen;
    Code:
    echo "$location1"
    To use a variable in any way, it's literally adding a dollar sign ($) before the name of the variable you wish to use.

    This is a basic definition and is fairly universal. Programming isn't much different, but i won't go into that here as it's not the subject.

    I'd guess the second part (after 'else' is where the problem lies). Step by step, you're;
    1 - Setting the directory
    2 - Calling the restore command with '$2'
    3 - Setting the filename variable to `basename "$restore"`
    4 - Attepting to move/mv the file specified in step 3 from "location1" to $location, telling it to prompt before overwrite

    To me, there's one main problem; "$location". You've not specified in the script what that is and you're trying to move files there. Usage of mv according to research shows something like this;

    Code:
    mv [flags] Source Destination
    Traditionally, you might enclose source and destination in speech marks ("Source" "Destination") unless it's a variable to form something like this;
    Code:
    mv -i $filename $location
    From that last line of the script, it's like it's looking for the directory "location1" inside $filename.

    I'm not sure if this will fix it (and i'm not responsible if it messes anything up or simply doesn't work, use at your own risk);

    Code:
    if [ "$1" == "-n" ]
    then
      cd /root/michael/trash
      restore`grep "$2" /root/michael/store`
      filename=`basename "$restore"`
      echo "Where would you like to save the file?"
      read location
      location1=`readlink -f "$location"`
      mv -i $filename "location1"/filename
    else
      location=`/insert/your/location/here`
      cd /root/michael/trash
      restore=`grep "$2" /root/michael/store`
      filename=`basename "$restore"`
      mv -i $filename "location1" $location
    fi
    You might also want to look at that last mv line in the original script you posted. Is there not meant to be a slash somewhere in there? I'm not sure whether you want to move from $filename/"location1" to $location, or from $filename to "location1"/$location. That alone would probably cause the script to bug out, but combined with the variable(s) you were missing, i'd hazard it's simply not knowing what you're intending to actually move.

    Again, it's all guesswork, but i believe that's the issue.
    Last edited by Chronalis; 2012-11-29 at 04:33 AM.
    [...]

  6. #6
    sadly i tried what you suggested even put a / in with $filename/"location1" to $location, or from $filename to "location1"/$location. but still came back with an mv error

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •