LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-20-2015, 04:30 PM   #1
willc86
Member
 
Registered: Dec 2014
Posts: 56

Rep: Reputation: Disabled
trying out a grep and find script but not working


Hi guys i added a screen shot of the script; sorry couldnt copy and paste =(

anyhow it keeps saying grep command is not found

but if I were to run a simple script like

#!/bin/bash
WORD=ping
grep -r -i --col $WORD /document

it will execute it. so why is the attached script not able to?
Attached Thumbnails
Click image for larger version

Name:	Capture.PNG
Views:	49
Size:	11.3 KB
ID:	18968  
 
Old 07-20-2015, 05:11 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
because you changed PATH.
 
1 members found this post helpful.
Old 07-21-2015, 04:11 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by smallpond View Post
because you changed PATH.
To elaborate, your script sets the path to notes/Job-Steps. It's probably a bad idea to use a relative directory in your PATH, and if you need programs like grep, you mustn't overwrite what was in PATH before.

So, a possible fix is PATH=$PATH:$PWD/notes/Job-Steps. $PWD is you current directory.

Last edited by berndbausch; 07-21-2015 at 04:13 AM.
 
Old 07-21-2015, 09:18 AM   #4
willc86
Member
 
Registered: Dec 2014
Posts: 56

Original Poster
Rep: Reputation: Disabled
I am not exactly sure what you mean =( I am not sure how path is reflecting the error "grep command not found". Sorry, I am still getting used to bash scripting =(

so pretty much, it is ( i changed the paths; is this what you meant?)

#!/bin/bash
PATH=$PATH:$PWD/notes/Job-Steps
read -p "would you like to grep or find? 1 for grep 2 for find .. " selection

if [ $selection == 1 ]
then
echo " "
echo " "
echo "what would you like to grep for?"
read -p "please insert string to grep for... " STRING1
echo " "
echo " "
echo " you selected $STRING1 "
grep -r -i --col $STRING1 notes/Job-Steps
else [ $selection == 2 ]
echo " "
echo " "
echo "what would you like to find?"
echo " "
echo " "
fi
 
Old 07-21-2015, 09:50 AM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
yes, that's what I meant. Does it work?

Explanation: The PATH contains the list of all directories where the shell looks for programs to execute. When you use grep. for example, it will find it in /usr/bin. If /usr/bin is not in the PATH, the shell won't find grep.

PATH=notes/Job-Steps sets the PATH to this directory and no other. Since grep is not in notes/Job-Steps, it's not found.

PATH=$PATH:notes/Job-Steps takes the original PATH and adds notes/Job-Steps. So grep will be found.

PATH should contain the absolute pathnames of directories. That is, they should start with a slash "/". If not, once you go to a different directory, notes/Job-Steps doesn't exist. It's a relative pathname that only exists if your current directory contains "notes". Using $PWD/notes/Job-Steps instead is a far superior solution, as this pathname will be valid no matter in which directory the script will be executed.

Written after ingestion of various types of alcohols; no guarantee.
 
Old 07-22-2015, 05:20 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
1. You need an 'elif', not 'else' there http://www.thegeekstuff.com/2010/06/...ment-examples/
2. You're using string comparators where you want numeric ones http://www.tldp.org/LDP/abs/html/comparison-ops.html
 
Old 07-22-2015, 07:55 AM   #7
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Because $PATH contains the directories that are searched to FIND the executables. It cannot find grep because it's not in notes/Job-Steps.

$ which grep

You could use the full path to execute grep and it should work. But your method is not a good practice. PATH is a reserved environment variable for bash (and other shells). It is not a USER defined variable.

Last edited by Shadow_7; 07-22-2015 at 07:56 AM.
 
Old 07-22-2015, 08:26 AM   #8
willc86
Member
 
Registered: Dec 2014
Posts: 56

Original Poster
Rep: Reputation: Disabled
Ahhh. It does work.
and #which grep is where the command lives which is /usr/bin/grep
so, what you are saying is, I am moving the "grep command to that directory?" the script works 100 just trying to understand it.

you also mentioned, you have to specify the absolute path of grep in order for it to work, so why would
#!/bin/bash
WORD=ping
grep -r -i --col $WORD /document

work?



also, Only thing I do not understand is, i thought you would have to call out that variable? like, this is how I am understanding the script...
#!/bin/bash

word=HELLO WORLD
echo "$word"

so... I would assume if you had
PATH=$PATH:notes/Job-Steps .........and as mentioned above, he is saying that PATH contains where the searchable stuff is at, not where grep lives? put technically, isnt this PATH still pointing where the searchable executables are?

you would have to call it out like

grep -r -i "string to search for" $PATH

that is the parth I am confused about =/

Last edited by willc86; 07-22-2015 at 08:40 AM.
 
Old 07-22-2015, 08:42 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
If /bin/grep is in your $PATH as the script runs, then it will work within the script.

My style is to always determine the full path for a command such as that and then code that within the script.

For my system:
Code:
$ which grep
/bin/grep
Therefore were I to code that in a script:
Code:
#!/bin/sh

/bin/grep <options> <pattern> <file>
And please use [code][/code] tags when you enclose code or examples. Makes things better to read.
 
Old 07-22-2015, 09:21 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Actually the path environment is a user defined variable. The global default is defined in the /etc/profile and is typically different for root versus regular users. The user can change the path for their specific needs in their local profile i.e. .bash_profile or .bashrc depending on distribution and shell as well as in a script.

As stated the path environment variable is used to search for executable files. In a nutshell if you want to run grep you can just type grep at the prompt instead of the absolute path of /bin/grep

In the case of your script grep -r -i "string to search for" $PATH would expand to something like grep -r -i "string to search for" /usr/local/bin:/usr/bin:/bin which would produce an error no such directory.

Not sure what you are trying to achieve with setting the path as posted.
 
  


Reply



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 to use cp find and grep together to copy a list of files using find with grep babhijit Linux - Newbie 10 07-03-2013 12:25 PM
bash script: find, grep if and else programing.. snaaps Linux - Newbie 3 10-20-2010 08:24 PM
Script to find, grep and email basisvasis Programming 3 09-13-2008 12:23 PM
Find and Grep in Script - almost there chess Programming 8 05-01-2007 09:15 AM
find | grep <pattern> not working duvalr Linux - Software 4 09-17-2006 04:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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