LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash loop using output of grep not working as needed (https://www.linuxquestions.org/questions/programming-9/bash-loop-using-output-of-grep-not-working-as-needed-614090/)

Jim Pye 01-16-2008 05:28 PM

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

h/w 01-16-2008 06:06 PM

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.

colucix 01-16-2008 06:37 PM

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


Jim Pye 01-16-2008 06:42 PM

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

Jim Pye 01-16-2008 06:45 PM

colucix

Snap (almost) :)

Cheers
3d

ghostdog74 01-16-2008 09:13 PM

Quote:

Originally Posted by Jim Pye (Post 3025076)
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


Jim Pye 01-16-2008 09:49 PM

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

ghostdog74 01-16-2008 10:27 PM

Quote:

Originally Posted by Jim Pye (Post 3025282)
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



All times are GMT -5. The time now is 11:52 PM.