If inside while loop script error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If inside while loop script error
# 1  
Old 06-23-2011
If inside while loop script error

Code:
#!/bin/bash
var1=0
x=1
while [ $x -lt 10 ]
do
echo "Welcome $x times"
var1=$(prstat -Lc 1 1 | nawk ' /^Total/ {print substr($8,1,length($8)-1)}')
echo "$var1"
if [ $var1 -gt 4 ]
then
break
fi
sleep 6
x=$(( $x + 1 ))
done

giving output:
Code:
bash-3.00$ ./ch1.sh
Welcome 1 times
5.90
./ch1.sh: line 9: [: 5.90: integer expression expected
Welcome 2 times
5.75
./ch1.sh: line 9: [: 5.75: integer expression expected
Welcome 3 times
5.64
./ch1.s
...
..

Please help.....
I am using KSH

Last edited by pludi; 06-23-2011 at 01:13 PM..
# 2  
Old 06-23-2011
use > symbol to compar

Code:
 
bash-3.00$ [ $PI >  5 ] && echo "High" || echo "Less"
High

bash-3.00$ [ "$PI" > "5" ] && echo "High" || echo "Less"
High

bash-3.00$ [ $PI -gt 5 ] && echo "High" || echo "Less"
bash: [: 3.14: integer expression expected
Less

# 3  
Old 06-23-2011
Code:
if [ "$var1" -gt 4 ]

# 4  
Old 06-23-2011
giving same error

Code:
#!/bin/bash
var1=0
x=1
while [ $x -lt 10 ]
do
echo "Welcome $x times"
var1=$(prstat -Lc 1 1 | nawk ' /^Total/ {print substr($8,1,length($8)-1)}')
echo "$var1"
if [ "$var1" -gt 9 ]
then 
break
sleep 6
fi
x=$(( $x + 1 ))
done

O/p

Code:
Welcome 1 times
2.53
./ch1.sh: line 9: [: 2.53: integer expression expected
Welcome 2 times
2.52
./ch1.sh: line 9: [: 2.52: integer expression expected
Welcome 3 times
2.52
./ch1.sh: line 9: [: 2.52: integer expression expected
Welcome 4 times
2.51


Last edited by pludi; 06-23-2011 at 01:14 PM..
# 5  
Old 06-23-2011
You should convert this value to integer like this:

Code:
% var=2.52            

% echo ${var%.*}      
2

% var=$(printf "%.0f\n" $var); echo $var
3

# 6  
Old 06-24-2011
hey guys It is still not working plz help...
Code:
#!/bin/bash
x=1
while [ $x -lt 10 ]
do
echo "Welcome $x times"
var1=$(prstat -Lc 1 1 | nawk ' /^Total/ {print substr($8,1,length($8)-1)}')
echo "$var1"
var=$(printf "%.0f\n" $var);
echo "$var1"
if ( $var1 -gt 10 )
then 
break
fi
sleep 1
x=$(( $x + 1 ))
done

O/p
Code:
Welcome 1 times
6.42
6.42
./ch1.sh: line 10: 6.42: command not found
Welcome 2 times
6.42
6.42
./ch1.sh: line 10: 6.42: command not found
Welcome 3 times
6.42
6.42
./ch1.sh: line 1....


Last edited by Franklin52; 06-24-2011 at 03:45 AM.. Reason: Please use code tags for code and data samples, thank you
# 7  
Old 06-24-2011
why you are not using > symbol ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger. if ] then echo "Only satisfied variable with value" ... (3 Replies)
Discussion started by: prince1987
3 Replies

2. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

3. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

4. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

5. Shell Programming and Scripting

help in running while loop inside a shell script

I have an input file at ./$1.txt with content of seq numbers like : 1234567890 1234589700 . . so on.. I need to take each seq nbr from the input file ,run the query below: select ackname,seqnbr from event where event_text like '%seqnbr( from the input file)' and redirect the... (11 Replies)
Discussion started by: rkrish
11 Replies

6. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

7. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

rsh script with inside a for loop

hi everyone, I have the following problem: the foreach loop inside rsh doesn'work. I have tried the for command but it's not recognized. with the foreach command I don't receive any error, but it doesn't really make the cycle, ignoring the foreach and executing 1 time the echo command. Anyone has... (5 Replies)
Discussion started by: trekianov
5 Replies

10. Shell Programming and Scripting

Error connecting oracle from inside while loop

Hi, I m trying to connect oracle database from inside while loop. I m trying to put the output of sql query in a flat file. Anyone please help me out. cat $FILE_NAME | \ while read da_name do $ORACLE_HOME/bin/sqlplus -s user_name/password@instance << EOF >> $OUTPUT_FILE select... (3 Replies)
Discussion started by: Devesh5683
3 Replies
Login or Register to Ask a Question