Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a script from a tutorial on strike

Status
Not open for further replies.

vakimova

Technical User
Dec 24, 2005
21
RU
dear sages,
please tell me what's wrong with the following script:

#!/bin/sh
#usage yd2ymd 1998213
# if there is no command line argument, assume one is being
# piped in and read it
if [ X$1 = X ]
then
read dt
else
dt=$1
fi

# break apart the year and the days

y=`expr $dt / 1000`
d=`expr $dt % 1000`

# subtract the number of days in each month starting from 1
# from the days in the date. When the day goes below 1, you
# have the current month. Add back the number of days in the
# month to get the correct day of the month
m=1
while [ `expr $d \> 0` = 1 ]
do
md=`sh monthdays $y $m`
d=`expr $d \- $md`
m=`expr $m \+ 1`
done

d=`expr $d \+ $md`

# the loop steps one past the correct month, so back up the month
m=`expr $m \- 1`

# assemble the results into a gregorian date
grg=`expr \( $y \* 10000 \) \+ \( $m \* 100 \) \+ $d`
echo $grg

---------------------
and all that resulted in --
---------------------
yd2ymd: 38: Syntax error: end of file unexpected (expecting "do")
# % >

Note: the script was copied from a tutorial, I only added "sh" in the line--
md=`monthdays $y $m`

to avoid "not found" message; the monthdays file is in its place and working OK.

yours`
serguey



 
Dear R.,

You are right "done" should be there and it is to the best of my sight:

do
md=`sh monthdays $y $m`
d=`expr $d \- $md`
m=`expr $m \+ 1`
done
^
|

Thank you for your try to help.

yours`
serguey
 
Hi

Works for me, with a fake monthdays. ( monthdays just returns hardcoded value of 30, the input number varies. )

I think the error is somewhere else, the code you posted is correct.

The excessive use of [tt]expr[/tt] makes the script obscure. My suggestion is to rewrite it, doing the arithmetic and relational operations with the shell itself.

Feherke.
 
Are you sure you are running YOUR copy of yd2ymd? Try

type yd2ymd

and see where your shell finds a copy of the script to run.

If that checks out - try adding a 'set -x' at the top of the script - that way you'll be able to identify where it stuffs up. Also you'll know the args to monthdays because I am not at all convinced that script is 'working OK'. Also put a 'set -x' in the monthdays script too for further problem isolation.


HTH,

p5wizard
 
Long shot: Any chance you have incorrect line termination in this file? Make sure you don't have ^M at the end of the lines.
 
dear Feherke,

you are right -- the script should work for it is copied from a tutorial.
Unfortunately, I am not prepared yet to rewrite obscure scripts yet.

Thank you for your kind attention.

yours`
serguey
 

dear p5 wizard,
the script is not MINE. I copied it from a tutorial, made a file of it, made the file executable with the command
chmod a+x yd2ymd
the same way I did to monthdays file which runs on its own quite OK.

Thank you for your help.

yours`
serguey
 


dear motoslide,
the bulleye! I had lots of ^Ms in the file but erased all of them with vi editor and now there are no ^Ms to get rid of. So what?

