Operating System - HP-UX
1752786 Members
5770 Online
108789 Solutions
New Discussion юеВ

if - then statement does'nt work

 
SOLVED
Go to solution
Sutoyo Kardi
New Member

if - then statement does'nt work

I have the following simple scripts on HP-UX B.11.31

#!/bin/sh
WK=`date +%W`
NWK=`expr $WK % 2`
if $NWK -eq "0"; then
WKS="week-1";
else
WKS="week-2";
fi
echo "today :" `date`
echo "#week :" $WK
echo "nweek :" $NWK $WKS


When I execute the result as follows:
./calcweek.sh[4]: 0: not found.
today : Tue Jul 13 10:58:35 TST 2010
#week : 28
nweek : 0 week-2


The question is
1. what means " 0: not found"
2. why if-then statement does'nt work? (if nweek=0 should "week-1")

Thanks very much for your willingness to help.
Regards,


5 REPLIES 5
Steven Schweda
Honored Contributor

Re: if - then statement does'nt work

> ./calcweek.sh[4]: 0: not found.

As a command, "$NWK" ("0") is not very
useful. Perhaps something more like:

if test "$NWK" -eq 0 ; then
or:
if [ "$NWK" -eq 0 ]; then


man test

(Quoting "$NWK" avoids syntax errors if NWK
is empty. If "-eq" is a numeric test, then
quoting "0" may be more confusing than
helpful. Some of your semi-colons
("WKS=...;") seem to be unnecessary.)
Sutoyo Kardi
New Member

Re: if - then statement does'nt work

I've made changes in my script:
#!/bin/sh
WK=`date +%W`
NWK=`expr $WK % 2`
if ["$NWK" -eq 0]; then
WKS="week-1"
else
WKS="week-2"
fi
echo "weeks:" $WK $NWK $WKS

The result is still same as previous:
./calcweek.sh[4]: [0: not found.
weeks: 28 0 week-2

I still expect your assistance.
Regards,
Steven Schweda
Honored Contributor
Solution

Re: if - then statement does'nt work

> I've made changes in my script:
> [...]

Perhaps you should try copy+paste. Or be
more careful. Sometimes, spaces can be
important.

> The result is still same as previous:
> [...]

No, it's not the same.

> ./calcweek.sh[4]: 0: not found.

> ./calcweek.sh[4]: [0: not found.

See the difference?


> man test

Also, to see how "if" works:

man sh
man sh-posix
Sutoyo Kardi
New Member

Re: if - then statement does'nt work

Thanks Steven,

You are corect. Sametimes, spaces is very usefull. I was careless.

Thanks for your explanation.
Regards,


Sutoyo Kardi
New Member

Re: if - then statement does'nt work

Thanks