Integer in conditional ksh statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Integer in conditional ksh statement
# 1  
Old 10-26-2011
Integer in conditional ksh statement

Hi,

This is confusing me and I would appreciate some help.

Consider this script

Code:
 
#!/bin/ksh
typset -i stat=`ls -l $WORK/stat |wc -l`
if (( $stat < 20 ))
then
echo true
else
echo false
fi

If the value of $stat is say 29. When I run this above it echos true and not false. How can this be, I know $stat is an integer as you can do the expr $stat + 1 and it increases.

This is really bugging me and I bet it is something really obvious
# 2  
Old 10-26-2011
Sorry, but why don't you use:
Code:
-lt

Code:
#!/bin/ksh
typset -i stat=`ls -l $WORK/stat |wc -l`
if [ $stat -lt 20 ]
then
echo true
else
echo false
fi

# 3  
Old 10-26-2011
I've tried everything

[ $stat -lt 20 ]
(( $stat<20))

when stat = 29 they all validate as true, does not make sense
# 4  
Old 10-26-2011
I don't use typeset, try the following:

Code:
#!/bin/ksh
stat=`ls -1 $WORK/stat |wc -l`
if [ $stat -lt 20 ]
then
echo true
else
echo false
fi

Sorry, but what you are trying to validate? Maybe there are other ways to do what you want.
# 5  
Old 10-26-2011
I took out the typeset and it just doesn't work, really weird. I was thinking if the command ls -l $WORK/stat |wc -l does not return an integer.

Anyway what I am trying to do is say....if the number of files is less than a threshold then do something else sleep x seconds and check again.
# 6  
Old 10-26-2011
Maybe this helps you:

Code:
#!/bin/ksh
stat=`find ${WORK}/stat -maxdepth 1 -type f | wc -l`
if [ ${stat} -lt 20 ]
then
echo true
else
echo false
fi

# 7  
Old 10-26-2011
Does your original code have typeset, or actually typset?

I can't get it to break (with typeset) on RHEL ksh93s+.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional statement in SFTP

Hi, I wanted to know if we can use conditional statements like if--else--fi inside an automated SFTP script session. (1 Reply)
Discussion started by: jhilmil
1 Replies

2. UNIX for Advanced & Expert Users

conditional statement

I need to implement something like this. 1) search for a file(say *.doc) from a path (say /home/user/temp) 2) if file found & if file size > 0 : yes --> file valid else : print file not valid. I am trying to implement something like this, but seems i am terribly wrong somewhere.. ... (1 Reply)
Discussion started by: animesharma
1 Replies

3. Shell Programming and Scripting

Conditional IF statement with a wildcard

I'm trying to create a shell script that will check for new files and or folders and if it exist then copy them to a different directory. Does anyone have a idea? if ; then echo "Copying files from the Upgrade Server" cp -Ruavp /home/upgrade/hex-code/*... (7 Replies)
Discussion started by: binary-ninja
7 Replies

4. UNIX for Dummies Questions & Answers

Conditional statement in bash

I want to combine 2 conditional statements by using -o in bash, but it won't work. if ; then echo "The number needs to be between 0 and $nr" fi Each time i execute the file it says: ./selectCitaat: line 10: syntax error near unexpected token `$1' (3 Replies)
Discussion started by: doc.arne
3 Replies

5. Shell Programming and Scripting

if conditional statement

Hi, I have a script like this: sample.sh mapping=$1 if then echo "program passed" fi I'm running the above script as ./sample.sh pass The script is not getting executed and says "integer expression expected" Could anyone kindly help me? (2 Replies)
Discussion started by: badrimohanty
2 Replies

6. UNIX for Dummies Questions & Answers

Problem with conditional statement

Hi, I'm getting a "bad number" error from the following conditional if statement. I understand the results of the grep command are not being treated a an integer but am unsure of the correct syntax. Any help would be appreciated. if Thanks (2 Replies)
Discussion started by: dlafa
2 Replies

7. Shell Programming and Scripting

conditional statement

Hi all, The following code is to find if a list of numbers from one file are within the range in another file. awk -F, '\ BEGIN { while ((getline < "file2") > 0) file2=$3 } {for (col1 in file2) if ($0>=30 && $1<=45) print $0} ' FILE1 But where I have the number 30 and 45, I... (3 Replies)
Discussion started by: dr_sabz
3 Replies

8. Shell Programming and Scripting

conditional statement

Hi Does Unix have a conditional statement like Java as follows: Condition ? Statement1 : Statement2 Thanks (8 Replies)
Discussion started by: lalelle
8 Replies

9. Shell Programming and Scripting

quoting in conditional statement

can somebody help, what quote i should use in below statement or what wrong of it ? the 1st (*) is a char, the 2nd and 3rd (*) is a wildcard if ] && ] && ] ................^ .............^ then echo "ok" fi thanks in advance. (2 Replies)
Discussion started by: 3Gmobile
2 Replies

10. Shell Programming and Scripting

awk conditional statement

how can i use awk or sed to do a conditional statement, so that HH:MM if MM not great than 30 , then MM=00 else MM=30 ie: 10:34 will display 10:30 10:29 will display 10:00 a=$(echo 10:34 | awk ......) Thanks in advance (10 Replies)
Discussion started by: 3Gmobile
10 Replies
Login or Register to Ask a Question