Two of the five camera clips in a folder are under five seconds long. The script below asks ffprobe how long each clip is and is supposed to list the short ones, so they can be looked at before anything is uploaded. Run the way a scheduled job usually runs it, with error output going nowhere, it lists neither of them and exits 0:

#!/bin/bash # List clips shorter than 5 seconds so they can be reviewed before upload. for f in clips/*.mp4; do dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$f") if [ "$dur" -lt 5 ]; then echo "short: $f ($dur s)" fi done echo "checked all clips" 
$ ./short_clips.sh 2>/dev/null; echo "exit status $?" checked all clips exit status 0 

Nothing in that output says anything went wrong. With error output left on the terminal, the same run says plenty:

$ ./short_clips.sh; echo "exit status $?" ./short_clips.sh: line 5: [: 3.000000: integer expression expected ./short_clips.sh: line 5: [: 4.600000: integer expression expected ./short_clips.sh: line 5: [: 7.000000: integer expression expected ./short_clips.sh: line 5: [: 12.000000: integer expression expected [mov,mp4,m4a,3gp,3g2,mj2 @ 0xb826a93f2b00] moov atom not found clips/clip_05.mp4: Invalid data found when processing input ./short_clips.sh: line 5: [: : integer expression expected checked all clips exit status 0 

Every clip produced an error, the comparison did not once come out true, and the last line still says every clip was checked. The script did check every clip. It just did not get one answer it could use.

Disclosure: I make an Android camera app. Nothing below uses its code or describes how it records or stores anything. The clips are test patterns made by the setup script below, and every output is pasted from the scripts shown. Versions:

GNU bash, version 5.1.16(1)-release (aarch64-unknown-linux-gnu) ffprobe version 4.4.2-0ubuntu0.22.04.1 GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1) 
#!/bin/bash # Four test clips of known length and one clip cut off mid-write. mkdir -p clips n=1 for t in 3 4.6 7 12; do ffmpeg -v quiet -f lavfi -i testsrc=size=320x240:rate=25 -t "$t" \ -c:v libx264 -pix_fmt yuv420p "clips/clip_0$n.mp4" n=$((n + 1)) done head -c 20000 clips/clip_04.mp4 > clips/clip_05.mp4 

The fifth clip is the first part of the fourth, cut off the way a file can be when the recorder stops before it finishes writing. Here is what ffprobe prints for each one:

#!/bin/bash # Print what ffprobe gives for each clip, and its exit status. for f in clips/*.mp4; do dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$f" 2>/dev/null) echo "$f: '$dur' (ffprobe exit status $?)" done 
$ ./durations.sh clips/clip_01.mp4: '3.000000' (ffprobe exit status 0) clips/clip_02.mp4: '4.600000' (ffprobe exit status 0) clips/clip_03.mp4: '7.000000' (ffprobe exit status 0) clips/clip_04.mp4: '12.000000' (ffprobe exit status 0) clips/clip_05.mp4: '' (ffprobe exit status 1) 

The single-bracket test only takes whole numbers

ffprobe prints the duration with six decimal places, even for a clip that is exactly three seconds long. The Bash manual's entry for -eq, -lt and the rest of the numeric operators is short on this point: "Arg1 and arg2 may be positive or negative integers." A number with a decimal point is not one, so [ refuses it, prints integer expression expected, and exits with status 2.

Status 2 is not status 1, but if does not tell them apart. Anything other than 0 sends if down the false branch, so a test that could not run looks exactly like a clip that is not short. Status 2 is the one clue, and the script does not look at it.

Double brackets fail in a different way

The usual next move is to switch to [[ ]], or to (( )), on the theory that the newer syntax is more forgiving. The manual says [[ ]] treats these operands differently: "When used with the [[ command, arg1 and arg2 are evaluated as arithmetic expressions (see Shell Arithmetic)." Running all four ways of asking the question on what ffprobe actually prints, with the exit status of each:

#!/bin/bash # Four ways to ask "is this duration under 5?" in bash, on what ffprobe prints. for dur in 3.000000 4.600000 12.000000 ""; do printf 'dur=%-12s' "'$dur'" [ "$dur" -lt 5 ] 2>/dev/null; printf ' [ -lt ]:%s' "$?" [[ $dur -lt 5 ]] 2>/dev/null; printf ' [[ -lt ]]:%s' "$?" (( dur < 5 )) 2>/dev/null; printf ' (( < )):%s' "$?" [[ $dur < 5 ]]; printf ' [[ < ]]:%s\n' "$?" done 
$ ./compare.sh dur='3.000000' [ -lt ]:2 [[ -lt ]]:1 (( < )):1 [[ < ]]:0 dur='4.600000' [ -lt ]:2 [[ -lt ]]:1 (( < )):1 [[ < ]]:0 dur='12.000000' [ -lt ]:2 [[ -lt ]]:1 (( < )):1 [[ < ]]:0 dur='' [ -lt ]:2 [[ -lt ]]:0 (( < )):0 [[ < ]]:0 

Read across the rows:

  • [ -lt ] gives 2 every time, including for the empty string from the broken clip. It does not answer once, but at least it is consistent about it.
  • [[ -lt ]] and (( )) give 1 for every real duration, the same status as false. On the empty string they give 0. The same manual entry says why: an expansion inside [[ ]] can come out empty, and "arithmetic expression evaluation treats those as expressions that evaluate to 0." Zero is less than five, so the one clip that has no duration at all is the one these forms call short.
  • [[ < ]] is not a numeric comparison. The manual's entry for it says: "True if string1 sorts before string2 lexicographically." The string 12.000000 sorts before the string 5 because the character 1 sorts before the character 5, so a twelve-second clip comes out under five seconds. Every row is 0.

The first three forms all print a message when they fail. These are the messages the compare script threw away:

#!/bin/bash # The messages the first three forms print when stderr is not thrown away. dur=3.000000 [ "$dur" -lt 5 ]; echo " -> exit status $?" [[ $dur -lt 5 ]]; echo " -> exit status $?" (( dur < 5 )); echo " -> exit status $?" 
$ ./errors.sh ./errors.sh: line 4: [: 3.000000: integer expression expected -> exit status 2 ./errors.sh: line 5: [[: 3.000000: syntax error: invalid arithmetic operator (error token is ".000000") -> exit status 1 ./errors.sh: line 6: ((: 3.000000: syntax error: invalid arithmetic operator (error token is ".000000") -> exit status 1 

(A zero-padded number such as 08 splits these same three forms a different way, which I wrote up separately in a post about octal clip numbers. A decimal point is its own problem.)

The single-bracket message names the problem. The other two say syntax error, which reads like a typo in the script, and point at .000000 rather than at the value.

Putting the double-bracket form into the real script, with the error output thrown away the way it often is once the messages get annoying, gives a result that is worse than none:

#!/bin/bash # The same check with [[ ]] instead of [ ]. for f in clips/*.mp4; do dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$f" 2>/dev/null) if [[ $dur -lt 5 ]] 2>/dev/null; then echo "short: $f ($dur s)" fi done echo "checked all clips" 
$ ./short_clips_dbl.sh; echo "exit status $?" short: clips/clip_05.mp4 ( s) checked all clips exit status 0 

The two clips that really are short are not listed. The one clip ffprobe could not read is listed as short, with a blank where its duration should be. A cleanup step that deletes or skips clips flagged here would act on the broken file and leave the real short clips alone, and the script would still exit 0.

set -e stops at the broken clip, not at the comparison

Adding set -e at the top of the first script changes where it stops, but not what it finds:

#!/bin/bash # The first script with set -e added at the top. set -e for f in clips/*.mp4; do dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$f") if [ "$dur" -lt 5 ]; then echo "short: $f ($dur s)" fi done echo "checked all clips" 
$ ./short_clips_e.sh; echo "exit status $?" ./short_clips_e.sh: line 6: [: 3.000000: integer expression expected ./short_clips_e.sh: line 6: [: 4.600000: integer expression expected ./short_clips_e.sh: line 6: [: 7.000000: integer expression expected ./short_clips_e.sh: line 6: [: 12.000000: integer expression expected [mov,mp4,m4a,3gp,3g2,mj2 @ 0xb8491cf61b00] moov atom not found clips/clip_05.mp4: Invalid data found when processing input exit status 1 

The four integer errors are still there and still do not stop anything, because a command tested by if is exempt from set -e. What stops the script is the assignment on the fifth clip. ffprobe exits 1 there, so the command substitution fails and set -e ends the script with status 1. That is a useful signal for the broken clip, but it arrives after a loop that found no short clip, and it would have stopped the loop early if the broken clip had been first in the folder.

Hand the comparison to something that knows decimals

awk compares decimal numbers directly, so the comparison can move there. Its exit status can carry the answer back to if: exit !(d < 5) exits 0 when the clip is short and 1 when it is not. awk has its own trap on the same input, though:

#!/bin/bash # awk compares decimals correctly, and also compares an empty string as 0. for dur in 4.600000 12.000000 "" "N/A"; do if awk -v d="$dur" 'BEGIN { exit !(d < 5) }'; then r=yes; else r=no; fi printf 'dur=%-12s under 5 per awk: %s\n' "'$dur'" "$r" done 
$ ./awk_empty.sh dur='4.600000' under 5 per awk: yes dur='12.000000' under 5 per awk: no dur='' under 5 per awk: yes dur='N/A' under 5 per awk: no 

The decimals compare correctly. The empty string also comes out under five, exactly as it did with [[ ]], and a non-numeric placeholder such as N/A comes out not under five. Neither answer means anything, so the value has to be checked before it is compared.

The version below does three things the first script did not: it treats an ffprobe failure as a missing value instead of letting it through, it checks that the value looks like a number before comparing it, and it keeps count of the clips it could not read and exits non-zero if there were any:

#!/bin/bash # List clips shorter than 5 seconds; report clips whose duration cannot be read. limit=5 short=0; unreadable=0 for f in clips/*.mp4; do dur=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$f" 2>/dev/null) || dur="" if [[ ! $dur =~ ^[0-9]+(\.[0-9]+)?$ ]]; then echo "unreadable: $f (ffprobe gave '$dur')" unreadable=$((unreadable + 1)) continue fi if awk -v d="$dur" -v l="$limit" 'BEGIN { exit !(d < l) }'; then echo "short: $f ($dur s)" short=$((short + 1)) fi done echo "short=$short unreadable=$unreadable" [ "$unreadable" -eq 0 ] 
$ ./short_clips_fixed.sh; echo "exit status $?" short: clips/clip_01.mp4 (3.000000 s) short: clips/clip_02.mp4 (4.600000 s) unreadable: clips/clip_05.mp4 (ffprobe gave '') short=2 unreadable=1 exit status 1 

Both short clips are listed, the broken clip is reported as unreadable instead of short, and the exit status says something needs a look. The pattern ^[0-9]+(\.[0-9]+)?$ accepts whole numbers and plain decimals and nothing else, so an empty value, N/A or an error message does not reach the comparison.

What to change in a script like this

  • Search for -lt, -gt, -le, -ge, -eq and -ne anywhere the value comes from another program's output. ffprobe durations, bc results and anything read from a CSV are the usual sources of decimals.
  • Do not swap [ for [[ to make the error go away. The error goes away and the wrong answers stay, and an empty value starts counting as zero.
  • Do not use < or > inside [[ ]] for numbers. It compares text.
  • Check the value against a number pattern before comparing it, and decide on purpose what an unreadable clip should do. Here it is reported and the script exits 1.
  • Run the check once with error output visible. The failures from [, [[ -lt ]], (( )) and ffprobe all printed a message; the scripts only hid them because the error output was thrown away. The text comparison and awk's empty string printed nothing, which is why the value check has to come first.

Things I did not test

  • Other Bash, ffprobe and awk versions. I ran the versions listed above. The quotes are from the current online Bash manual, and what I ran matched them, but I did not run another version.
  • Other shells. I ran Bash only. A script that starts with a different shell line may behave differently in each of these cases.
  • Other locales. The manual says the double-bracket < sorts using the current locale. I ran one locale and did not test whether another changes the order of these strings.
  • Other ways a clip can break. The broken clip here is the first part of a good one. A clip damaged some other way may make ffprobe print something other than an empty line, which the number pattern would still reject.
  • Real uploads or deletions. Nothing was uploaded or deleted. The scripts only print.

Sources. Quotes are from the GNU Bash Reference Manual page Bash Conditional Expressions. Outputs are pasted unedited from the scripts above; only the hex address in ffprobe's error message changes between runs.

If you want an old Android phone recording in the background so there is a folder of clips for a script like this to check, the app I make is Background Camera RemoteStream on Google Play. Nothing above depends on it.

Of the four comparisons, the one that filled the screen with integer expression expected was the only one that never put a wrong clip on the list.