f



if loop in UNIX

Hi all,
 I have a doubt using IF loop in UNIX. I have the following statement.

if [[ `cat
/udb/db2enpid/clnpis/udb09/scripts/export_table_bluee_5/count_messages_begin_bluee|wc
-l` != 10 ]]
then
>    print -- "not equal"
> else
>    print -- "equal"
> fi
not equal

but when i do wc on the file it gives

cat
/udb/db2enpid/clnpis/udb09/scripts/export_table_bluee_5/count_messages_begin_bluee|wc
-l
      10

The result i want to get out of the if loop is equal. Can somebody help
me please.

Thanks in advance

0
11/22/2006 2:53:33 PM
comp.unix.questions 2631 articles. 0 followers. Post Follow

6 Replies
333 Views

Similar Articles

[PageSpeed] 10

On 22 Nov 2006 06:53:33 -0800, chaitu wrote:
> Hi all,
>  I have a doubt using IF loop in UNIX. I have the following statement.
>
> if [[ `cat
> /udb/db2enpid/clnpis/udb09/scripts/export_table_bluee_5/count_messages_begin_bluee|wc
> -l` != 10 ]]
> then
>>    print -- "not equal"
>> else
>>    print -- "equal"
>> fi
> not equal
>
> but when i do wc on the file it gives
>
> cat
> /udb/db2enpid/clnpis/udb09/scripts/export_table_bluee_5/count_messages_begin_bluee|wc
> -l
>       10
>
> The result i want to get out of the if loop is equal. Can somebody help
> me please.



You might want to see what is going on with


set -xv

_fn=/udb/db2enpid/clnpis/udb09/scripts/export_table_bluee_5/count_messages_begin_bluee

if [[ `cat $_fn |wc -l` != 10 ]]
then
    print -- "not equal"
 else
    print -- "equal"
 fi

set -

=================================

If it were me

_fn=/udb/db2enpid/clnpis/udb09/scripts/export_table_bluee_5/count_messages_begin_bluee

if [ $( wc -l < $_fn ) -eq 10 ] ; then
  echo "Equal"
else
  echo "Not equal"
fi

===================================

As for your question
http://tldp.org/LDP/abs/html/index.html     (hint break)

For extra points
man test
0
BitTwister2 (1376)
11/22/2006 4:39:06 PM
On 2006-11-22, chaitu wrote:
> Hi all,
>  I have a doubt using IF loop in UNIX. I have the following statement.

   There is no if loop; if is for conditional execution. If the test
   is true, then the commands following then are executed; if not, the
   commands following else (if there is one) is executed.

> if [[ `cat
> /udb/db2enpid/clnpis/udb09/scripts/export_table_bluee_5/count_messages_begin_bluee|wc
> -l` != 10 ]]
> then
>>    print -- "not equal"

   print is a not a standard command. Use printf:

printf "not equal\n"

   or echo:

echo "not equal"

>> else
>>    print -- "equal"
>> fi
> not equal
>
> but when i do wc on the file it gives
>
> cat
> /udb/db2enpid/clnpis/udb09/scripts/export_table_bluee_5/count_messages_begin_bluee|wc
> -l
>       10

    Don't use cat unnecessarily. Or the non-portable [[...]] syntax. 

    The problem is that your version of wc prints "     10", not "10".

> The result i want to get out of the if loop is equal. Can somebody help
> me please.

    Use simple steps, rather than trying to squeeze everything into
    one statement:

lines=$( wc -l < "$FILENAME" )
if [ $line -ne 10 ]
then
   .....


-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |         is released under the
   2005, Apress                 |    GNU General Public Licence
0
cfajohnson (1827)
11/22/2006 11:22:46 PM
2006-11-22, 18:22(-05), Chris F.A. Johnson:
[...]
>> The result i want to get out of the if loop is equal. Can somebody help
>> me please.
>
>     Use simple steps, rather than trying to squeeze everything into
>     one statement:
>
> lines=$( wc -l < "$FILENAME" )
> if [ $line -ne 10 ]
> then
>    .....
[...]

