Friday, July 29, 2022

[SOLVED] Compare number from a txt file with an number in bash script

Issue

I am trying to read text file which contain numbers line by line in

test.txt

10.5
0.52
78.5
29.8
45
13
21.45
0.02
0.99
1.00

I want to read the number one by one and compare it with 30. I am not getting how can I do it.

I tried this but its not working.

#!/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

Please note: I am new to programing


Solution

This line:

        if [[ "$line > 30" | bc) -eq "1" ]]; then

should be:

        if [[ $(echo "$line > 30" | bc) -eq "1" ]]; then


Answered By - Barmar
Answer Checked By - Mildred Charles (WPSolving Admin)