LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-29-2013, 09:54 AM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686

I see quite a few issues but before commenting would you please show an example of how you would execute your script, ie exactly what you pass as the parameter which will become $1?
 
Old 11-01-2013, 10:07 AM   #17
gasric
LQ Newbie
 
Registered: Oct 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Hi and sorry i have been away for a few days and im not returning to this little project.

Right grail i have redone my code and have one issue that seems to escape me...

Code:
if [ -f $1 ]                        
then  
  restore=`grep "$1" ~/fp.txt`      
  filename=`basename "$restore"`
  echo "Type the files new location"
  read location
  location=`readlink -f`       
  mv $location/$1      
else                                    #this section below is working correctly
  restore=`grep "$1" ~/fp.txt`      
  filename=`basename "$restore"`         
  mv ~/dustbin/$1 $restore           
fi
My issue is that after i enter a location for the file to go in the debug it says this..
Quote:
+read location
~/tools #test folder
readlink -f
++ readlink -f
readlink :missing operand
+location=
mv /alpha #test file
Im guessing that i have done some thing wrong with the readlink -f and will look into that as i think if thats fixed it will give the location that i provide but even though it reads the location i enter, it just doesn't seem to want to add to the mv command?
will keep at this evening and see how i get on
 
Old 11-01-2013, 12:01 PM   #18
gasric
LQ Newbie
 
Registered: Oct 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quick update
new code change
Code:
if [ -f $1 ]
then  cd ~/dustbin
  restore=`grep "$2" ~/fp.txt`      
  filename=`basename "$restore"`
  echo "Type the files new location"
  read location
  location1=`readlink -f $location`       
  mv $2 $location1
else
new error now:
Quote:
readlink -f $location
++ readlink -f '~/tools'
+ location1=
+ mv
mv: missing file operand
Still fails to find the location even though readlink displays it, and now the move error is back.
I just cant see what im doing wrong so any help would be great please?
 
Old 11-01-2013, 07:00 PM   #19
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
I notice that you often ask your shell to run a command for you, but you hardly ever check $? after doing so to verify that the command worked.

You also seem to want to use readlink to verify that location exists. Wouldn't it be simpler to use the test function to do that? (Note that readlink -f '~/tools' will return an empty string if the file ~/tools does not exist, although $? will be 1, not 0, when it returns.)

You might try something like this:
Code:
if [ -f $1 ]
then  cd ~/dustbin
  restore=`grep "$2" ~/fp.txt`      
  filename=`basename "$restore"`
  read -p "Enter the new location for ${filename}: " location
  if [ -e "${location}" ]    # Does the proposed location exist?
  then
    if [ -d "${location}" ]  # Is the proposed location a directory?
    then  
      mv "$2" "${location}"
    else
      mv -f "$2" "${location}" # Assume "location" is a file (Should probably be checked.)
    fi
    [ $? -ne 0 ] && echo "Restoration of \"$2\" to \"${location}\" failed.
  else                                  # If we're here, $location does not exist
    dir=$(dirname "{location}")         # Find the directory
    if [ -e "${dir}" ]                  # Is the proposed location in an existing directory?
    then
      mv "$2" "${location}"
      [ $? -ne 0 ] && echo "Restoration of \"$2\" to \"${location}\" failed.
    else                                # The proposed location does not exist.
      read -N 1 -t 10 -p "\"${dir}\"} does not exist. Should it be created? (Y/n): " answer
      case "${answer}" in
        ("N"|"n") echo "Restoration of \"$2\" to \"${location}\" aborted.";;
        ("Y"|"y"|"")
           mkdir -p "${dir}" 2>/dev/null
           if [ $? -ne 0 ]
           then
             echo "\"${dir}\" could not be created. Restoration of \"$2\" to \"${location}\" aborted."
           else
             mv "$2" "{location}"
             [ $? -ne 0 ] && echo "Restoration of \"$2\" to \"${location}\" failed.
           fi;;
    fi
  fi
else
Warning: Untested

Note: I assumed that, by location, you meant a directory into which you wished to move a specific file or directory found in ~/dustbin.

Have you looked at the tar command, as I suggested above, that can do all you seem to want to accomplish and, if you wish, compress the files in what would be a ~/dustbin.tgz file instead of a directory? There's little point in reinventing the wheel unless this is just a coding exercise for you.
 
Old 11-02-2013, 02:49 AM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
You still have not answered my question from post #16 on how you call your script. If there are multiple ways please show the different ones.
 
Old 11-02-2013, 06:28 AM   #21
gasric
LQ Newbie
 
Registered: Oct 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Here's a example:
Quote:
restore <filename>

alpha is a blank file

#restore alpha <--- I use this one but can use the other
or
#restore.sh alpha
Hope that helps

Last edited by gasric; 11-02-2013 at 06:30 AM.
 
Old 11-02-2013, 10:05 AM   #22
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Ok so questions:

1. What is the value of $2 if you only pass the filename to the script?

2. readlink still serves no purpose. It tests for nothing, as PT pointed out, and allows you to create paths that do not exist

3. you call basename on the line returned from fp.txt, which is the name of the original so you have potentially gained nothing

4. step 3 also shows the issue that if the file name you pass in (alpha in your example) has multiple file names or directories containing that name in fp.txt then they will all be returned and hence you may not get the one you wanted

As you can see a number of issues and you current code still does not seem to remove the data from fp.txt.
It would also help us to help you to show us what you pass to your remove script and what that does?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
how do i write a bash script to restore deleted files to original location? BrinkofMadness Linux - Newbie 8 12-05-2010 07:04 PM
[SOLVED] my bash file is deleted , trying to restore from kernel arguments miteshpant Linux - Newbie 9 09-25-2009 01:19 PM
Bash script help for dummy. need to monitor and restore my adsl connection default Programming 10 07-09-2004 05:10 PM
How to focus / restore / min / max windows with bash ? peb Linux - Newbie 2 02-29-2004 02:21 AM


All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration