I've had a good read of How do you tell if a string contains another string in Unix shell scripting? but I am struggling to get my code working with this as an example.
I am doing this in the default FreeBSD shell /bin/sh. No bash answers please.
Here is my code
while
: ${start=$i}
var1="$(command | awk -v awk_i=$i 'NR==awk_i { print $1 }')"
if test ${var1#*$var2} = $var2
then
row=$i
break
fi
i="$(( $i+1 ))"
[ "$i" -lt $iterate ]
do :;
done
I know that var1 is working OK because the test fails and continues to iterate through the series until the end. The problem is that when the test should pass, the variable for var1 is empty.
Here's some debug output where we have the iteration happening, the correct value for var2 being found and the test in theory passing, and then because the variable isn't expressed as part of the test properly, it keeps going.
+ awk -v 'awk_i=27' 'NR==awk_i { print $1 }'
+ var1=file/path/variable
+ test file/path/variable '=' var2
+ i=28
+ [ 28 -lt 52 ]
+ :
+ : 2
+ command
+ awk -v 'awk_i=28' 'NR==awk_i { print $1 }'
+ var1=file/path/variable
+ test '=' var2
test: =: unexpected operator
+ i=29
+ [ 29 -lt 52 ]
+ :
+ : 2
+ command
+ awk -v 'awk_i=29' 'NR==awk_i { print $1 }'
+ var1=file/path/variable
+ test file/path/variable '=' var2
+ i=30
+ [ 30 -lt 52 ]
I've even tried this alternate method: ##*/ use in UNIX Shell Scripting
if ${var1##*/$var2} = $var2
Can anyone make any suggestions why the variable is ending up blank and thus the test fails for the correct entry? Is it the / that could be causing havoc? (Though the other outputs are fine it seems)
Thanks