LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-09-2013, 04:37 PM   #1
NashChap
LQ Newbie
 
Registered: Oct 2013
Posts: 1

Rep: Reputation: Disabled
Bash program, grep returning No such file or directory


I am writing a recycle bin program for work (unix training) where it has to mimic the rm command but instead of deleting, move into a new folder named delete, whilst adding on the iode of the file/dir onto the end and recording the original name and path in a separate file named .restore.info for when the file needs to be restored.


Code:
  1 #!/bin/bash
  2
  3 WHERE=$(grep $1 ~/.restore.info | cut -d: -f2)
  4
  5 ONAME=$(grep $1 ~/.restore.info | cut -d_ -f1)
  6
  7
  8 echo " this is the home $HOME"
  9 echo " the original location $WHERE "
 10 #echo " the original name $ONAME"
 11
 12 restore(){
 13 #if the file does NOT exist
 14 if [ ! -f ./safe_rm_restore/$1 ]
 15 then
 16         echo "$1 not found!"
 17
 18 #if the file DOES exist
 19 else
 20         print "Do you want to overwrite $1 ?"
 21         read answer
 22
 23 answer=$(echo $response | cut -c1)
 24
 25 if [[ $answer = "y" || $answer = "Y" ]]
 26 then
 27
 28         mv $HOME/deleted/$1 $WHERE
 29
 30         fi
 31
 32         if [[ $answer = "n" || $answer = "N" ]]
 33
 34         then
 35
 36         echo "file has not been restored"
 37
 38         fi
 39
 40 fi
 41
 42 }
this is my code for the restore portion of the task but whenever i run it it nothing returns

i thought it may be something to do with my grep so i did grep ~/.restore.info PRACTICE_23116842 to see if it works and it just returned "No such file or directory", but i can actually see the file manually....

i have no idea what im doing wrong haha

thanks in advance for the help

Last edited by NashChap; 10-09-2013 at 04:39 PM.
 
Old 10-09-2013, 04:55 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
In your manual command you have the grep in wrong order:
grep ~/.restore.info PRACTICE_23116842
It should be:
grep PRACTICE_23116842 ~/.restore.info

In your code you have $1 for file name - are you passing that in on invocation (i.e. restore.sh PRACTICE_23116842)? You should be aware that positional parameters are relative to the last command so while your command:
WHERE=$(grep $1 ~/.restore.info | cut -d: -f2)
Might work with the file name PRACTICE_23116842 being $1 the next command might NOT work.
The usual practice is to set the passed in parameters to something unique within the program e.g.

DELFILE=$1
WHERE=$(grep $DELFILE ~/.restore.info | cut -d: -f2)
ONAME=$(grep $DELFILE ~/.restore.info | cut -d_ -f1)

Also you should be aware that "where" is a keyword in many things so you really ought to use something different (i.e. LOCATION) just to avoid confusion.
 
Old 10-09-2013, 09:52 PM   #3
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
well i dont understand the complete thing.. but it is always advisable to store arguments in a variable & then to proceed further... and this doesnt looks like a problem here..

where does it says an error:

Code:
bash -x [filename.sh]

Quote:
print "Do you want to overwrite $1 ?"
21 read answer
22
23 answer=$(echo $response | cut -c1)
Also.. the above looks incorrect to me!!! it should be something like:
Code:
response=$(echo $answer |cut -c1)
if [[ $response = "y" || $response = "Y" ]]
then
do_something
else
nested_if_if_required
do_something
fi

Quote:
grep ~/.restore.info PRACTICE_23116842
it should be: $grep [pattern] [filename/location]

Last edited by SAbhi; 10-09-2013 at 09:59 PM.
 
Old 10-10-2013, 03:07 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Maybe you could show the call to the restore function as well seeing it is being passed a parameter.

I have also noticed a distinct lack of quoting ... are we to assume that all files being moved to the trash will never contain whitespace or other unusual characters?
 
  


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
[SOLVED] How do i grep file names in current directory? Rogue45 Linux - Newbie 10 09-07-2012 12:38 PM
Bash file using ls -l and grep Pocho Linux - Newbie 18 08-13-2010 09:07 AM
Grep in bash script returns "No such file or directory", works manually gizza23 Programming 7 02-25-2010 04:37 PM
How to configure 'grep' to ignore 'No such file or directory n179911 Linux - Newbie 4 07-08-2009 04:14 AM
How to diagnose cause of grep returning "sense key Medium Error" on a particular file bcrozier Linux - Software 5 11-20-2008 09:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:17 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
Open Source Consulting | Domain Registration