how to convert string to an integer and how to do calculations like add.,sub.,mult. on it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to convert string to an integer and how to do calculations like add.,sub.,mult. on it
# 1  
Old 02-22-2011
how to convert string to an integer and how to do calculations like add.,sub.,mult. on it

How to convert string into an integer or number
For example :
Code:
% set tim = `date`
% echo $tim
Tue Feb 22 16:25:08 IST 2011

here How to increment time by 10 hrs

like 16+10 , here 16 is a string in date cmd. .. how to convert 16 to an integer and added to a another nimber ?

Thanks in advance
krishna chaitanya

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 02-22-2011 at 07:51 AM.. Reason: code tags
# 2  
Old 02-22-2011
date -d '10 hours'
# 3  
Old 02-22-2011
my question is too generic way
see ,, long running jobs are there
i need to check how much time that job run
so how can i convert the string to integer and make evaluation

Thanks
krishna chaitanya
# 4  
Old 02-22-2011
As a rule, you would use $hour for a numeric value vice ${hour} for text.

Code:
# hour=`date +%H`
# echo $hour
10

# hour_plus_10=$(($hour + 10))
# echo $hour_plus_10
20

To use the string version:

# echo "What is your ${hour}-${hour_plus_10}?"
What is your 10-20?


Last edited by mfsteve; 02-22-2011 at 11:09 AM.. Reason: Additional info
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert string number to a integer

I have data as below "ROWS merge process complete. thousand rows changed" I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000. Any help or pointer. Please use CODE tags as required by forum rules! (6 Replies)
Discussion started by: dsravanam
6 Replies

2. Shell Programming and Scripting

Convert string to date and add 20 days

Hi, I have a requirement where I am getting date in string format (20161130). I need to add 20 days(not no. 20) to the above string. The o/p should 20161220. In case of 20170228, it should show 20170320. Could you please help me with the command to achieve this. Note: I am using AIX 7.1... (5 Replies)
Discussion started by: satyaatcgi
5 Replies

3. Shell Programming and Scripting

Convert string to date and add 1 hours

i have some set of date data inside csv files and need to convert the timezone, 08302016113611861 08302016113623442 08302016113541570 08302016113557732 08302016113548439 08302016112853115 08302016113620684 08302016113432827 08302016113630321 date format is : %m%d%Y%H%M%Smilisec ... (2 Replies)
Discussion started by: before4
2 Replies

4. Shell Programming and Scripting

How to read from file and convert from string to integer?

Hi all, I am trying to write a script to be able to Run top command Pick the PIDs that are taking more than 90% of CPU time Get more details like what is the script name and location for that PID Script should run until I manually kill it by ctrl + C I have come up with following script... (3 Replies)
Discussion started by: pat_pramod
3 Replies

5. Shell Programming and Scripting

How to convert string into integer in shell scripting?

Hi All, sessionid_remote=$(echo "select odb_sessionid from sysopendb where odb_dbname='syscdr';" | sudo -u cucluster ssh ucbu-aricent-vm93 "source /opt/cisco/connection/lib/connection.profile; $INFORMIXDIR/bin/dbaccess sysmaster@ciscounity") for sid in $sessionid_remote;do if * ]];... (2 Replies)
Discussion started by: deeptis
2 Replies

6. Emergency UNIX and Linux Support

Convert string to date and add 1

Hi All, I want to convert string in format YYYYMMDD(20120607) to date in unix and add 1 day to it and convert back to string in format YYYYMMDD. Please help. (4 Replies)
Discussion started by: cns1710
4 Replies

7. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

8. Shell Programming and Scripting

Convert to Integer

Hi fellows!! i'm doing something which is not working out for me properly which i don't understand why nowdate=`date +%s` echo $nowdate now the problem how to convert a date which is stored in a variable mydate="22/Oct/2011" mydate=`date -d '$mydate' +%s` it gives error... (11 Replies)
Discussion started by: me_newbie
11 Replies

9. Shell Programming and Scripting

Non-integer calculations in bash

I'm new at scripting but I thought I was getting pretty good at it. I've hit a snag. I try to use expr to compute a fraction say: expr 3 / 4, and I'm getting zero. I guess it's just truncating to the integer, in this case 0, but I need the decimal 0.75. What can I do to compute this value in... (2 Replies)
Discussion started by: jeriryan87
2 Replies

10. UNIX for Dummies Questions & Answers

convert from an integer to a string

i want to convert from an integer to a string..in unix...i am writing a C program with embedded SQL... I remeber using itoa...but for some reason it doesnt work......i cant find it in the manual..... Maybe that is the wrong command..... but i have checked Dev Studio.....and it doest exist in the... (6 Replies)
Discussion started by: mojomonkeyhelper
6 Replies
Login or Register to Ask a Question