LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-17-2009, 10:09 PM   #1
brfindla
LQ Newbie
 
Registered: Apr 2004
Location: Merrimack, NH
Posts: 13

Rep: Reputation: 0
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
 
Old 08-17-2009, 10:25 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Stretch (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
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.
 
Old 08-17-2009, 10:27 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 739Reputation: 739Reputation: 739Reputation: 739Reputation: 739Reputation: 739Reputation: 739
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?
 
Old 08-17-2009, 10:51 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,606

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

$(PWD) to just a dot . ie current dir
 
Old 08-17-2009, 10:54 PM   #5
brfindla
LQ Newbie
 
Registered: Apr 2004
Location: Merrimack, NH
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 08-17-2009, 10:58 PM   #6
brfindla
LQ Newbie
 
Registered: Apr 2004
Location: Merrimack, NH
Posts: 13

Original Poster
Rep: Reputation: 0
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!
 
Old 08-23-2009, 04:42 PM   #7
hunnykhan3
LQ Newbie
 
Registered: Aug 2009
Location: Islamabad,pakistan
Distribution: red hat linux 5
Posts: 23

Rep: Reputation: 15
.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...
 
Old 08-23-2009, 04:55 PM   #8
brfindla
LQ Newbie
 
Registered: Apr 2004
Location: Merrimack, NH
Posts: 13

Original Poster
Rep: Reputation: 0
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
 
Old 08-23-2009, 09:02 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,606

Rep: Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445
@hunnykhan3: please don't hijack someone else's thread. Open a new thread for your qn, with a meaningful title.
Thx
 
Old 08-24-2009, 09:54 AM   #10
hunnykhan3
LQ Newbie
 
Registered: Aug 2009
Location: Islamabad,pakistan
Distribution: red hat linux 5
Posts: 23

Rep: Reputation: 15
okay i'll make a new thread
 
  


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
question on shell scripting and multi-directional conditional branching jonhanna Linux - Newbie 11 01-09-2009 10:58 AM
Bash scripting - find command with variable substitution and quoting bgeddy Programming 3 12-05-2008 07:00 AM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 03:14 PM
newbe bash question ( grep processing) therealbxp Programming 2 11-20-2004 07:40 AM
Is this a valid command? find ./ -name bash.bashrc | cat davidas Linux - Newbie 11 04-06-2004 02:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:24 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