I would like to search a file line by line and print out a given message to the screen when a string is matched and a different message to the screen when the string isn't matched.
This is what I have so far:
A file called network.txt that is located in the same directory that the script is running from. This is the contents of the file:
am.12345
XXXXXXXX
am.67890
XXXXXXXX
This is the script:
#!/bin/bash
file="network.txt"
for line in `cat $file`
do
if [ $line == am.* ]
then
echo $line
elif [ $line != am.* ]
then
echo "We couldn't find what you were looking for"
fi
done
This is the output I receive from the bash debug:
+ file=network.txt
++ cat network.txt
+ for line in '`cat $file`'
+ '[' am.12345 == 'am.*' ']'
+ '[' am.12345 '!=' 'am.*' ']'
+ echo 'We couldn'\''t find what you were looking for'
We couldn't find what you were looking for
+ for line in '`cat $file`'
+ '[' XXXXXXXX == 'am.*' ']'
+ '[' XXXXXXXX '!=' 'am.*' ']'
+ echo 'We couldn'\''t find what you were looking for'
We couldn't find what you were looking for
+ for line in '`cat $file`'
+ '[' am.67890 == 'am.*' ']'
+ '[' am.67890 '!=' 'am.*' ']'
+ echo 'We couldn'\''t find what you were looking for'
We couldn't find what you were looking for
+ for line in '`cat $file`'
+ '[' XXXXXXXX == 'am.*' ']'
+ '[' XXXXXXXX '!=' 'am.*' ']'
+ echo 'We couldn'\''t find what you were looking for'
We couldn't find what you were looking for
But when I run the script, I don't get the expected behaviour:
macbook:~ enduser$ ./network.sh
We couldn't find what you were looking for
We couldn't find what you were looking for
We couldn't find what you were looking for
We couldn't find what you were looking for
I believe the output should look like this:
am.12345
We couldn't find what you were looking for
am.67890
We couldn't find what you were looking for
What do I need to change to fix this?