Execution Problems with bash script

Hello,

can someone please help me to fix this script,
I have a 2 files, one file has hostname information and second file has console information of the hosts in each line, I have written a script which actually reads each line in hostname file and should grep in the console file and paste the information in the third file, but not sure, I am not getting the desire o/p in third file it is just a blank file.

and Consoles file has 5000 line and hostname file has 500 line

I was writing these scripts non of them working please suggest.

#!/bin/bash
while read line
do
        grep -i '$line' /scripting/consoles >> /scripting/output
done < /scripting/hostlist.txt
#!/bin/bash

/bin/date > /scripting/found.txt
/bin/date > /scripting/notfound.txt

while read line

do
  echo "checking" $line;
  sleep 2
  if [[ `/bin/grep $line /scripting/consoles` != "" ]];
    sleep 1
    then
    echo "$line found in /scripting/consoles file"
    echo "$line" >> /scripting/found.txt
    sleep 1
  else
    echo "$line  not found in /scripting/consoles file"
    echo "$line" >> /scripting/notfound.txt
  fi
done < /scripting/hostlist.txt

Thanks,

Hi.

From your first script, change:

grep -i '$line' 

to

grep -i "$line"

(i.e. with double quotes, to allow the shell to expand $line (it won't do that in single quotes)).

In your second script, you have a sleep 1 before then. If you need that, put it after the then of before the if.

Moreover, perhaps post a sample of both the input files (using

```text
```

-tags, please).

1 Like

Thanks for the reply

but I still have issues, third file is still empty.
and when I tried to debug the script, This is what I notice.

+ read line
+ grep -i $'pmx1app1\r' /scripting/consoles
+ sleep 1
+ read line
+ grep -i $'drmx1app1\r' /scripting/consoles
+ sleep 1
+ read line
+ grep -i $'pmx1app2\r' /scripting/consoles
+ sleep 1
+ read line

Here is input file entries
hostname file

pmx1app1
drmx1app1
pmx1app2
drmx1app2

Console file entries

pmx1app1 tlmcs001.us.net 5050
drmx1app1 tlmcs001.us.net 5051
pmx1app2 tlmcs001.us.net 5052
drmx1app2 tlmcs001.us.net 5053
$ grep -if hostfile console
pmx1app1 tlmcs001.us.net 5050
drmx1app1 tlmcs001.us.net 5051
pmx1app2 tlmcs001.us.net 5052
drmx1app2 tlmcs001.us.net 5053

The output is the same, so perhaps a more representative sample of the data would be helpful?

$ awk 'NR == FNR { H[$1]=1; next } H[$1]' hostfile console

I think I confused you,
Input files that I provided are just sample file and host file has around 500 lines, each line is hostname and it should search in console file which as 5000 enteries.

and "grep -if" did not worked

Try to pre-process your hostlist like this:

....
sed 's/\r//' $hfile | while read line ; do 
...
done
...

which will gt rid of those pesky windowsy carriage returns (\r). Otherwise you're grepping for string AND a carriage return, which not matching anything.

I suggest you use variables for filenames instead of hard-coding them and quote the output of grep in case it's more than one word:

cfile=/scripting/consoles
if [[ "`/bin/grep $line $cfile`" != "" ]] ; then 

As suggested I tried to redirect my hostlist file, I was having some errors.....

[root@localhost scripting]# bash -x final.sh
+ /bin/date
+ /bin/date
final.sh: line 24: syntax error near unexpected token `('
final.sh: line 24: `done < (sed 's/\r//' $hfile)'
[root@localhost scripting]#

Probably just missing a <:

done < <(sed 's/\r//' /scripting/hostlist.txt)

The redirection wasn't good. Sorry; I fixed my previous post. Pipe it into the loop instead.

@scottn: That fixes it. Why is it that < () is not enough to redirect output of subshell?