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!

Converting a string to integer in Korn shell 2

Status
Not open for further replies.

rogers42

Technical User
Mar 30, 2007
64
CA
Hi Folks,

I need help to convert a string to integer so I can manipulate the numbers

My Korn shell script is as follows

typeset -i today=`date +%d`
print $today
yesterday=$today-1
print $yesterday

I get "21 -1" for yesterday

Thanks in advance

rogers42

 
See the [tt]let[/tt] builtin command in the [tt]ksh[/tt] man page.

[tt]let yesterday=today-1

(( yesterday=today-1 ))[/tt]

What you are doing is just string concatenation...



HTH,

p5wizard
 
In the Korn shell, "[tt]typeset[/tt]" can also force a variable to be an integer.
Code:
#!/bin/ksh

typeset -i TODAY=$(date '+%d')
typeset -i YESTERDAY

(( YESTERDAY = TODAY - 1 ))

print $YESTERDAY


 
Sambones said:
In the Korn shell, "typeset" can also force a variable to be an integer.

Granted, but you still need to do the arithmetic with [tt]let[/tt] or the [tt](( ))[/tt] equivalent for [tt]let[/tt] ...

HTH,

p5wizard
 
Yup, I wasn't contradicting what you said, just adding to it.

Of course after making my post, I noticed that he had "typeset -i" in his example already, so I was probably stating what he already knew.

Sorry!

 
Another way in ksh:
yesterday=$(($(date +%d)-1))

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Would it work for the first of the month?
No.
Have a look here:
faq822-4802

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top