5

I have the following script that extracts data from the EXIF command on my busybox ash-based system. I parse the date tag from the end of the file and use case as a final check to ensure the format is correct. My problem is the if statement at the end always seems to return true; i.e. d="$e - Jebby (exif" always runs. If I echo $en and echo $dn it shows $en is greater than $dn but the if statement still runs the d="$e - Jebby (exif".

x=<valid filename>
e=$(exif -d -m -t 0x9003 -- "$x" 2>/dev/null)
let l=${#e}-18
e=$(expr substr "$e" $l 19)
e="${e%[ ][0-2][0-9][:][0-6][0-9][:][0-6][0-9]*}" 
e="${e##*[!1-2][!09][!0-9][!0-9][!:][!0-1][!0-9][!:][!0-3][!0-9]}"
e=$(expr substr "$e" 1 4)$(expr substr "$e" 6 2)$(expr substr "$e" 9 2)
case "$e" in 
   [1-2][09][0-9][0-9][0-1][0-9][0-3][0-9])
     let en=$e+3
     ds="${d% - Jebby (}"
     let dn=$ds
     if [ $en -lt $dn ]; then
        d="$e - Jebby (exif"
     fi
esac
agc
  • 7,045
  • 3
  • 23
  • 53
Wags
  • 317
  • 3
  • 10
  • 1
    Are you _sure_ you're using `ash`, not `bash` or some other shell? AFAIK `ash` has no `let` built in. – terdon Jan 19 '14 at 15:25
  • It is a Synology NAS running busybox which my understanding is close to ash shell. Let is present but declare is not. – Wags Jan 19 '14 at 16:48
  • 1
    Ah, OK, my local version of `ash` gives various error messages when trying to run your code so I can't really test. You should probably quote the variables you are comparing: `if [ "$en" -lt "$dn" ];` – terdon Jan 19 '14 at 16:50
  • I don't know what `ds="${d% - Jebby (}" is supposed to expand to, but when you assign that value to dn, it doesn't look like a number, so the numeric `lt` comparison fails in unexpected ways. Echo out the values of $en and $dn for a failing case to confirm. – Jeff Schaller Aug 08 '16 at 01:29
  • `let` works fine in `ash`. If you want to try yourself you can download a busybox binary here: https://busybox.net/downloads/binaries/ – michas Dec 03 '16 at 10:10
  • @michas, How would a `busybox` binary explain what's missing in `ash`? I've never seen any version of `ash` that includes a `let`, if you know of one, please include a URL to that version. – agc Dec 03 '16 at 13:49
  • 1
    @agc busybox is a multicall binary which is its own `ash`. Just take the binary from the link above and run `./busybox ash`. – michas Dec 03 '16 at 13:52
  • @michas, thanks, that explains it. Have tweaked OP somewhat to reflect `busybox ash` invocation, but it may need more rewording to clarify this fact. – agc Dec 03 '16 at 13:58

1 Answers1

2

For me comparison in ash works fine:

./busybox ash
$ [ 1 -lt 2 ] && echo true || echo false
false
$ [ 3 -lt 2 ] && echo true || echo false
true
$ a=1 b=2 c=3
$ [ $a -lt $b ] && echo true || echo false
true
$ [ $c -lt $b ] && echo true || echo false
false
$ 

What exactly is the content of your variables? You are aware that -lt compares numerically?

You might also do set -x to see what is going on.

michas
  • 21,190
  • 4
  • 63
  • 93
  • On my BusyBox v1.22.1 the first one actually returns true and the second false ("1 less than 2" is supposed to be truthy, i.e. zero). On an unrelated note, sad to see this question closed because it is genuinely helpful. – Norrius Dec 12 '18 at 18:33