Sponsored Content
Full Discussion: Problem with Maths
Top Forums Shell Programming and Scripting Problem with Maths Post 302070210 by Quesa on Monday 3rd of April 2006 06:30:31 AM
Old 04-03-2006
Problem with Maths

Heres a script i wrote as a bit of practise. What it does is insert a line in the middle of a file. The line being $1 and the file being $2


#!/bin/bash

rm tempfile
touch tempfile

count=1
linenum= `wc -l < $2`
if [ "$linenum" -eq 0 ]
then
echo $1 >> $2
else

even=`expr "$linenum" % 2`
if [ "$even" -eq 0 ]
then
midline=`expr "$linenum" / 2`
else
midline=`expr "$linenum" / 2`
midline=`expr "$midline" + 0.5`
fi

while [ "$linenum" -gt 0 ]
do
sed -n '$count p' $2 >> tempfile

if [ "$linenum" -eq "$midline" ]
then
echo $1 >> tempfile
fi

count=`expr "$count" + 1`
linenum=`expr "$linenum" - 1`
done
fi
cat tempfile > $2


However when I run this I get the following errors:

testscript: 0: command not found
testscript: [: : integer expression expected
expr: non-numeric argument
testscript: [: : integer expression expected
expr: non-numeric argument
expr: non-numeric argument
testscript: [: : integer expression expected

This would suggest that the expr argument does not have numbers in it. I know I have the variables in double quotes but this should not matter should it. Can anyone help me with any of these errors.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - maths equation - need help

if input to the perl program is ' ( p * ((a+b) * (c+d))) + q ' it shuld give the output as ' pac + pad + pbc + pbd + q ' .can anyone suggest a way to do this ? (7 Replies)
Discussion started by: Anuj8584
7 Replies

2. AIX

user login problem & Files listing problem.

1) when user login to the server the session got colosed. How will resolve? 2) While firing the command ls -l we are not able to see the any files in the director. but over all view the file system using the command df -g it is showing 91% used. what will be the problem? Thanks in advance. (1 Reply)
Discussion started by: pernasivam
1 Replies

3. AIX

AIX OS problem? network problem?

Dear ALL. I installed AIX OS on customer sites. but Only one site is too slow when I connected telnet, ftp.. Ping is too fast. but telnet and FTP is not connected.. of course i check the configuration file on aix but it's normal. Do any Idea?? thanks in advance. - Jun - (3 Replies)
Discussion started by: Jeon Jun Seok
3 Replies

4. Shell Programming and Scripting

Maths with variables

Hello, I'm trying to write a while loop for a decimal value in tcsh which I know can't be done. Instead I want my increments to be one order of magnitude too large and then divide it by 10 when I use the variable. However, I don't know how to divide my variable and set it as another. set... (1 Reply)
Discussion started by: DFr0st
1 Replies

5. Shell Programming and Scripting

Using IF statements with maths where the input is not an integer

Hi All I've made a few scripts which using GDAL extract the value of a pixel within a given raster. The purpose is to work out the combine value of every pixel. I thought there may have been an easier way to do this but alas! The code below extracts the pixel value at position X Y. The... (3 Replies)
Discussion started by: StudentFitz
3 Replies

6. IP Networking

Problem with forwarding emails (SPF problem)

Hi, This is rather a question from a "user" than from a sys admin, but I think this forum is apropriate for the question. I have an adress with automatic email forwarding and for some senders (two hietherto), emails are bouncing. This has really created a lot of problems those two time so I... (0 Replies)
Discussion started by: carwe
0 Replies

7. Shell Programming and Scripting

Simple maths calculator loop.

Hi, I am trying to make a maths calculator that: 1. Prompts the user for a number. 2. Prompts the user for an operation (add, subtract, divide or multiply) 3. Prompts the user for a number. 4. Prompts the user for another operation (same as above) OR the option to get the result for the... (4 Replies)
Discussion started by: johnthebaptist
4 Replies

8. Shell Programming and Scripting

Maths in shell scripts

Hi, Need help on this. I need to increment a variable by 1 but retain as 2 characters. I am using expr to do additions: NEWSERIAL=`expr $SERIAL + 1` $SERIAL can range from 01-99. After adding "1", I need the result to be 2 characters, eg: 02+1 = 03. By default expr will truncate the... (4 Replies)
Discussion started by: vchee
4 Replies

9. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

10. Shell Programming and Scripting

Calculate the constant e to 14+ decimal places using integer maths.

Hi guys... I am loving this integer maths thing. 64 bit systems are certainly easier than 32 bit, but hey, I don't intend to leave out my fav' platform. Using one of the 'Brothers' methods, URL inside the code. #!/bin/sh # # #!/usr/local/bin/dash # e_constant.sh # Brother's formula . #... (2 Replies)
Discussion started by: wisecracker
2 Replies
Tcl_ExprLong(3) 					      Tcl Library Procedures						   Tcl_ExprLong(3)

__________________________________________________________________________________________________________________________________________________

NAME
Tcl_ExprLong, Tcl_ExprDouble, Tcl_ExprBoolean, Tcl_ExprString - evaluate an expression SYNOPSIS
#include <tcl.h> int Tcl_ExprLong(interp, expr, longPtr) int Tcl_ExprDouble(interp, expr, doublePtr) int Tcl_ExprBoolean(interp, expr, booleanPtr) int Tcl_ExprString(interp, expr) ARGUMENTS
Tcl_Interp *interp (in) Interpreter in whose context to evaluate expr. const char *expr (in) Expression to be evaluated. long *longPtr (out) Pointer to location in which to store the integer value of the expression. int *doublePtr (out) Pointer to location in which to store the floating-point value of the expression. int *booleanPtr (out) Pointer to location in which to store the 0/1 boolean value of the expression. _________________________________________________________________ DESCRIPTION
These four procedures all evaluate the expression given by the expr argument and return the result in one of four different forms. The expression can have any of the forms accepted by the expr command. Note that these procedures have been largely replaced by the object- based procedures Tcl_ExprLongObj, Tcl_ExprDoubleObj, Tcl_ExprBooleanObj, and Tcl_ExprObj. Those object-based procedures evaluate an expression held in a Tcl object instead of a string. The object argument can retain an internal representation that is more efficient to execute. The interp argument refers to an interpreter used to evaluate the expression (e.g. for variables and nested Tcl commands) and to return error information. For all of these procedures the return value is a standard Tcl result: TCL_OK means the expression was successfully evaluated, and TCL_ERROR means that an error occurred while evaluating the expression. If TCL_ERROR is returned then the interpreter's result will hold a message describing the error. If an error occurs while executing a Tcl command embedded in the expression then that error will be returned. If the expression is successfully evaluated, then its value is returned in one of four forms, depending on which procedure is invoked. Tcl_ExprLong stores an integer value at *longPtr. If the expression's actual value is a floating-point number, then it is truncated to an integer. If the expression's actual value is a non-numeric string then an error is returned. Tcl_ExprDouble stores a floating-point value at *doublePtr. If the expression's actual value is an integer, it is converted to floating- point. If the expression's actual value is a non-numeric string then an error is returned. Tcl_ExprBoolean stores a 0/1 integer value at *booleanPtr. If the expression's actual value is an integer or floating-point number, then they store 0 at *booleanPtr if the value was zero and 1 otherwise. If the expression's actual value is a non-numeric string then it must be one of the values accepted by Tcl_GetBoolean such as "yes" or "no", or else an error occurs. Tcl_ExprString returns the value of the expression as a string stored in the interpreter's result. SEE ALSO
Tcl_ExprLongObj, Tcl_ExprDoubleObj, Tcl_ExprBooleanObj, Tcl_ExprObj KEYWORDS
boolean, double, evaluate, expression, integer, object, string Tcl 7.0 Tcl_ExprLong(3)
All times are GMT -4. The time now is 04:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy