expr: An integer value was expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting expr: An integer value was expected
# 1  
Old 12-12-2011
expr: An integer value was expected

Hi,

I am trying to execute a simple script as below to compare a value from a file and copy that line based on a condition.

Code:
while read line
do
code_check = `expr substr "$line" 6 1`

if [$code_check -eq '*']; then
    echo "${line}" >> /temp/bill/push_updated.dat
else
    echo "line ignored"
fi
done < /temp/bill/push.dat > /temp/bill/push_log

but when i am trying to execute the script, i am getting the below error message.

Code:
expr: An integer value was expected.

test_script_push.sh[9]: test: A ] character is missing.

Can somebody please correct me where i am doing the mistake.

Last edited by methyl; 12-12-2011 at 08:35 AM.. Reason: please use code tags
# 2  
Old 12-12-2011
Quote:
Originally Posted by ramkiran77
Hi,

I am trying to execute a simple script as below to compare a value from a file and copy that line based on a condition.

Code:
while read line
do
code_check = `expr substr "$line" 6 1`

if [$code_check -eq '*']; then
    echo "${line}" >> /temp/bill/push_updated.dat
else
    echo "line ignored"
fi
done < /temp/bill/push.dat > /temp/bill/push_log

but when i am trying to execute the script, i am getting the below error message.

Code:
expr: An integer value was expected.

test_script_push.sh[9]: test: A ] character is missing.

Can somebody please correct me where i am doing the mistake.
try like this
Code:
code_check=`expr substr "$line" 6 1`

Code:
if [[ $code_check = "*" ]];


Last edited by ygemici; 12-12-2011 at 08:47 AM..
# 3  
Old 12-12-2011
I agree with this:
Quote:
code_check=`expr substr "$line" 6 1`
But this line needs to be a character comparison:
Code:
if [ "$code_check" = '*' ]; then

Also, lose the outward redirect on the last line - it will destroy your input file!

Also, do you actually have a directory called "/temp". It's usually "/tmp" in unix.
# 4  
Old 12-12-2011
Thanks ygemici & methyl, that changes seems to have worked for the integer value expected error.

