# string comparison not working inside while loop

**URL:** <https://community.unix.com/t/string-comparison-not-working-inside-while-loop/280020>\
**Category:** Shell Programming and Scripting\
**Created:** [December 31, 2010, 4:00am UTC](https://community.unix.com/t/string-comparison-not-working-inside-while-loop/280020 "2010-12-31T04:00:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudvishw](https://community.unix.com/letter_avatar/sudvishw/32/6_449c533f586939b6e5f246632a46421f.png) [@sudvishw](https://community.unix.com/u/sudvishw)\
**Post date:** [December 31, 2010, 4:00am UTC](https://community.unix.com/t/string-comparison-not-working-inside-while-loop/280020/1 "2010-12-31T04:00:45Z")

</div>

The string comparison highlighted below is not working fine. Please help:

```nohighlight
while read line
do
    # Get File name by deleting everything that preceedes and follows Filename as printed in cvs status' output
    f_name=`echo $line | sed -e 's/^File:[^I]//' -e 's/ *Status:.*//' | awk '{print $NF}'`
    f_status=`echo $line | cut -d ":" -f3`
    if [["${f_status}" == "Needs Checkout"]]; then
    f_path='Only in Repository'; echo "$f_name | $f_status | $f_path" >> $out1; continue; fi
    find /home/ustst/ -name "$f_name" -print > $f_out
    find_out=`cat $f_out|wc -l`
    if [$find_out -eq 1]; then path=`cat $f_out`; f_path=`dirname $path`; echo "$f_name | $f_status | $f_path" >> $out1; fi
    if [$find_out -gt 1]; then
 
        while read $file
        do
            dir=`dirname $file`
            cd $dir
            Status=""
            if [-d CVS]; then
                Status=`cvs -Q status $f_name|grep Status|cut -d ":" -f3`
            fi
            if [[-n "${Status}" && "${Status}" != "Up-to-date"]]; then
                echo "$f_name | $Status | $dir" >> $out1
            fi
            cd - > /dev/null
        done < $f_out
    fi
done < $out

```

Even if f\_status=Needs Checkout, it is still not going inside the if condition.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [December 31, 2010, 4:11am UTC](https://community.unix.com/t/string-comparison-not-working-inside-while-loop/280020/2 "2010-12-31T04:11:21Z")

</div>

Looks fine to me.

```nohighlight
$ f_status="Needs Checkout"
$ if [["${f_status}" == "Needs Checkout"]]; then echo hello; fi
hello

```

---

<div class="post-metadata">

**Author:** ![xoops](https://community.unix.com/letter_avatar/xoops/32/6_449c533f586939b6e5f246632a46421f.png) [@xoops](https://community.unix.com/u/xoops)\
**Post date:** [December 31, 2010, 5:47am UTC](https://community.unix.com/t/string-comparison-not-working-inside-while-loop/280020/3 "2010-12-31T05:47:53Z")

</div>

not sure if it helps,

look for any hidden character in the line.  
if you have copy-pasted the script/line then then try to re-type it

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [December 31, 2010, 6:22am UTC](https://community.unix.com/t/string-comparison-not-working-inside-while-loop/280020/4 "2010-12-31T06:22:17Z")

</div>

Hi.

I suggest you look at the content of the variable. Here are some methods:

```nohighlight
#!/usr/bin/env bash

# @(#) s1	Demonstrate 3 ways to see value of string.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

f_status="Needs Checkout"

pl " Method 1: print bounded by some character to show whitespace:"
echo " f_status is :$f_status:"

pl " Method 2: use utility to show all characters:"
echo " f_status details:"
echo "$f_status" | od -bc

pl " Method 3: use shell to show details of operations:"
set -x
if [["${f_status}" == "Needs Checkout"]]; then echo hello; fi

exit 0

```

producing:

```nohighlight
% ./s1

-----
 Method 1: print bounded by some character to show whitespace:
 f_status is :Needs Checkout:

-----
 Method 2: use utility to show all characters:
 f_status details:
0000000 116 145 145 144 163 040 103 150 145 143 153 157 165 164 012
          N e e d s C h e c k o u t \n
0000017

-----
 Method 3: use shell to show details of operations:
+ [[Needs Checkout == \N\e\e\d\s\ \C\h\e\c\k\o\u\t]]
+ echo hello
hello
+ exit 0

```

Good luck ... cheers, drl

---

<div class="post-metadata">

**Author:** ![guto](https://community.unix.com/letter_avatar/guto/32/6_449c533f586939b6e5f246632a46421f.png) [@guto](https://community.unix.com/u/guto)\
**Post date:** [December 31, 2010, 12:02pm UTC](https://community.unix.com/t/string-comparison-not-working-inside-while-loop/280020/5 "2010-12-31T12:02:07Z")

</div>

At first glance, the script looks fine to me, so the easiest way of tracking what's happening would be using activating bash debugging utility just prior to that specific line of code: "set -x".  
This will cause every subsequent standard output to come output (each line) to start with a "+" character. The magic is that, it allows you to see exactly what is taking place within you coding sequence. In other words, what variables are correctly assigned or not and many more details.

It's a very helpful debugging utility.  
have fun!
