LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-25-2013, 09:12 AM   #1
zaayu87
LQ Newbie
 
Registered: Sep 2012
Location: India
Posts: 6

Rep: Reputation: Disabled
Usage of "-f" option of grep in a script results in "Permission Denied" error


Hi,

I am trying to implement a script which searches for lines with specific patterns from a source file and writes them to a new file.
I have over 40k lines that I need to search which is tiresome to put onto the command line. Hence I listed them all in a file and passed that as an argument using the "grep -f" command.

This worked perfectly fine in the command line, but when i ran this inside a script it gives me "Permission Denied" error on the source data file.

Script is below:

#!/bin/bash

INFILE=$1
OUTDIR=$2

FILE=`basename "$INFILE"`

Path='/home/test'

grep -f "$Path/lines.txt" "$INFILE" > "$OUTDIR/$FILE"

[ $? = 1 ] && exit 1

exit 0

Error:
[/home/test]$ sh line_filter.sh Data_Feed.csv /home/test/output
line_filter.sh: line 10: Data_Feed.csv: Permission denied

Please advice what am i doing wrong here.
 
Old 07-25-2013, 11:39 AM   #2
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
You may want to try adding -F to your grep. Also I would not use Path as a variable name. Are you sure basename is acting like you think it should? You may want to echo out what it is giving you.
 
Old 07-25-2013, 12:23 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267
Quote:
Originally Posted by zaayu87 View Post
Code:
#!/bin/bash

INFILE=$1
OUTDIR=$2

FILE=`basename "$INFILE"`

Path='/home/test'

grep -f "$Path/lines.txt" "$INFILE" > "$OUTDIR/$FILE"

[ $? = 1 ] && exit 1

exit 0
Error:
[/home/test]$ sh line_filter.sh Data_Feed.csv /home/test/output
line_filter.sh: line 10: Data_Feed.csv: Permission denied
First, the variable Path gets set but is never used in the script. (not the cause of the error)

The "permission denied" error could be due to any of:
  1. File /home/test/output/Data_Feed.csv already exists, and you don't have write permission for it,
  2. You lack write permission in directory /home/test/output,
  3. You lack execute permission in /home/test/output or in one or more of its parent directories.
Directory /home/test would normally be the login directory for username "test". I trust that is what you intended.
 
1 members found this post helpful.
Old 07-25-2013, 01:45 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Actually $FILE is being used as grep's output file name.

The external basename command isn't needed though. You can use a built-in parameter substitution instead.

Code:
FILE=${1##*/}
Anyway, I agree with the above possibilities, as there's really nothing in the script itself that could be causing that kind of error. My best guess is that the user the script is running under is different from the one owning the file.

I also see another point of caution in the error message itself:

Code:
sh line_filter.sh Data_Feed.csv /home/test/output
"sh" tells me that the interpreter being used isn't bash, but the system's POSIX shell (which may indeed be bash too, but in a more restricted form). This can only happen if the file is being executed like this:

Code:
$ sh line_filter.sh file.txt outdir
When done this way the shebang at the top of the file is ignored as a comment, and the /bin/sh shell does the actual interpreting. When a script has a shebang configured for it, just execute it directly with the (relative or absolute) path preceding the name. It will then execute using that pre-configured interpreter.

Code:
$ ./scriptname.sh file.txt outdir
Not that it's really a problem in this case though, since all the syntax in the script is POSIX compliant anyway. But I suppose there's a possibility that the restricted access is related to it as well.


PS: Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
unpredictable "delete" "move to trash" or "cut" file menu option dorianrenato Linux - General 3 11-28-2011 07:41 PM
[SOLVED] du says "cannot read directory" and "permission denied", but I have rwx permissions mrtwice Linux - Software 14 04-21-2010 01:38 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 08:38 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM


All times are GMT -5. The time now is 10:13 AM.

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