0

if [ $data == $dis ] this line is not giving expected results in the script

Tried on terminal, it works fine. "true" is expected from the script and then wanna delete that line from the file

root@M5-L-G01FQY9:/mnt/c/Users/Vinita.a.wadhwani/Downloads/Rough# d="<disabled></disabled>"
root@M5-L-G01FQY9:/mnt/c/Users/Vinita.a.wadhwani/Downloads/Rough# c="<disabled></disabled>"
root@M5-L-G01FQY9:/mnt/c/Users/Vinita.a.wadhwani/Downloads/Rough# if [ $d == $c ]
> then
> echo "true"
> fi
true

Code is as follows:

dirmon=$1
action=$2
node=$(grep -l $dirmon *)
line=$(grep -n -m 1 $dirmon $node | sed  's/\([0-9]*\).*/\1/')
echo 'Performing' $action 'action on dirmon-'$dirmon 'which is present in node-'$node
no=$(($line+20))
data=$(sed -n "${no}p" $node) #gives results as <disabled></disabled>
echo $data
dis="<disabled></disabled>"
echo $dis
if [ $data == $dis ]
then
echo 'true'
fi
#echo 'false'

expected results:

root@M5-L-G01FQY9:/mnt/c/Users/Vinita/Downloads/Rough# ./On* YELLOW start
Performing start action on dirmon-YELLOW which is present in node-NODE.txt
<disabled></disabled>
<disabled></disabled>
true

Actual results

root@M5-L-G01FQY9:/mnt/c/Users/Vinita/Downloads/Rough# ./On* YELLOW start
Performing start action on dirmon-YELLOW which is present in node-NODE.txt
<disabled></disabled>
<disabled></disabled>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Do you have `#!/bin/bash` at the beginning of the script? `==` is a bash extension, the standard way to compare strings is with `=`. If you don't have `#!/bin/bash`, the script will be run with `sh` instead of `bash`, so it won't implement the extensions. – Barmar Jun 20 '19 at 21:19
  • 1
    You should also quote your variables. – Barmar Jun 20 '19 at 21:20
  • I tried both of your suggestions earlier itself, and it didn't work. – Vinita Wadhwani Jun 20 '19 at 21:21
  • The way to debug shell scripts is by putting `set -x` at the beginning of the script. Then it will show you each command as it's being executed, so you can see how the variables are substituted. – Barmar Jun 20 '19 at 21:22
  • How do you run the script? – Barmar Jun 20 '19 at 21:23
  • 1
    Before the comparison try `echo "data: \"$data\" -- dis: \"$dis\"" . might be an extra space at the end?? – GoinOff Jun 20 '19 at 21:23
  • Debugging worked. I can see that there is white space in $data. – Vinita Wadhwani Jun 20 '19 at 21:25
  • 1
    The if should be `if [ "$data" = "$dis" ]` – GoinOff Jun 20 '19 at 21:25
  • 1
    Please truncate your prompt for posting. – Dennis Williamson Jun 20 '19 at 21:26
  • Instead of `echo $dis`, do `od -c <<<"$dis"` (do same with $data) to see if there are any "invisible" characters in there – glenn jackman Jun 20 '19 at 21:28
  • ```' data=' ``` is the value in $data – Vinita Wadhwani Jun 20 '19 at 21:28
  • See https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable to trim whitespace – GoinOff Jun 20 '19 at 21:31
  • 1
    using ```sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' ``` to trim. – Vinita Wadhwani Jun 20 '19 at 21:32
  • @Barmar : Technically speaking, `[[ ... ]]` would be the bash extension which supports the `==`, while `[ something... ]` is just a shortcut to `test something ....`. Now, what puzzles me, is that if i write (in bash) `/usr/bin/test a == a`, I get zero exit code, while `/usr/bin/test a == b` yields exit code 1, which suggests that even /usr/bin/test somehow understands the `==` operator. The _test_ man page doesn't mention `==`, so I am surprised that there is no error message. – user1934428 Jun 21 '19 at 06:23
  • @user1934428 Most shells implement `test/[` as a built-in, so it gets whatever extensions they implement. – Barmar Jun 21 '19 at 15:53
  • @user1934428 On my Mac, the `test` man page says "For compatibility with some other implementations, the = primary can be substituted with == with the same meaning." I guess Linux also implements this, it's just not mentioned in the man page. – Barmar Jun 21 '19 at 15:56
  • @Barmar: Perhaps. Interestingly, on my Mac (still Mac OS-X 10.6), this clause is missing from _man test_. – user1934428 Jun 22 '19 at 06:28

1 Answers1

0

The reason was the whitespace, due to which comparison was failing.

A tremendous command that helped to identify issue set -x is used for debugging.

Modified code:

#!/bin/bash
set -x
dirmon=$1
action=$2
node=$(grep -l $dirmon *)
line=$(grep -n -m 1 $dirmon $node | sed  's/\([0-9]*\).*/\1/')
echo 'Performing' $action 'action on dirmon-'$dirmon 'which is present in node-'$node
no=$(($line+20))
data=$(sed -n "${no}p" $node| sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' )
echo $data
dis="<disabled></disabled>"
echo $dis
if [ "$data" = "$dis" ]
then
echo 'true'
fi
#echo 'false'
  • Which whitespace are you referring to? Could you point out what part of the modification fixed this? – Barmar Jun 21 '19 at 15:58
  • It looks like the only change you made was to add quotes in the `if` statement. If there's whitespace at the end of `$data`, that should make the comparison fail when it used to succeed. But that's the opposite of the problem you were reporting. – Barmar Jun 21 '19 at 16:00
  • the line that i modified is "data=$(sed -n "${no}p" $node| sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' )" – Vinita Wadhwani Jun 25 '19 at 22:48
  • The detailed explanation should be in the answer, not a comment. – Barmar Jun 26 '19 at 15:02
  • In your original code you didn't have quotes around `$data`, so whitespace around it would have been ignored. – Barmar Jun 26 '19 at 15:04