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:
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
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
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