Sponsored Content
Top Forums UNIX for Dummies Questions & Answers converting string to number in shell script Post 302600842 by turki_00 on Wednesday 22nd of February 2012 08:32:38 AM
Old 02-22-2012
Question 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=$[$timeTot+$timeTmp] #This is line 28
echo "total RunTime=" $timeTot


this is the error msg:

./ids2.sh: line 28: 0+1.35: syntax error: invalid arithmetic operator (error token is ".35")
total RunTime= 0
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell to number and to string

Hello Does the unix korn shell provide a function to convert between number and string data-types regards Hrishy (1 Reply)
Discussion started by: xiamin
1 Replies

2. Shell Programming and Scripting

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: #!/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... (4 Replies)
Discussion started by: tpatput
4 Replies

3. Shell Programming and Scripting

Converting Shell Script to HTML

Hi, Im new to shell scripting. My task is to convert shell script feed into html, so basically I have a lot of information in shell script and I want to convert it html. I know you can simply convert the information by hand, but is there any simpler way? Thank you Dave (3 Replies)
Discussion started by: davwel
3 Replies

4. 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

5. Shell Programming and Scripting

How to Check given string is number in shell script?

Hi, Can anyone help me out to check whether the input argument is number? Example: REQUEST_ID="123456" I need to check the REQUEST_ID value is number or string. Thanks in Advance Regards BS (6 Replies)
Discussion started by: balajiora
6 Replies

6. Shell Programming and Scripting

Shell Script to identify the line number containing a particular "string"

Hi, I have a log file, where i am required to identify the line number, where a particular string/line appears in the log file. And then copy 200 lines above that line number to a new file. Can someone provide pointers on how to write this script or what command to be used ? Any... (2 Replies)
Discussion started by: kk2202
2 Replies

7. Shell Programming and Scripting

Unix shell script converting temperatures.

:confused:Please I really need help with an assignment question. I need to write a script that will take the input from a file and convert the number from Centigrade(Celcius) to Fahrenheit or vice versa. Thank you so much. I really need it to be detailed. Please remember the input comes from a file. (1 Reply)
Discussion started by: starter101
1 Replies

8. Shell Programming and Scripting

Converting rows to columns using shell script

I have a script which converts rows to columns. file_name=$1 mailid=$2 #CREATE BACKUP OF ORIGINAL FILE #cp ${file_name}.xlsx ${file_name}_temp.xlsx #tr '\t' '|' < ${file_name}_temp.xlsx > ${file_name}_temp.csv #rm ${file_name}_temp.xlsx pivot_row=`head -1 ${file_name}` sed 1d... (3 Replies)
Discussion started by: andy538
3 Replies

9. Shell Programming and Scripting

Converting a shell script to program

Hi I am new in programming. I have written a shell code, but i want to secure my code. I have tried SHC. It is converting it to binary, but can be converted in plain text again by core dump. I have tried to convert it in rpm by "rpmbuild -bb my.spec" option but the result is same. ... (4 Replies)
Discussion started by: Priy
4 Replies

10. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies
EXPR(1) 						    BSD General Commands Manual 						   EXPR(1)

NAME
expr -- evaluate expression SYNOPSIS
expr expression DESCRIPTION
The expr utility evaluates expression and writes the result on standard output. All operators and operands must be passed as separate arguments. Several of the operators have special meaning to command interpreters and must therefore be quoted appropriately. All integer operands are interpreted in base 10 and must consist of only an optional leading minus sign followed by one or more digits. Arithmetic operations are performed using signed integer math with a range according to the C intmax_t data type (the largest signed integral type available). All conversions and operations are checked for overflow. Overflow results in program termination with an error message on stdout and with an error status. Operators are listed below in order of increasing precedence; all are left-associative. Operators with equal precedence are grouped within symbols '{' and '}'. expr1 | expr2 Return the evaluation of expr1 if it is neither an empty string nor zero; otherwise, returns the evaluation of expr2 if it is not an empty string; otherwise, returns zero. expr1 & expr2 Return the evaluation of expr1 if neither expression evaluates to an empty string or zero; otherwise, returns zero. expr1 {=, >, >=, <, <=, !=} expr2 Return the results of integer comparison if both arguments are integers; otherwise, returns the results of string comparison using the locale-specific collation sequence. The result of each comparison is 1 if the specified relation is true, or 0 if the relation is false. expr1 {+, -} expr2 Return the results of addition or subtraction of integer-valued arguments. expr1 {*, /, %} expr2 Return the results of multiplication, integer division, or remainder of integer-valued arguments. expr1 : expr2 The ``:'' operator matches expr1 against expr2, which must be a basic regular expression. The regular expression is anchored to the beginning of the string with an implicit ``^''. If the match succeeds and the pattern contains at least one regular expression subexpression ``(...)'', the string corresponding to ``1'' is returned; otherwise the matching operator returns the number of characters matched. If the match fails and the pattern contains a regular expression subexpression the null string is returned; otherwise 0. Parentheses are used for grouping in the usual manner. The expr utility makes no lexical distinction between arguments which may be operators and arguments which may be operands. An operand which is lexically identical to an operator will be considered a syntax error. See the examples below for a work-around. The syntax of the expr command in general is historic and inconvenient. New applications are advised to use shell arithmetic rather than expr. EXIT STATUS
The expr utility exits with one of the following values: 0 the expression is neither an empty string nor 0. 1 the expression is an empty string or 0. 2 the expression is invalid. EXAMPLES
o The following example (in sh(1) syntax) adds one to the variable a: a=$(expr $a + 1) o This will fail if the value of a is a negative number. To protect negative values of a from being interpreted as options to the expr command, one might rearrange the expression: a=$(expr 1 + $a) o More generally, parenthesize possibly-negative values: a=$(expr ( $a ) + 1) o With shell arithmetic, no escaping is required: a=$((a + 1)) o This example prints the filename portion of a pathname stored in variable a. Since a might represent the path /, it is necessary to pre- vent it from being interpreted as the division operator. The // characters resolve this ambiguity. expr "//$a" : '.*/(.*)' o With modern sh(1) syntax, "${a##*/}" expands to the same value. The following examples output the number of characters in variable a. Again, if a might begin with a hyphen, it is necessary to prevent it from being interpreted as an option to expr, and a might be interpreted as an operator. o To deal with all of this, a complicated command is required: expr ( "X$a" : ".*" ) - 1 o With modern sh(1) syntax, this can be done much more easily: ${#a} expands to the required number. SEE ALSO
sh(1), test(1) STANDARDS
The expr utility conforms to IEEE Std 1003.1-2008 (``POSIX.1''). The extended arithmetic range and overflow checks do not conflict with POSIX's requirement that arithmetic be done using signed longs, since they only make a difference to the result in cases where using signed longs would give undefined behavior. According to the POSIX standard, the use of string arguments length, substr, index, or match produces undefined results. In this version of expr, these arguments are treated just as their respective string values. BSD
September 9, 2010 BSD
All times are GMT -4. The time now is 01:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy