While read line loop

Hi I'm writing a bash script which will read an input file and look for occurrences of the current user ($USER) executing the script. When i find the occurrence of the username I take that line and append it to a file with a line number and bracket display next to line.

The input file has been called yy and contains env data.

the output file should contain output in the following form:

1) PWD=/home/users/bund.bash
2) USER=bund.bash
3) MAIL=/mail/bund.bash
4)..... etc etc etc

I have written the code but cant seem to get it to run....I believe there is an error with the syntax in the while loop. Many thanks in advance for any help.

 
#!/bin/bash
yy=$1
count=1
 
inputfile=$(grep $USER $yy)
 
while $inputfile | read line
do
 
        count=$((count+1))
        echo $count")" $line >> filesave
 
done 
 
 

should the done end with done <yy perhaps??

I didn't understand you correctly, but still some questions

Do you want to search for $USER in the yy file?
inputfile is a variable in your case not a file.

perhaps, you could try:

while read line
do
        found=$(grep $USER $line)
        if [ "$found" ]; then
          echo $count")" $line >> filesave
        fi
        count=$((count+1))
         
done < $yy

@ anchal

yes i want to search for every time $USER is in the yy file and write output in the form:

1) data
2) data
3) data

etc

I see what you have done and I understand the code but when i try and run the script i get the error: $yy: ambiguous redirect

what are you passing as $1? A filename with absolute path or if only filename then it must exists in the current dir.

can you show us the messages?

yy=$1 and yes its in the current directory. Here is the complete code:



#!/bin/bash

yy=$1
count=1



while read line
do

        found=$(grep $USER $line)
        if [ "$found" ]; then

        echo $count")" $line >> filesave
        fi
        count=$((count+1))


done < $yy

filesave is also in the current directory and is an empty file but it has been created

sorry,I did a mistake, dont you get grep error also?
try this

count=1



while read line
do

        found=$(echo $line | grep "$USER")
        if [ "$found" ]; then

        echo $count")" $line >> filesave
        fi
        count=$((count+1))


done < $1

Post your input file between code tags.

further to anchal_khare . Protecting any space characters in $line and avoiding syntax issues in the "if" statement when $found is null. Not clear (to me) where the value of $USER comes from.

#!/bin/bash

yy="${1}"

count=1

while read line
do

        found=$(echo "${line}"|grep "${USER}")
        if [ ! "${found}""X" = "X" ]
        then
              echo "${count}) ${line}" >> filesave
        fi
        count=$((count+1))


done < $yy

@Anchal - Many thanks I got it working using the found variable but I have been told there is a way using just the while loop.....

@Franklin - input file is "env" command's output. Am unable to post as it contains sensitive information.

I appended the contents on env to the yy file first using the following format:
env >> yy
env >> yy
env >> yy

Here is what I have done now and I get an output but it isn't the right one.......

#!/bin/bash

yy=$1
count=1
lineinput=$(echo $line | grep $USER)
while read line
do

        echo $count")" $line >> filesave
        count=$((count+1))
 
done < yy
 

Something like this?

env | awk -v var="$USER" '$0 ~ var{print NR ") " $0}'

yes franklin but I need to carry it out using only a while loop and no use of the sed or awk commands.....

Is this a homework question?

no am learning bash scripting for a new job.....it's not homework