Home > Back-end >  How to stop bash script if numberoflines < 1000? [duplicate]
How to stop bash script if numberoflines < 1000? [duplicate]

Time:09-16

I've this script. I must delete from a file, the lines between 48 and a variable value only if the total number of lines of the file is > 1000000. If I execute it, it deletes all the lines after line 48, also if the number of lines of the file is < 1000000. Where am I wrong?

#!/bin/bash
while true
do
numberoflines=$(sed -n '$=' app.log)
echo number of line is $numberoflines
    if [[ $numberoflines > 1000000 ]] ; then
        if [ $((numberoflines%2)) -eq 0 ]
        then
        echo "Number is even."
        numberoflinestodelete=$(((numberoflines)/2))
        sed -i -e "48,${numberoflinestodelete}d" app.log
        else
        echo "Number is odd."
        numberoflinestodelete=$(((numberoflines 1)/2))
        sed -i -e "48,${numberoflinestodelete}d" app.log
        fi
    fi
done

CodePudding user response:

Change this line:

if [[ $numberoflines > 1000000 ]] ; then

to become:

if [ "$numberoflines" -gt 1000000 ] ; then

> is for compare strings, -gt is for compare numbers. And quoting variables is also good practice

  • Related