If condition is not working and getting error

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers If condition is not working and getting error
# 1  
Old 01-23-2017
If condition is not working and getting error

Hi Team,

If condition is not working properly and getting below error

Code:
# ./score1.sh
Enter your score [0-100%] ('q' for quit): 102
Enter your score [0-100%] ('q' for quit): q
./score1.sh: line 9: [: q: integer expression expected
Average is: 102%.
Exit.

Actual code

Code:
# Calculate the average of given number.
SCORE=0
AVERAGE=0
SUM=0
NUM=0
while true; do
echo -e  "Enter your score [0-100%] ('q' for quit): \c "
read SCORE;
if [ $SCORE -lt 0  -a $SCORE -gt 100 ]; then
echo "Give correct range "
elif [ $SCORE == q ]; then
echo "Average is: $AVERAGE%."
break
else
SUM=$[$SUM + $SCORE]
NUM=$[$NUM + 1]
AVERAGE=$[$SUM / $NUM]
fi
done
echo "Exit."


Thanks
Torrid.

Last edited by rbatte1; 01-23-2017 at 10:21 AM.. Reason: Added CODE tags
# 2  
Old 01-23-2017
The line if [ $SCORE -lt 0 -a $SCORE -gt 100 ]; then is looking to compare integers. You are giving it letter, which it cannot handle. You would be better to put the quit-test before it.

That said, you also don't check that someone enters the value b or Hello world! in there.

Is this a homework exercise?




Robin
# 3  
Old 01-24-2017
you were partially correct robin.

I am new to shell script and learning through online. when I search for help I got this forum and posted my query.

if made a change in IF statement it is working fine

Code:
if [[ $SCORE -gt 100 || $SCORE -lt 0  ]] ; then

but getting below error when i give 09 as input


Code:
./score1.sh
Enter your score [0-100%] ('q' for quit): 09
./score1.sh: line 12: [[: 09: value too great for base (error token is "09")
./score1.sh: line 12: [[: 09: value too great for base (error token is "09")
./score1.sh: line 18: 0 + 09: value too great for base (error token is "09")
Exit.

Moderator's Comments:
Mod Comment Seriously: Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-24-2017 at 07:06 AM.. Reason: Added CODE tags.
# 4  
Old 01-24-2017
Quote:
Originally Posted by Torrid
you were partially correct robin.
.
.
.
I think he's totally correct. Although you changed the mechanism to evaluate the conditional expression, you still try to do integer calculations / comparisons with string values which may not result in a syntax error, but is a logical one. Avoid that by following his/her advice "to put the quit-test before it".

Some shells (including sh and bash) interpret integers with leading zeroes as octal numbers, so 09 can't be evaluated. bash allows the base specification by adding e.g. a leading 10#.

Last edited by rbatte1; 01-24-2017 at 08:31 AM.. Reason: I'm definately a "he"
These 2 Users Gave Thanks to RudiC 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

If condition on shell not working , not sure what is the mistake I am doing?

I have a requirement to perform specific set of tasks based on server , So I want to have the condition(s) defined based on server. Here is the script I came up with and I have read multiple blogs and couldn`t find any mistake from my script. Can you guide on what I am overlooking here ? ... (2 Replies)
Discussion started by: Varja
2 Replies

2. Shell Programming and Scripting

Error: if condition

please follow below code if ; then its giving me an error as "test1.sh: line 3: r=$(eval echo '$lck_'$TABLE != "") if can anyone tell me how to do inside if condition...with out use any variable.. TIA (6 Replies)
Discussion started by: gnnsprapa
6 Replies

3. UNIX for Dummies Questions & Answers

error in if condition

Hi All, I need to compare the header of the input files which can be comma demilited, tild(~) or Pipr(|) seperated file. I am comparing the file header by taking 1st row and comparing it with input row: Below is the actual code:(ksh script) if then echo $dat >>... (4 Replies)
Discussion started by: abhi_123
4 Replies

4. Shell Programming and Scripting

while condition error:

I'm trying to run following code: while do echo "Hello World" done but I'm getting error on first line: ./test: line 1: syntax error near unexpected token `(' can anyone please tell me who can i run this loop. Also please tell me what will be syntax of do while loop in case i... (11 Replies)
Discussion started by: kashif.live
11 Replies

5. Shell Programming and Scripting

Condition error!

Hi Guys, Can you please help me to check if my condition is correct. if && then if ; then # active SE_USERID_LUZON_5="A" elif ; then # not active SE_USERID_LUZON_5="D" ${ECHO_CMD}... (3 Replies)
Discussion started by: nikki1200
3 Replies

6. Shell Programming and Scripting

Using ssh to transfer file not working inside if-condition

Hi all, ssh uname@remote_server 'cat /tmp/remote_file_name > home_dir/a512386/new/local_file_name' The above given command is working fine. but if i try to move the file only if exists in the remote server i.e) giving the same within if condition it executes but the file is not stored in my... (1 Reply)
Discussion started by: Shri123
1 Replies

7. UNIX for Advanced & Expert Users

If condition and htm not working

checkSync() { CONNECT_STRING=TLDB61/TLDB61@TL10G SQLPLUS_SETTINGS="SET PAGESIZE 0 LINESIZE 1500 ECHO OFF TRIMS ON TAB OFF FEEDBACK OFF HEADING OFF" SQL_RESULT_SYNC_PMCM=`sqlplus -s ${CONNECT_STRING} << EOF ${SQLPLUS_SETTINGS} (SELECT... (2 Replies)
Discussion started by: madfox
2 Replies

8. Shell Programming and Scripting

wild card in if condition not working

Hi, I am using RHEL5. I have following if condition. if In the above condition, if the value of a contains word WARNING, it should match. i.e., WARNING_MESSAGE, CRITICAL WARNING, WARNING ALERT etc. it should match. For b, alert error, ALERT ERROR, ERROR IMMEDIATE ACTION REQUIRED, etc... (2 Replies)
Discussion started by: user7509
2 Replies

9. Shell Programming and Scripting

error in if-else condition

Hi.. Im using the following script to find whether the present day is monday or not. If it is monday it has to do a specific set of things. #!/bin/sh Present_Date=`date` LOAD_DAY=`date -d "$Present_Date" | cut -d " " -f 1` echo $LOAD_DAY if ;then echo "Monday" #statements;... (6 Replies)
Discussion started by: abala
6 Replies

10. Shell Programming and Scripting

If condition not working

Hi Gurus, I have shell script in which i have to check if time is between to possible value or not. For that i am using following line of code if then echo 'Found In Between' echo $ftime ... (5 Replies)
Discussion started by: MANISH KU
5 Replies
Login or Register to Ask a Question