LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 01-16-2008, 05:28 PM   #1
Jim Pye
LQ Newbie
 
Registered: May 2004
Location: New Zealand
Distribution: openSuSE & Fedora Core
Posts: 10

Rep: Reputation: 0
Bash loop using output of grep not working as needed


People

In a bash script I am writing I have a for loop that uses the output from a grep command to provide the data for the loop ie.

Code:
for CGI_DIR in `grep -e "^ *ScriptAlias " /etc/apache/httpd.conf`; do
  echo "CGI_DIR = ${CGI_DIR}"
done
Problem is that the output of this is:

Code:
CGI_DIR = ScriptAlias
CGI_DIR = /cgi-bin/
CGI_DIR = "/srv/www/cgi-bin"
CGI_DIR = ScriptAlias
CGI_DIR = /2nd-cgi-bin/
CGI_DIR = "/loc/of/sec/cgi/scripts"
What I would like is for the CGI_DIR variable to have the complete line so the output becomes:

Code:
CGI_DIR = ScriptAlias /cgi-bin/ "/srv/www/cgi-bin"
CGI_DIR = ScriptAlias /2nd-cgi-bin/ "/loc/of/sec/cgi/scripts"
I will then use a nested loop to deal with each section of the variable.

I have tried putting the grep command in " " eg.

Code:
for CGI_DIR in "`grep -e "^ *ScriptAlias " /etc/apache/httpd.conf`"; do
but then the output becomes:

Code:
CGI_DIR = ScriptAlias /cgi-bin/ "/srv/www/cgi-bin"
ScriptAlias /2nd-cgi-bin/ "/loc/of/sec/cgi/scripts"
This is a single line complete with a line break in the middle.

All documentation I have read says that grep outputs whole lines that match the criteria but I think bash then sees the spaces and chops it up.

Any thoughts on how to stop bash chopping up the grep output so I can deal with the whole line on each iteration?

3d
 
Old 01-16-2008, 06:06 PM   #2
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
I believe the OP of this thread here - http://www.linuxquestions.org/questi...-good.-613980/ - is asking the same question.
Setting the IFS as chrism01 has suggested there, should suffice.
 
Old 01-16-2008, 06:37 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I would opt for
Code:
while read CGI_DIR
do
  echo $CGI_DIR | grep -qe "^ *ScriptAlias " && echo "CGI_DIR = ${CGI_DIR}"
done < /etc/apache/httpd.conf
 
Old 01-16-2008, 06:42 PM   #4
Jim Pye
LQ Newbie
 
Registered: May 2004
Location: New Zealand
Distribution: openSuSE & Fedora Core
Posts: 10

Original Poster
Rep: Reputation: 0
h/w

Cheers for the heads up. I did all sorts of searches before posting but who would have guessed what the thread was about by the title

Anyways I tried the suggestion of the IFS and got the same output as if I had used the " " around the grep command.

I then looked at the suggestion of using the read command and the following looks like it might do what I am after:

Code:
while read line
do
  if [ "$( echo $line | gawk -F " " '{print $1}' )" = "ScriptAlias" ]; then
    echo "$line"
  fi
done </etc/apache/httpd.conf
Note I am using the newer $( ) instead of the back quotes here but I had to put the echo/gawk command pipe in quotes as the comparison gave errors on some of the other lines in the input file.

Again thanks for the tip.

3d
 
Old 01-16-2008, 06:45 PM   #5
Jim Pye
LQ Newbie
 
Registered: May 2004
Location: New Zealand
Distribution: openSuSE & Fedora Core
Posts: 10

Original Poster
Rep: Reputation: 0
colucix

Snap (almost)

Cheers
3d
 
Old 01-16-2008, 09:13 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Jim Pye View Post
People

In a bash script I am writing I have a for loop that uses the output from a grep command to provide the data for the loop ie.

Code:
for CGI_DIR in `grep -e "^ *ScriptAlias " /etc/apache/httpd.conf`; do
  echo "CGI_DIR = ${CGI_DIR}"
done
Problem is that the output of this is:

Code:
CGI_DIR = ScriptAlias
CGI_DIR = /cgi-bin/
CGI_DIR = "/srv/www/cgi-bin"
CGI_DIR = ScriptAlias
CGI_DIR = /2nd-cgi-bin/
CGI_DIR = "/loc/of/sec/cgi/scripts"
What I would like is for the CGI_DIR variable to have the complete line so the output becomes:

Code:
CGI_DIR = ScriptAlias /cgi-bin/ "/srv/www/cgi-bin"
CGI_DIR = ScriptAlias /2nd-cgi-bin/ "/loc/of/sec/cgi/scripts"
I will then use a nested loop to deal with each section of the variable.

I have tried putting the grep command in " " eg.

Code:
for CGI_DIR in "`grep -e "^ *ScriptAlias " /etc/apache/httpd.conf`"; do
but then the output becomes:

Code:
CGI_DIR = ScriptAlias /cgi-bin/ "/srv/www/cgi-bin"
ScriptAlias /2nd-cgi-bin/ "/loc/of/sec/cgi/scripts"
This is a single line complete with a line break in the middle.

All documentation I have read says that grep outputs whole lines that match the criteria but I think bash then sees the spaces and chops it up.

Any thoughts on how to stop bash chopping up the grep output so I can deal with the whole line on each iteration?

3d

use awk
Code:
# awk '/ScriptAlias/' file
ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/
ScriptAlias /cgi-bin1/ /usr/local/apache2/cgi-bin1/
depending on what you want to do after you get those lines
Code:
#!/bin/sh
awk '/ScriptAlias/ { 
  # do your stuff with ScriptAlias lines here
}' file
 
Old 01-16-2008, 09:49 PM   #7
Jim Pye
LQ Newbie
 
Registered: May 2004
Location: New Zealand
Distribution: openSuSE & Fedora Core
Posts: 10

Original Poster
Rep: Reputation: 0
ghostdog74

Cheers for the response. I have the script working with the read command but I will keep the awk syntax in mind for future.

Cheers
3d
 
Old 01-16-2008, 10:27 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Jim Pye View Post
ghostdog74

Cheers for the response. I have the script working with the read command but I will keep the awk syntax in mind for future.

Cheers
3d
it can be simplified then, no need to call gawk for every line
Code:
while read line
do
  case "$line" in 
      ScriptAlias* ) echo "$line" ;;
  esac
done </etc/apache/httpd.conf
 
  


Reply



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
problem with 'rpm -qa | grep' in while loop ionic_slim Programming 9 11-11-2007 08:09 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
parsing a string from grep output in bash xpromisex Programming 2 11-12-2006 09:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:00 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
Open Source Consulting | Domain Registration