3

When I run my script I get this error:

234.sh: line 3: syntax error near unexpected token `do
234.sh: line 3: `for folder in $array ; do

I don't see the mistake. Help?

#!/bin/bash
base=$(pwd)
array=`find * -type d`
 for folder in $array ; do
  cd $folder ;
  grep -n $1 * | while read line ;
   do    name=$(echo "$line" | cut -f1 -d:) ;
        if [ "$name" == "1234.sh" ]; then
        continue ;
        else
        string=$(echo "$line" | cut -f2 -d:) ;
        a=$(expr $string - 10)
        if [ $a -lt 1 ] ; then 
        a=1 ;
        fi ;
        b=$(expr $string + 10) ;   
        echo "-----------------------"
        echo $name:$a
        sed -n $a,${b}p $name;
        fi ;
    done
   cd $base ;
done
3
  • remove the ';' before the 'do' and put 'do' on a newline Commented Jul 31, 2012 at 13:00
  • 1
    That shouldn't be necessary; it's legal to terminate the statement with a semi-colon. Commented Jul 31, 2012 at 13:09
  • 2
    Your variable named "array" is not an array, it is a string. Commented Jul 31, 2012 at 13:12

4 Answers 4

3

A few suggestions:

  1. Make array a proper array, not just a string. (This is the only suggestion that actually addresses your syntax error.)

  2. Quote parameters

  3. Use IFS to allow read to split your line into its two components

  4. Use a subshell to eliminate the need to cd $base.

  5. Most of your semicolons are unnecessary.


#!/bin/bash
array=( `find * -type d` )
for folder in "${array[@]}" ; do
  ( cd $folder
    grep -n "$1" * | while IFS=: read fname count match; do
      [ "$fname" == "1234.sh" ] && continue

      a=$(expr $count - 10); [ $a -lt 1 ] && a=1
      b=$(expr $count + 10) 
      echo "-----------------------"
      echo $fname:$a
      sed -n $a,${b}p $fname
    done
  )
done
Sign up to request clarification or add additional context in comments.

Comments

1
#!/bin/bash

base=$(pwd)
array=`find . -type d`
for folder in $array
do
  cd $folder
  grep -n $1 * | while read line
  do    
      name=$(echo "$line" | cut -f1 -d:)
      if [ "$name" == "1234.sh" ]
      then
        continue
      else
        string=$(echo "$line" | cut -f2 -d:)
        a=$(expr $string - 10)
        if [ $a -lt 1 ]
        then 
          a=1
        fi
        b=$(expr $string + 10)
        echo -----------------------
        echo $name:$a
        sed -n $a,${b}p $name
      fi
  done
  cd $base
done

Comments

1

What you're trying to accomplish looks like a context grep for a specified pattern on all files in a directory tree.

I suggest you use Gnu grep Context Line Control

#!/bin/bash
base=$(pwd)
spread=10
pattern=$1

find . -type d | while read dir; do
    (cd $dir && egrep -A $spread -B $spread $pattern *)
done

This is the simple version, without dealing with 1234.sh or empty directories

Comments

1

This solution is even less complex, and in addition deals with the exempt file name. It depends on the xargs and Gnu grep Context Line Control as well.

#!/bin/bash
spread=10
pattern=$1

find . -type f ! -name "1234.sh" |
    xargs egrep -A $spread -B $spread $pattern 

1 Comment

If presentation is very important, more work is needed for this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.