admiredly yours`
serguey
 
Does that mean the problem is fixed?

Did it work before you added the sh? Try changing it to sh -x perhaps, maybe the problem is in monthdays.

Annihilannic.
 
dear Annihillanic,

the problem is not fixed and here is the whole story--

I ran across a bunch of scripts in a tutorial, typed them, 'chmod a-X'ed them and they all work except for this unfixed one.

To make sure it's not because of my typos I copied the script from the tutorial, pasted it (here ^Ms enter the story) and what I had for my pains is put at the start of this thread.
Any remedies I applied since then (both recommended and self-invented) have only been increasing the number of errors and illegal options.

Who ever told learning unix is better than running a darned farm?!
 
So, go back to your original version (remove the ^M-s)

add the "sh " if you like (better is to add "sh -x " for debugging purposes) to the monthdays line.

then run your script as follows (make sure you are in the directory containing both scripts):

sh -x yd2ymd 1998213

You'll get a whole lot of debugging info, and if you can't make sense of it all - post the output back here and let us have a look at it.


HTH,

p5wizard
 
dear p5wizard,
thank you for talking business.
Here you are:
=============
Script started on Fri Dec 30 17:17:12 2005
# sh -x yd2ymd 1998321
+ [ X1998321 = X ]
+ dt=1998321
+ expr 1998321 / 1000
+ y=1998
+ expr 1998321 % 1000
+ d=321
+ m=1
+ expr 321 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 1
+ [ X1998 = X ]
+ [ X1 = X ]
+ expr ( 1998 * 10000 ) + ( 1 * 100 ) + 1
+ ymd=19980101
+ expr 19980101 / 10000
+ y=1998
+ expr ( 19980101 % 10000 ) / 100
+ m=1
+ echo 31
+ exit
+ md=31
+ expr 321 - 31
+ d=290
+ expr 1 + 1
+ m=2
+ expr 290 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 2
+ [ X1998 = X ]
+ [ X2 = X ]
+ expr ( 1998 * 10000 ) + ( 2 * 100 ) + 1
+ ymd=19980201
+ expr 19980201 / 10000
+ y=1998
+ expr ( 19980201 % 10000 ) / 100
+ m=2
+ sh yeardays 1998
+ diy=365
+ echo 28
+ exit
+ md=28
+ expr 290 - 28
+ d=262
+ expr 2 + 1
+ m=3
+ expr 262 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 3
+ [ X1998 = X ]
+ [ X3 = X ]
+ expr ( 1998 * 10000 ) + ( 3 * 100 ) + 1
+ ymd=19980301
+ expr 19980301 / 10000
+ y=1998
+ expr ( 19980301 % 10000 ) / 100
+ m=3
+ echo 31
+ exit
+ md=31
+ expr 262 - 31
+ d=231
+ expr 3 + 1
+ m=4
+ expr 231 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 4
+ [ X1998 = X ]
+ [ X4 = X ]
+ expr ( 1998 * 10000 ) + ( 4 * 100 ) + 1
+ ymd=19980401
+ expr 19980401 / 10000
+ y=1998
+ expr ( 19980401 % 10000 ) / 100
+ m=4
+ echo 30
+ exit
+ md=30
+ expr 231 - 30
+ d=201
+ expr 4 + 1
+ m=5
+ expr 201 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 5
+ [ X1998 = X ]
+ [ X5 = X ]
+ expr ( 1998 * 10000 ) + ( 5 * 100 ) + 1
+ ymd=19980501
+ expr 19980501 / 10000
+ y=1998
+ expr ( 19980501 % 10000 ) / 100
+ m=5
+ echo 31
+ exit
+ md=31
+ expr 201 - 31
+ d=170
+ expr 5 + 1
+ m=6
+ expr 170 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 6
+ [ X1998 = X ]
+ [ X6 = X ]
+ expr ( 1998 * 10000 ) + ( 6 * 100 ) + 1
+ ymd=19980601
+ expr 19980601 / 10000
+ y=1998
+ expr ( 19980601 % 10000 ) / 100
+ m=6
+ echo 30
+ exit
+ md=30
+ expr 170 - 30
+ d=140
+ expr 6 + 1
+ m=7
+ expr 140 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 7
+ [ X1998 = X ]
+ [ X7 = X ]
+ expr ( 1998 * 10000 ) + ( 7 * 100 ) + 1
+ ymd=19980701
+ expr 19980701 / 10000
+ y=1998
+ expr ( 19980701 % 10000 ) / 100
+ m=7
+ echo 31
+ exit
+ md=31
+ expr 140 - 31
+ d=109
+ expr 7 + 1
+ m=8
+ expr 109 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 8
+ [ X1998 = X ]
+ [ X8 = X ]
+ expr ( 1998 * 10000 ) + ( 8 * 100 ) + 1
+ ymd=19980801
+ expr 19980801 / 10000
+ y=1998
+ expr ( 19980801 % 10000 ) / 100
+ m=8
+ echo 31
+ exit
+ md=31
+ expr 109 - 31
+ d=78
+ expr 8 + 1
+ m=9
+ expr 78 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 9
+ [ X1998 = X ]
+ [ X9 = X ]
+ expr ( 1998 * 10000 ) + ( 9 * 100 ) + 1
+ ymd=19980901
+ expr 19980901 / 10000
+ y=1998
+ expr ( 19980901 % 10000 ) / 100
+ m=9
+ echo 30
+ exit
+ md=30
+ expr 78 - 30
+ d=48
+ expr 9 + 1
+ m=10
+ expr 48 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 10
+ [ X1998 = X ]
+ [ X10 = X ]
+ expr ( 1998 * 10000 ) + ( 10 * 100 ) + 1
+ ymd=19981001
+ expr 19981001 / 10000
+ y=1998
+ expr ( 19981001 % 10000 ) / 100
+ m=10
+ echo 31
+ exit
+ md=31
+ expr 48 - 31
+ d=17
+ expr 10 + 1
+ m=11
+ expr 17 > 0
+ [ 1 = 1 ]
+ sh -x monthdays 1998 11
+ [ X1998 = X ]
+ [ X11 = X ]
+ expr ( 1998 * 10000 ) + ( 11 * 100 ) + 1
+ ymd=19981101
+ expr 19981101 / 10000
+ y=1998
+ expr ( 19981101 % 10000 ) / 100
+ m=11
+ echo 30
+ exit
+ md=30
+ expr 17 - 30
+ d=-13
+ expr 11 + 1
+ m=12
+ expr -13 > 0
expr: illegal option -- 1
usage: expr [-e] expression
+ [ = 1 ]
[: =: unexpected operator
+ expr -13 + 30
expr: illegal option -- 1
usage: expr [-e] expression
+ d=
+ expr 12 - 1
+ m=11
+ expr ( 1998 * 10000 ) + ( 11 * 100 ) +
expr: syntax error
+ grg=
+ echo
=======================================
By the by, what does your "HTH" mean?

yours`
serguey
 
