LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash scripting question with find and grep and .bashrc - a multi year problem (https://www.linuxquestions.org/questions/linux-software-2/bash-scripting-question-with-find-and-grep-and-bashrc-a-multi-year-problem-748305/)

brfindla 08-17-2009 10:09 PM

Bash scripting question with find and grep and .bashrc - a multi year problem
 
No lie.. I've tried to solve this one off and on for years, I'll mess with it for awhile, get frustrated, give up.. so let me ask here as I still can't figure this out.

I have this script which works PERFECTLY for me below, I can search all the PWD and all directories below from where I call it and it will search all *.c and *.h files for the text I put after the -n at the end. PERFECTLY...


find $(pwd) -name '*.[ch]' -print0 -type f | xargs -0 grep -n <seach text>


However, like alot of things in linux, that is a very long and cumbersome command to type so I stuck it into my .bashrc file like this...

alias ff="find $(pwd) -name '*.[ch]' -print0 -type f | xargs -0 grep -n"

It now works differently if I use it as follows..

ff CREEPING_CRUD

instead of working from the PWD and below, it continues checking everywhere on the file system, and usually NOT where I called if from, but elsewhere. This sometimes makes a simple search of a small branch of a directory tree take minutes instead of seconds.

HOW do I get the alias (or whatever method works better) to do exactly the same thing to happen which happens on the command line (i.e. search from PWD and below, not everywhere)? Aside from just fixing this, I would like to understand WHY it is doing this as I am sure there are ramifications to this that will whack me in the future with other things.

Best!

Brian

neonsignal 08-17-2009 10:25 PM

Because you are using double quotes instead of single ones around the alias definition.

In double quotes, the $(pwd) still retains its definition, which means that the pwd gets expanded at the time the alias is made, not when it is used.

Change it to single quotes (you will need to escape the single quotes inside the definition).

You can list your aliases by the command 'alias', so you can see whether anything has been expanded.

pixellany 08-17-2009 10:27 PM

What happens if you make a soft link instead of an alias?

Have you done any experiments with simpler commands to see if aliases behave differently?

chrism01 08-17-2009 10:51 PM

Alternatively, as it seems you're always in the start dir when you call it, change

$(PWD) to just a dot . ie current dir

brfindla 08-17-2009 10:54 PM

Finally!!! It works! Thanks!!
 
I still haven't tried the soft link, I really have to give that a try. I've only ever used soft links for directories.. thanks for helping me ever expand my limited but growing knowledge in Linux.. BTW.. rather than escaping the inner ' I just "" and it seems to work fine.


alias ff='find $(pwd) -name "*.[ch]" -print0 -type f | xargs -0 grep -n'


1000 thanks, as stupid as it may sound, this has been kicking me for years, I really haven't done that much scripting, but its something that worth taking the time for.. now to find that time..


Thanks again all.. I swear this forum has never ever let me down.. thanks for that!!!

Brian

brfindla 08-17-2009 10:58 PM

Hi chrism01,
Yeah I had originally used the . but I had the same problem.. rather than reverting back to using the ., I figured that $(PWD) was pretty unambiguous (okay.. so is the dot)... but the problem was where it was expanded at the time the alias was made (I guess) as neonsignal said.

Thanks All!

hunnykhan3 08-23-2009 04:42 PM

.bashrc
 
i edited .bashrc........i don't know what should i have done...but now instead of root@localhost


i see
bash-3.2#


why is it so....i can't revert back...

brfindla 08-23-2009 04:55 PM

Hi HunnyKhan3,
Post your .bashrc maybe I can tell you. I am sure you know that logging in as root will give you the # and the bash-3.2 is the default.. so it looks like you killed that somehow. Post and lets see.

brfindla

chrism01 08-23-2009 09:02 PM

@hunnykhan3: please don't hijack someone else's thread. Open a new thread for your qn, with a meaningful title.
Thx

hunnykhan3 08-24-2009 09:54 AM

okay i'll make a new thread


All times are GMT -5. The time now is 07:38 AM.