Click here to Skip to main content
16,020,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello Everyone,

I am trying to read a file which contains numbers(Floats and integers) line by line, I am able to read the line correctly and it works fine, but when I try to compare it with a different number I am failing at that point

test.txt contains numbers as:
10.5
0.52
78.5
29.8
45
13
21.45
0.02
0.99
1.00


if the number in text file is greater than 30 then print greater else print smaller.

I am new to programming

Thanks in advance

What I have tried:

Here is my code:

#!/bin/bash

file=~/test.txt
while IFS= read -r line
do
    echo $line
        if [[ "$line > 30" | bc) -eq "1" ]]; then
            echo Greater
        else
            echo Smaller
        fi
done < $file
Posted
Updated 26-Jul-22 11:54am

1 solution

You can use normal comparison operators inside a bash [[ ]] expresion.
Try
Bash
if [[ $line > 30 ]]; then
    ...
fi
 
Share this answer
 
Comments
CPallini 27-Jul-22 2:27am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900