Type casting problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Type casting problem
# 1  
Old 04-03-2012
Java Type casting problem

hi guys,

i m new to shell script..

i dont know how to type cast string into integers and i mention my problem below..

date="21091988"
month=$((${date:2:2})) # extract the month from previous date variable
temp=$(($month*23))

when i am trying to calculate the temp value i got the following error..
09: value too great for base (error token is "09")
*23: syntax error: operand expected (error token is "*23")

i tried with following declaration,
declare -i month

bt above also not works....

i dont know where it gets wrong...

when i m assigning month value less than 8... it works nicely yar.....

i think it s something related to octane.... bt i dont know the extactly dears...

please help me....SmilieSmilieSmilieSmilieSmilieSmilieSmilie
# 2  
Old 04-03-2012
To overcome .. Smilie
Code:
month=$((10#${dat:2:2}))

This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 04-03-2012
Code:
month=${date:2:2}
((temp=${month#${month%%[1-9]*}}*23))

This User Gave Thanks to complex.invoke For This Post:
# 4  
Old 04-03-2012
Here's a work-around. I've added a line to remove zeros from the beginning of $month, if it exists.

Code:
date="21091988"
month=${date:2:2} # This much is sufficient. Double round braces are not required.
month=${month/#0/}
temp=$(($month * 23))

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 04-03-2012
Dear All,

Its working yar... thanks to all dears...... its really interesting and i wanna know some ideas..

month=$((10#${dat:2:2}))

y we need to add "10#" and how this work dear.. please give idea yar......

is that base 10.... converting from base 8 to base 10......
==============================================================
((temp=${month#${month%%[1-9]*}}*23))

hey how this work... please give idea dear....
# 6  
Old 04-03-2012
Quote:
Originally Posted by raadhaakrishnan
Dear All,

Its working yar... thanks to all dears...... its really interesting and i wanna know some ideas..

month=$((10#${dat:2:2}))

y we need to add "10#" and how this work dear.. please give idea yar......
==============================================================
((temp=${month#${month%%[1-9]*}}*23))

hey how this work... please give idea dear....
$((10#${dat:2:2})) : converting the string to decimal digits
${month%%[1-9]*} : stripping from the first non-zero character until the end of the string, that is, just remain the leading sequentially zeros,
${month#${month%%[1-9]*}} : stripping the leading sequentially zeros!
This User Gave Thanks to complex.invoke For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Very strange output with casting

Hi All, I am having a strange issue. Below is the code snippet. If I print fraction * (double)::pow((double)10,scalingFactor) which is a double I am getting 154 when I type cast that to int as (int)( ((fraction) * ((double)::pow((double)10,scalingFactor)))) it is becoming 153. Not sure why... (0 Replies)
Discussion started by: arunkumar_mca
0 Replies

2. Shell Programming and Scripting

Casting the data command !

Hey guys. 1st problem: I have to create a script for every hour run a script against a DB and then produce results to log file and eventually email. I have to manipulate the date command from something like this date '+%F %H:%M.%s' which produces the below the below result 2014-08-01... (2 Replies)
Discussion started by: mo_VERTICASQL
2 Replies

3. Programming

C++ type-casting a member variable in const methods

Is it permitted to type-cast a member variable passed in as a parameter in a member const method .. I am doing something like : in a return-type method () const { variable other = long(member variable) } this other assigned variable is not updating and I wonder if type-casting in such... (1 Reply)
Discussion started by: shriyer123
1 Replies

4. UNIX for Advanced & Expert Users

How type casting works ?

I am having a doubt with type casting in C. Very often in network programming, we have something like this: char *data = ...; struct iphdr *ip = (struct iphdr *) data; where iphdr is define in netinet/ip.h. Let us say that the size of the content of held by 'data' is much more than... (1 Reply)
Discussion started by: radiatejava
1 Replies

5. Shell Programming and Scripting

Problem with type-casting in awk

Hi I'm trying to write a script which takes an integer as input and checks for files with size > input and displays it on screen. The code I've tried : echo 'Enter the min file size expected' read filesize du -ah <folder name> | awk -F: '$1>int(filesize) {print $1,$2)' is always... (28 Replies)
Discussion started by: vivek.bharadwaj
28 Replies

6. Shell Programming and Scripting

Find problem listing directories even -type f

Hi All, #!/bin/ksh find /home/other -ls -type f -xdev | sort -nrk7 | head -2 >bigfile.txt The above is my script, which writes the large file into a file called bigfile.txt. My script contains only the above two lines. after execution i am getting the output like find: cannot chdir to... (1 Reply)
Discussion started by: Arunprasad
1 Replies

7. Shell Programming and Scripting

casting

Hi everyone, I was wondering how i could cast a floating value to an integer in csh Thanks (0 Replies)
Discussion started by: ROOZ
0 Replies

8. Shell Programming and Scripting

Stuck on a matching, copying type problem

Hi all, I am new to these forum and to scripting in general for that matter. i have been looking through the forums for something that could explain my problem. I have be come pretty familiar with sed and awk but I can not seem to figure this out... I am trying to match data from 3 files but every... (4 Replies)
Discussion started by: derek3131
4 Replies

9. UNIX for Advanced & Expert Users

terminal type problem with cygwin on aix

hey, I use cygwin to connect to AIX 5.2 but when I open vi I get an error saying: ex: 0602-108 cygwin is not a recognized terminal type how can I fix that? I thought cygwin was tty vt100? (1 Reply)
Discussion started by: rein
1 Replies

10. Shell Programming and Scripting

Problem with variable type

Dear All, I am trying to write an script to calculate geometric centre of selected residues of a protein structure. Say I have a PDB file (a text containing the x, y, and z coordinates of atoms of a protein). I want to extract the X coordinates of the atoms belonging to the interested residues... (2 Replies)
Discussion started by: siavoush
2 Replies
Login or Register to Ask a Question