string comparison not working inside while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string comparison not working inside while loop
# 1  
Old 12-31-2010
string comparison not working inside while loop

The string comparison highlighted below is not working fine. Please help:
Code:
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.
# 2  
Old 12-31-2010
Looks fine to me.
Code:
$ f_status="Needs Checkout"
$ if [[ "${f_status}" == "Needs Checkout" ]]; then echo hello; fi
hello

# 3  
Old 12-31-2010
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
# 4  
Old 12-31-2010
Hi.

I suggest you look at the content of the variable. Here are some methods:
Code:
#!/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:
Code:
% ./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
# 5  
Old 12-31-2010
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

2. Shell Programming and Scripting

If loop inside function not working.

check_deplver () { dir=/abc/def/ghi if ssh -o StrictHostKeychecking=no $1 "" 2> /dev/null then echo " output is " ssh -o StrictHostKeychecking=no $1 "ls -lrt $dir | grep -i abc" 2> /dev/null else echo " directory not presnt" fi } This is not working. But... (7 Replies)
Discussion started by: NarayanaPrakash
7 Replies

3. Shell Programming and Scripting

set -options not working inside for loop?

I'm a beginner in shell scripting (I'm using ksh). I'm manipulating some files and I'm using set -A to transform each read line into a numeric array. However, inside the 'for' loop the options of set (ie '-A') are not recognized (the vi editor doesn't highlight it and it doesn't work). Where... (4 Replies)
Discussion started by: kasumlolla
4 Replies

4. Shell Programming and Scripting

String comparison not working inside while loop

Hi, In the code included below, the string comparision is not working fine. Please help while (( find_out >= i )) do file=`head -$i f.out|tail -1` dir=`dirname $file` cd $dir Status="" if ; then Status=`cvs -Q status... (3 Replies)
Discussion started by: sudvishw
3 Replies

5. Shell Programming and Scripting

String concatenation not working in a loop

Hi, First post, so I hope someone can help me with this weirdness :) I have a number files with some rows of information I want to extract, at the same time I want to add to a string some details from the file. I have found two different ways of looping over rows in a file, but one method... (5 Replies)
Discussion started by: LostInTheWoods
5 Replies

6. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

7. Shell Programming and Scripting

String handling is not working inside if loop

Hi All, I am comparing two strings inside an if condition if the strings are same then it should go inside the loop else it should execute code given in else part. But there is a but inside my script Even if the if condition is true it is not going inside the loop also it is executing... (4 Replies)
Discussion started by: usha rao
4 Replies

8. Shell Programming and Scripting

regex inside if comparison

I'm trying to compare the last octet of an IP to a regex: IP=$(ifconfig eth0 | grep inet | awk -F: '{print $2}' | awk -F. '{print $4}' | awk '{print $1}') if ]; then echo "GOOD: Correct IP range for server" else echo "ERROR:... (6 Replies)
Discussion started by: s_becker
6 Replies

9. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies

10. Shell Programming and Scripting

Help with if loop (string comparison)

Hi Can someone please tell me what is wrong with this (ksh).. if + ]] then echo ${COMP_TEMP} fi What i need here is, say if the variable is a 1 or 2 digit number, then execute the if loop. Basically the variable can either be 1-30 or some other character sequence say '?', '&&'... (4 Replies)
Discussion started by: psynaps3
4 Replies
Login or Register to Ask a Question