Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.