bash search function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash search function
# 1  
Old 09-30-2008
bash search function

I want to have a function with a similar interface:
Code:
search *.cpp asdf

that will search recursively all directories for *.cpp file, containing line 'asdf' inside. I tried this:
Code:
function search { find . -name "$1" | xargs grep -li $2; }

But it doesn't work all the time. For example, if i run it in dir1:
Code:
dir1
\dir2
\\file2.cpp
\file1.cpp

it only greps file1.cpp without going down to file2.cpp. I figured out it happens because of globbing *.cpp to the list of files in dir1, but i have no idea, how to avoid it in function without turning globbing off. Quotes do not help either. Any ideas?
# 2  
Old 09-30-2008
Search Current Directory
Code:
egrep asdf *.cpp

Search all directories under current including current:
Code:
egrep -R asdf *.cpp

# 3  
Old 09-30-2008
Did you try:

Code:
search "*.cpp" asdf

# 4  
Old 09-30-2008
Quote:
Originally Posted by Ikon
Search all directories under current including current:
Code:
egrep -R asdf *.cpp

Thanks for a shorter solution, but it still doesn't go to dir2.
I think *.cpp is extanding to file1.cpp and then we have
Code:
egrep -R asdf file1.cpp


Quote:
Originally Posted by radoulov
Did you try:

Code:
search "*.cpp" asdf

Strange, i was sure i tried it before and it didn't help. However it works now Smilie Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to search file for string and lauch function if found

In the bash below I am searching the filevirus-scan.log for the Infected files: 0 line (in bold) and each line for OK. If both of these are true then the function execute is automatically called and processing starts. If both these conditions are not meet then the line in the file is sent to the... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Bash function problem

I am trying to figure out why I am having a "problem" with some functions in a bash script I am running. The reason for air quoting is that the functions are working, they are just not displaying anything to screen when called from another function. Here's an example: function Create_Input {... (6 Replies)
Discussion started by: dagamier
6 Replies

3. UNIX for Dummies Questions & Answers

Bash INKEY$ function...

This is probably common knowledge to the professionals but not so much for amateurs like myself. This is a code snippet for the equivalent of BASIC's... LET char$=INKEY$ As the timeout parameter cannot be less than 1 second then this is the only limitation... It is a single line... (5 Replies)
Discussion started by: wisecracker
5 Replies

4. Programming

Search function help

Hello again unix.com, I need a help with a search function on the website i'm building. http://auto-nonstop.ro/index.php?page=search --> when I press Apply the search fields of the plugin pops up... all i want to do is http://auto-nonstop.ro/index.php?page=search to be the page before I press... (1 Reply)
Discussion started by: galford
1 Replies

5. Shell Programming and Scripting

Bash function

startvm() { startguest } Is there a way use one line to get this ? actually I want startvm=startguest (5 Replies)
Discussion started by: yanglei_fage
5 Replies

6. UNIX for Dummies Questions & Answers

If function to search for files

Hi, I need to concatenate two files as long as both files are present. As of now, I am using the following code: #!/bin/bash for y in {1..14} do cat -s $y.F $y.R > $y.1 done However, the outputfile is being generated even in the absent of both infiles. I would like to search for both... (2 Replies)
Discussion started by: Xterra
2 Replies

7. Shell Programming and Scripting

BASH function error

Hey everyone. I am currently testing my first function based BASH script. The ultimate goal is going to be moving logs from point A to point B (or if B is down, to point C). Part of this involves the following function: function testAlive{ ping -c 1 -q $1 } Now when I run ping -c... (1 Reply)
Discussion started by: msarro
1 Replies

8. UNIX for Dummies Questions & Answers

How to find function name based on word search

Hi All, I want to find out function name based on word. Here i ll give u one example I have several files based on below format. file will start and ends with same name only EX: file1.txt public function calculate1() { ---- ---- call Global Function1() ---- ---- } END... (9 Replies)
Discussion started by: spc432
9 Replies
Login or Register to Ask a Question