LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Closed Thread
 
Search this Thread
Old 09-28-2012, 07:07 AM   #1
calvarado777
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Rep: Reputation: Disabled
Bash- grep and loops


Hello all, I am in week two of learning Bash and I am lost!
My first task is to search the current directory for files with the extensions .c and .cc, and search for the strings printf and fprintf within those files. IF the print statements are found, then I am to add in a header if it's not already there.

This is where I am at so far-- I am finding the man pages hard to understand and googling everything has become overwhelming!

Can anyone maybe just give me a few hints? My errors that I have run into so far is that I can't seem to get the right syntax when I am piping my 'ls' results to my grep in the outer for loop...so I eventually decided to work from the inside out...but now I can't seem to get the right type of arguements for my inner grep :-(

Code:
#!/bin/sh

for FILE in `ls *.cc *.c` # | egrep "printf\ | fprintf"

do
        echo $FILE;                                    #to make sure ls worked
        if [ grep -Fqx '#include <stdio.h>' "$FILE" ]  
        then
                sed '1i\
                #include <stdio.h> ' file > new_file;
                mv new_file file;
        fi
done
 
Old 09-28-2012, 08:54 AM   #2
Toggan
Member
 
Registered: Oct 2011
Distribution: CentOS 5.9
Posts: 39

Rep: Reputation: 4
You may want to try using exit codes for your if statement. Something along the lines of:

grep -Fqx '#include <stdio.h>' $FILE
if [[ $? -eq 0 ]]; then
sed '1i\
#include <stdio.h> ' file > new_file;
mv new_file file;
fi

This will do the quiet grep, and then only continue if the command was successful. If you'd like some error checking, you could put an else before the fi and echo error or something.
 
Old 09-28-2012, 09:23 AM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

Follow thread here: http://www.linuxquestions.org/questi...ps-4175429419/
 
  


Closed Thread

Tags
bash, grep, loop


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
[SOLVED] Bash- grep and loops calvarado777 Linux - Newbie 7 09-28-2012 03:00 PM
Bash loops verse123 Linux - Newbie 3 10-22-2011 04:25 PM
Bash Loops multiplex A/V lpn1160 Linux - General 1 02-23-2008 06:01 AM
Bash Loops UnitedWeFall Programming 8 08-21-2007 06:33 AM
bash scripting and loops phoeniks Programming 5 01-24-2005 04:00 PM


All times are GMT -5. The time now is 04:16 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration