Unix Shell - Using if loop

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > Using if loop





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Using if loop
chaitu

2006-11-22, 1:17 pm

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

Radoulov, Dimitre

2006-11-22, 1:17 pm

chaitu wrote:
[...]
:: 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.


[ "$(wc -l<infile)" -ne 10 ] && echo "Not equal" || echo "Equal"



Regards
Dimitre



chaitu

2006-11-22, 1:17 pm

Hi Dimitri,
Thank you for the response. I got the result but there is some other
thing which is bugging me.

[ "$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA5)" -ne "$(wc
-l<$EXPORT_TABLE_BLUEE_SCHEMA3)" || "$(wc
-l<$EXPORT_TABLE_BLUEE_SCHEMA6)" -ne "$(wc
-l<$EXPORT_TABLE_BLUEE_SCHEMA3)" || "
$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA7)" -ge 1 ] -a mailx -s 'Export
Failed (hq1al063)' dba_group@gwise Check message logs at
EXPORT_TABLE_BLUEE_SCHEMA7 for errors || echo "Ex
port Successful"

This is how i want to e-mail using mailx if the export fails but when
it is successfull i just want to echo that on to the screen, but when i
did that it gives me the following followed by the o/p.

[/udb/db2enpid/clnpis/udb09/scripts] > ./export_tables_schema3.ksh
db2enpid clnpis
../export_tables_schema3.ksh[143]: test: 0403-021 A ] character is
missing.
../export_tables_schema3.ksh[143]: 10: not found.
../export_tables_schema3.ksh[143]: 0: not found.
Export Successful.

Can you look into this.

Thanks in advance.

Ed Morton

2006-11-22, 1:17 pm

chaitu wrote:

> Hi Dimitri,
> Thank you for the response. I got the result but there is some other
> thing which is bugging me.
>
> [ "$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA5)" -ne "$(wc
> -l<$EXPORT_TABLE_BLUEE_SCHEMA3)" || "$(wc


ITYM "-o" rather than "||".

Ed.
Radoulov, Dimitre

2006-11-22, 1:17 pm

chaitu wrote:
:: Hi Dimitri,
:: Thank you for the response. I got the result but there is some other
:: thing which is bugging me.
::
:: [ "$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA5)" -ne "$(wc
:: -l<$EXPORT_TABLE_BLUEE_SCHEMA3)" || "$(wc
:: -l<$EXPORT_TABLE_BLUEE_SCHEMA6)" -ne "$(wc
:: -l<$EXPORT_TABLE_BLUEE_SCHEMA3)" || "
:: $(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA7)" -ge 1 ] -a mailx -s 'Export
:: Failed (hq1al063)' dba_group@gwise Check message logs at
:: EXPORT_TABLE_BLUEE_SCHEMA7 for errors || echo "Ex
:: port Successful"
::
:: This is how i want to e-mail using mailx if the export fails but when
:: it is successfull i just want to echo that on to the screen, but
:: when i did that it gives me the following followed by the o/p.
::
:: [/udb/db2enpid/clnpis/udb09/scripts] > ./export_tables_schema3.ksh
:: db2enpid clnpis
:: ./export_tables_schema3.ksh[143]: test: 0403-021 A ] character is
:: missing.
:: ./export_tables_schema3.ksh[143]: 10: not found.
:: ./export_tables_schema3.ksh[143]: 0: not found.
:: Export Successful.

Use [ ... ] || [ ... ] or [ ... -o ... ].

So:

[ "$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA5)" -ne
"$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA3)" \
-o "$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA6)" -ne
"$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA3)" \
-o "$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA7)" -ge 1 ] && mailx -s 'ExportFailed
(hq1al063)' \
dba_group@gwise Check message logs at EXPORT_TABLE_BLUEE_SCHEMA7 for errors
\
|| echo "Export Successful"

or

[ "$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA5)" -ne
"$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA3)" ] \
|| [ "$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA6)" -ne
"$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA3)" ] \
|| [ "$(wc -l<$EXPORT_TABLE_BLUEE_SCHEMA7)" -ge 1 ] && mailx -s
'ExportFailed (hq1al063)' \
dba_group@gwise Check message logs at EXPORT_TABLE_BLUEE_SCHEMA7 for errors
\
|| echo "Export Successful"



Doublecheck the logic, use ( ) for expression/operators grouping/precedence.


Regards
Dimitre


Keith Thompson

2006-11-22, 7:22 pm

"chaitu" <chaituonweb@gmail.com> writes:
> I have a doubt using IF loop in UNIX.

[...]

An if statement is not a loop. A loop is something that executes
repeatedly, such as a "for" or "while".

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com