But even though it is comparing the strings, it is throwing the below error
Code:
test_script_push.sh[6]: [[*:  not found.

and its just reading all the files in the temp directory (temp directory has been created by me)

Data from the input file as below
Code:
3510212*7646*3343*;
4270203*7646*3343*;
1021782*7666*3343*;
1021956*7642*3343*;
1021948*7642*3343*;
16045*3441*3357*;
16050*3441*3357*;
46810*3379*3363*;
46921*3379*3363*;
46920*3379*3363*;

Please suggest


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 12-12-2011 at 10:28 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 12-12-2011
Please post the current version of the script.

The quotes in my version of the "if" line were important because $code_check is likely to contain an asterisk character which the Shell will expand to a files list if you don't put quotes round it.

Judging by the error message you have space character(s) missing on your "if" line. Look carefully at each line and compare with our suggestions.

Last edited by methyl; 12-12-2011 at 11:19 AM..
# 6  
Old 12-12-2011
Please find below the code from the script that i am using...

Code:
while read line
do
itemcode_check=`expr substr "$line" 6 1`
echo $itemcode_check
if [ $itemcode_check='*' ]; then
echo "$line" >> /lr/lr_lr2pkg/lead/script/testPush/push_updated.dat
else
echo "Line ignored as 6th char is not *"
fi
done < /lr/lr_lr2pkg/lead/script/testPush/push_mod.dat

Also the output file is being written with all the values from the input file, without any difference
Please suggest

Last edited by methyl; 12-12-2011 at 11:25 AM.. Reason: repaired code tags
# 7  
Old 12-12-2011
Quote:
Originally Posted by ramkiran77
Please find below the code from the script that i am using...

code
while read line
do
itemcode_check=`expr substr "$line" 6 1`
echo $itemcode_check
if [ $itemcode_check='*' ]; then
Pay careful attention to the spacing given when you use code given to you. Here you've stripped out all the spaces, causing the if statement to fail.

Code:
if [ "$itemcode_check" = '*' ]; then

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Integer expression expected

Hello , This is the piece of the code which is giving me trouble if ;then exit_proc "${SOURCEDIR}/${OUT_FILE} does not exists or not readable" 2 else word_count=`wc -l < ${SOURCEDIR}/$OUT_FILE` fi if ;then exit_proc "Word_count is more than allowed limit" 1 else... (6 Replies)
Discussion started by: Sri3001
6 Replies

2. Shell Programming and Scripting

Integer expression expected

Hi, I am getting the below error while comparing the month of a file to current month. I am using ls -lrth to get the month of that file and while using the if else condition i am getting the below error.. a1=`ls -lrth abc.txt | awk '{print substr($6,1,3)}'` This gives me the month from... (1 Reply)
Discussion started by: jaituteja
1 Replies

3. Shell Programming and Scripting

Integer expression expected

I need some help with this if but then it says line 11: I don't know what I'm doing wrong (1 Reply)
Discussion started by: margg
1 Replies

4. Shell Programming and Scripting

Expr: non-integer argument

This is my code.... It works correct, but does not work with 4 and 5. My program is about finding average. so when i run 4 5 it gives me error "expr: non-integer argument". But when i say sh average 45 67 it works. Whats wrong?how to fix it? sum=0 n=0 if then for i in $* do if ... (2 Replies)
Discussion started by: Natalie
2 Replies

5. Shell Programming and Scripting

Getting error in bash script; expr $a + 1: integer expression expected

Hi, I am new to shell/bash script. I am trying to run below script #!/bin/bash a=0 b=10 if then echo "a is equal to be" else echo "a is not equal to be" fi MAX=10 while do echo $a a='expr $a + 1' done (1 Reply)
Discussion started by: Mallikgm
1 Replies

6. Shell Programming and Scripting

Integer expression expected

Hi, I have placed the script and running successfully on AIX However in Linux it throws integer expression expected Could some one please help me to fix this MaxThreshold4alert=`echo "$MaxCapacitycnt*(80/100)" |bc -l` echo $MaxThreshold4alert Error: 40.00000000000000000000: integer... (2 Replies)
Discussion started by: ajothi
2 Replies

7. Shell Programming and Scripting

Integer expression expected

Newb here echo "$yesterdaysclose" echo "$close" if ; then echo "stocks moving up" elif ; then echo "stock is moving down" else echo "no change" fi seems to evaluate the floating decimal correctly however returns ./shellscript1.sh: line 17: [: : integer expression expected... (3 Replies)
Discussion started by: harte
3 Replies

8. Shell Programming and Scripting

Non-integer argument in expr

i wrote this simple shell script #!/bin/bash read N1 read N2 expr $N1 + $N2 it work fine in bash and i add it on xinetd for some test but when i try to use in with telnet i got this error : ehsan@debian:~$ telnet 192.168.1.4 1234 Trying 192.168.1.4... Connected to 192.168.1.4.... (14 Replies)
Discussion started by: niasha
14 Replies

9. Shell Programming and Scripting

how to add integer with expr?

i got a file called Marks The format of Marks is: 12345678 5 7 23456789 7 9 3 What can i do with a loop, read expr and echo command to produce a new file like below: 12345678:12 23456789:20 and also when we adding fewer than 3 value with expr, we need to change any null value for... (13 Replies)
Discussion started by: mingming88
13 Replies

10. Shell Programming and Scripting

expr: Integer argument too large

Hi all, In KSH, I have got an error message like, "expr: Integer argument too large" I received this error message when I mutiply two large values and displaying the resultant output. Is there any other altenative way to go with too large values? Kindly let me know asap... Thanks in... (12 Replies)
Discussion started by: iamgeethuj
12 Replies
Login or Register to Ask a Question