What operating system is this on? expr doesn't behave that way on any of the operating systems I have tried...

Try changing it to expr -e, perhaps that will prevent it from treating -13 as an option/switch.

HTH means "Hope That Helps".

Annihilannic.
 
looks like your UNIX version's impementation of expr doesn't like "expr -13 + 30" or "expr -17 \> 0".

Your expr assumes an option or flag follows the dash or minus sign.

Perhaps you can circumvent the problem:

try modifying line 22 into:

while [ `expr 0 + $d \> 0` = 1 ]

and see how far you get.

Hope This Helps,

p5wizard.
 
And why not simply this ?
while [ "$d" -gt 0 ]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yes, of course PHV. But I was aiming for a minimal modification. And as this is a script from a tutorial, it looks to me as being an "expr" script exercise...


HTH,

p5wizard
 
dear Annihilannic,

before your question I was sure my OS was fbsd-5.3, but now I just suppose it is so.
I crammed "-e" in after each "expr" I could see in the script yet the message "usage: expr [-e] expression" popped up again.
Thank you for your explaining of "HTH".

yours`
serguey
 
dear p5wizard,

adding "0 \+" before "$d" as you proposed took me some six lines further where I repeated the trick you prompted and the whole script worked! IWI!

"IWI" = It Works Indeed

please receive my gratitude and endless admiration

yours`
serguey
 
dear PHV,

of course you are right: the modification you proposed eliminated the problem and the script worked OK.
Thank you for the insight and the lead to FAQs.

much obliged
yours`
serguey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top