I have a bash script that has a filename and a string contained within a file and I want to compare a substring from the file against the filename(tar.gz) to see if they match or not.
substr=$(echo "$(cat /home/user/configrc | grep Version)" | cut -d '_' -f4-5)
xfile="/home/user/data_1806_01.tar.gz"
echo $substr
echo $xfile
echo "Is $substr in $xfile"
if [ "$xfile" == *"$substr"* ]; then echo "Match"; else echo "No Match"; fi
the output is as follows
DD_DATA_AB_1806_01
1806_01
/home/user/data_1806_01.tar.gz
in /home/user/data_1806_01.tar.gz
No Match
I can't get a match to occur, 1806_01 should have the substring match in DD_DATA_AB_1806_01. Also what is strange is that the line - echo "Is $substr in $xfile" does not get printed out properly - "Is $substr" or "Is 1806_01" is missing
Is there some sort of termination that i need at the end of substr??
The file configrc contains the line Version=DD_DATA_AB_1806_01
I have looked at many examples online but can't see what I am doing wrong.... such a simple problem....
Thank you very much :)
Centos 7.2
/home/user/configrc
have DOS-style (CRLF) line endings?cat
becausegrep Version /home/user/configrc
will send it to standard output. Also, your string doesn't match because it's printingDD_DATA_AB_1806_01
as the value of$substr
which doesn't match1806_01
which is the value of$xfile
because it's not piping it into thecut
command. Even if it worked, it still wouldn't match because you set_
as a delimiter with thecut
command so printing fields 4 and 5 will give you1806 01
which isn't1806_01
.$xfile
to/home/user/data_1806_01.tar.gz
. You'll have to print the1806_01
form that to get a match.