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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to convert character to number in the shell?

Status
Not open for further replies.

stibitz

MIS
Oct 3, 2007
2
0
0
US
The shell seems to know when I cut a "number" from a string, that it is indeed not a number. Is there a way to use it as a number? Convert it through another variable assignment to be a number?
 
Perhaps you should post an example of your problem. In ksh, this cuts out 123456:

Code:
myvar=$(echo "123 hi there 456" |sed 's/[^0-9]//g')
echo "$myvar"
 
answering my own question here...

secs=$(print $elapsed | cut -c5-6)
where $elapsed might = "14:21:36"
at this point, secs="36"
and temp=$(( $secs * 60 )) won't work.
if first invocation of secs is preceded by,
integer secs
shell converts(?) it to a number when assigned.
Well, whatever it does, it now works in shell arithmetic.
 
I think you meant to cut -c7-8. I don't recall ever having to force a variable containing numerics to 'integer' to allow me to perform arithmetic.

Annihilannic.
 
Looks like you are using ksh88 or ksh93. If so, try the following
Code:
$ elapsed="14:21:36"
$ print $(( ${elapsed:6} * 60 ))
2160
$
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top