5

I would like to have a bash script that checks if a file has more than # amount of lines but i have not yet got it working right and I'm not so sure on how to do it. I've never used bash before.

right now i use: linesStr=$(cat log | wc -l) to get the amount of lines in the file (expect it to be a string). when echo'ing it gives me the number 30 which is correct.

but since its most likely a string it doesnt do the if-statement, so i need to have linesStr converted into a int called linesInt.

I also have the feeling the if-statement itself is not done correctly either.

#!/bin/bash

linesStr=$(cat log | wc -l)
echo $linesStr

if [$linesStr > 29]
    then echo "log file is bigger than 29 lines"
    #sed -i 1d log
fi

I would appreciate if anyone can give me a simple beginners solution.

3
  • You can just do wc -l < log. Commented Feb 11, 2017 at 23:09
  • 1
    wc -l < log returns "30 log" and $(cat log | wc -l) just gives "30" so I'd prefer to use that instead Commented Feb 11, 2017 at 23:31
  • 1
    Sorry, that was a bug in my original comment. wc -l log prints 30 log, but wc -l < log just prints 30. Commented Feb 12, 2017 at 1:04

4 Answers 4

11
  1. No need for cat.
  2. Lack of spaces around [ and ].
  3. Use a numeric comparison operator instead of the redirect operator.

Here is a working script.

#!/bin/bash

linesStr=$( wc -l < log )

if [[ "$linesStr" -gt "29" ]]; then
    echo Foo
fi
Sign up to request clarification or add additional context in comments.

10 Comments

No need for awk.
Try it without.
With your script i get: -bash: test.bash: line 15: syntax error near unexpected token fi' -bash: test.bash: line 15: fi'
why using awk when you can do something like this wc -l < log
Either with wc or with awk , we can avoid sub shells and pipes: awk 'END {print NR}' log is the same as wc -l <log = prints the number of lines directly.
|
2

your if block of code is wrong if [$linesStr > 29] there should be a space after [ and before ]

#!/bin/bash

linesStr=$(wc -l < log )
echo $linesStr

if [[ $lineStr -gt 29 ]];then 
    echo "log file is bigger than 29 lines"
fi

it is advisable that you always use [[ ]] with an if statement rather than using [ ]. Whenever you want to compare integers dont use > or <, use -gt -ge -lt -le. And if you want to do any form of mathematical comparison it is advisable that you use (( )).

(( lineStr > 29 )) && {
    # do stuff
}

you should also note that you don't need the bash comparison operators or getting the value of a variable with $ when using (( ))

1 Comment

Comparing numbers in bash (man bash - arithmetic evaluation) can be done with (( )) for hard coded numbers but with variables there is a bug: If one of the variables holds not valid number (i.e text) the result of arithmetic evaluation is incorrect. Test it with : a=4;b="3";(($a>$b)) && echo "ok" || echo "not ok" (this works fine) and then repeat the test by assigning b="asd".
1

There are no string or integer types to convert. The problem is that you're using the wrong comparison operator. For numeric comparison use if [ $linesStr -gt 29 ]. Read man bash section CONDITIONAL EXPRESSIONS for available operators.

1 Comment

The other problem is that [ is a command (and ] is an argument of that command), so you need spaces around [ and ]. [$linesStr will try to run a command named [30.
0
(( $(wc -l < log) > 29 )) && echo too long

1 Comment

While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.