need help converting string to number for calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help converting string to number for calculation
# 1  
Old 04-12-2007
need help converting string to number for calculation

I've searched the forum and google, but can't see an answer to this simple problem. Here's my small test script:
Code:
#!/bin/csh
echo "enter a number:"
read num
echo "you entered $num"
set num = `expr $num + 1`
echo new value is $num

can someone show me how to do this calculation? note that in the real life script I'm working on, variables will be input, so something like
Code:
set num = 1

won't work for me

here's an example of the execution
Quote:
sh ./test.sh
enter a number:
12
you entered 12
new value is 12
Thanks in advance for your time and help.
Tom
# 2  
Old 04-12-2007
tpatput,
Is this what you need:
num=$num+1
# 3  
Old 04-12-2007
thank you for your response shell_life.

I modified the test script to this and it worked under sh shell
Code:
#!/bin/sh
echo "enter a number:"
read num
echo "you entered $num"
num=`expr $num + 1`
echo the result is $num

execution:
Quote:
sh-2.05$ ./test.sh
enter a number:
12
you entered 12
the result is 13
sh-2.05$ echo $0
/bin/sh
I couldn't run just num=$num+1. Maybe I'm getting screwed up with the login shell and the script shell.

is there a way to do this in csh (or tcsh)?

Thanks again for your help.
Tom
# 4  
Old 04-13-2007
Quote:
Originally Posted by tpatput
thank you for your response shell_life.

I modified the test script to this and it worked under sh shell
Code:
#!/bin/sh
echo "enter a number:"
read num
echo "you entered $num"
num=`expr $num + 1`
echo the result is $num

execution:


I couldn't run just num=$num+1. Maybe I'm getting screwed up with the login shell and the script shell.

is there a way to do this in csh (or tcsh)?

Thanks again for your help.
Tom
Code:
#/bin/csh
echo -n "enter a number:"
set num = $<
echo "you entered $num"
set num = `expr $num + 1`
echo the result is $num

# 5  
Old 04-13-2007
anbu23, shell_life, thanks so much for your quick responses. You've really helped and it is very much appreciated.

Tom
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Converting negative number to positive in a file

Hi ALL, I am having semi column separated file as below. I am having negative values for the records starting with 11095. How can I convert that positive number I tried this below seems not working sed 's/ \(*\)$/ -\1/;t;s/\(.*\)-/\1/ myfile myfile... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

2. Shell Programming and Scripting

Decimal number calculation problem

I have a below snippet of code from my perl script and its causing a problem when the output of $lTAX is 0 (zero) its getting displayed as -0.00. I want output to be 0 not -0.00. Any help would be appreciated. #!/usr/bin/perl my $lTotA = 50.94; my $lVatA = 8.49; my $lAllocD; my $lAdjNr =... (4 Replies)
Discussion started by: Devesh5683
4 Replies

3. UNIX for Dummies Questions & Answers

converting string to number in shell script

Hi, I am having a problem in converting a string to number so I can preform arithmetic operations. timeTot=0 timeTmp=$(cat idsOutput | grep 'Run time' | cut -c 36-39) timeTot=$ #This is line 28 echo "total RunTime=" $timeTot this is the error msg: ./ids2.sh: line 28: 0+1.35: syntax... (8 Replies)
Discussion started by: turki_00
8 Replies

4. Shell Programming and Scripting

KSH SHELL: problem calculation number of lines inside compressed file

Hi Gurus, I am working with a korn shell script to simplify some operations of calculation number of lines inside compressed file. The called function (inside a cycle) is the following: ######################################### # F.ne: CheckCount #########################################... (3 Replies)
Discussion started by: GERMANICO
3 Replies

5. Shell Programming and Scripting

Performing a calculation on string to be replaced

Hi, I have a file with occurances of the string "TO_DATE(<number here>,'J')" at random places. I need minus 2400000 from the number and replace the string with "convert(date, dateadd(dd, <new number here>,'16 Nov 1858')". I'm finding this difficult as the string isn't necessarily in the... (9 Replies)
Discussion started by: user_invalid
9 Replies

6. Shell Programming and Scripting

Average calculation based on number of rows

Dear users, I need your support, I have a file like this: 272134.548 6680572.715 272134.545 6680572.711 272134.546 6680572.713 272134.548 6680572.706 272134.545 6680572.721 272134.543 6680572.710 272134.544 6680572.715 272134.543 6680572.705 272134.540 6680572.720 272134.544... (10 Replies)
Discussion started by: Gery
10 Replies

7. Shell Programming and Scripting

Problem in converting number in shell script

Hi All, I am writing a shell script in which I want to convert a number like : Suppose the number is "98487657" and we have to convert it to "98000000", what I want to do is to retain first 2 digits and convert all remaining digits to "0". Number could be of any length (length... (4 Replies)
Discussion started by: amitanshu.verma
4 Replies

8. Shell Programming and Scripting

Help needed in converting number to bit

Hi, I have the following file input:- 542 4795 61 543 4795 61 544 0 0 545 292 ... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

9. Shell Programming and Scripting

Converting Stirngs to number

I have written a script to compare the values of string (which actually are numbers) but it is not giving the expected output. Please suggest for the script. #!/usr/bin/ksh sysdate=20071009 echo $sysdate currfile=20071008 echo $currfile if ; then echo "Equal" else echo "Not... (2 Replies)
Discussion started by: raman1605
2 Replies

10. Programming

converting character string to hex string

HI Hi I have a character string which contains some special characters and I need it to display as a hex string. For example, the sample i/p string: ×¥ïA Å gïÛý and the o/p should be : D7A5EF4100C5010067EFDBFD Any pointers or sample code pls. (5 Replies)
Discussion started by: axes
5 Replies
Login or Register to Ask a Question