Note that there may be more efficient ways to check whether a
file is 10 lines long or not.

If "$FILENAME" may be very big.

awk '
  NR > 10 {exit}
  END {exit(NR != 10)}' < "$FILENAME"

Or:

lines=$(head -n 11 < "$FILENAME" | wc -l)
[ "$lines" -ne 10 ]

would be more efficient as it would avoid reading and processing
the whole file.

-- 
St�phane
0
this.address (408)
11/23/2006 8:18:44 AM
On 2006-11-23, Stephane CHAZELAS wrote:
> 2006-11-22, 18:22(-05), Chris F.A. Johnson:
> [...]
>>> The result i want to get out of the if loop is equal. Can somebody help
>>> me please.
>>
>>     Use simple steps, rather than trying to squeeze everything into
>>     one statement:
>>
>> lines=$( wc -l < "$FILENAME" )
>> if [ $line -ne 10 ]
>> then
>>    .....
> [...]
>
> Note that there may be more efficient ways to check whether a
> file is 10 lines long or not.
>
> If "$FILENAME" may be very big.
>
> awk '
>   NR > 10 {exit}
>   END {exit(NR != 10)}' < "$FILENAME"

   And if the number of lines to check for is small (on my system
   <50), it's more efficient to use a shell loop that call one external
   command.

num=0
while read line
do
  num=$(( $num + 1 ))
  [ num -gt 10 ] && break
done < $FILENAME

> Or:
>
> lines=$(head -n 11 < "$FILENAME" | wc -l)
> [ "$lines" -ne 10 ]

   Calling two external commands is less efficient than a shell loop
   for up to 150 lines.

> would be more efficient as it would avoid reading and processing
> the whole file.


-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |         is released under the
   2005, Apress                 |    GNU General Public Licence
0
cfajohnson (1827)
11/23/2006 2:25:46 PM
2006-11-23, 09:25(-05), Chris F.A. Johnson:
[...]
>    And if the number of lines to check for is small (on my system
>    <50), it's more efficient to use a shell loop that call one external
>    command.
>
> num=0
> while read line
> do
>   num=$(( $num + 1 ))
>   [ num -gt 10 ] && break
> done < $FILENAME

Note though that the above is not standard and fails if
some lines end in an odd number of backslash characters.

>> Or:
>>
>> lines=$(head -n 11 < "$FILENAME" | wc -l)
>> [ "$lines" -ne 10 ]
>
>    Calling two external commands is less efficient than a shell loop
>    for up to 150 lines.
[...]

YMMV. But for such short files, the time would be under the
hundredth of a second so would not be noticeable.

-- 
St�phane
0
this.address (408)
11/23/2006 3:18:41 PM
On 2006-11-23, Stephane CHAZELAS wrote:
> 2006-11-23, 09:25(-05), Chris F.A. Johnson:
> [...]
>>    And if the number of lines to check for is small (on my system
>>    <50), it's more efficient to use a shell loop that call one external
>>    command.
>>
>> num=0
>> while read line
>> do
>>   num=$(( $num + 1 ))
>>   [ num -gt 10 ] && break
>> done < $FILENAME
>
> Note though that the above is not standard

    It is standard, apart from the typo:

[ $num -gt 10 ] && break

>  and fails if some lines end in an odd number of backslash
> characters.

    Define fail. If a file has lines ending in backslashes, it is
    usually because the lines are supposed to be run together.

    If you do not want that behaviour, use "read -r".

>>> Or:
>>>
>>> lines=$(head -n 11 < "$FILENAME" | wc -l)
>>> [ "$lines" -ne 10 ]
>>
>>    Calling two external commands is less efficient than a shell loop
>>    for up to 150 lines.
> [...]
>
> YMMV. But for such short files, the time would be under the
> hundredth of a second so would not be noticeable.

   That depends on the speed of the machine, and the number of times
   the loop is used in a script.


-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |         is released under the
   2005, Apress                 |    GNU General Public Licence
0
cfajohnson (1827)
11/23/2006 4:07:19 PM
Reply: