1

I have the following code and I get the Error "line 9: [: not found":

#!/bin/sh
msg=$(dmesg | tail -n1)
echo "$msg"
if [ "$msg" = "Tasklet grp12" ]
then
    echo "Test was successful, Strings are equal."
else
    echo "Test failed, Strings are not equal."
fi
Simon Rechermann
  • 467
  • 3
  • 18
  • That doesn't seem to be the code that causes your error, seeing that the lines don't match. – Benjamin W. Jan 08 '21 at 14:55
  • `[` is, like every other command busybox can build into its single self-call binary, a compile-time option. – Charles Duffy Jan 08 '21 at 14:58
  • Make sure your script doesn't contain any hidden characters and isn't using unicode lookalikes; but once that's done it may be time to investigate how your specific copy of busybox was compiled. – Charles Duffy Jan 08 '21 at 14:59
  • @CharlesDuffy busybox lets you disable the command (or at least, require it to appear as an external command)? I'm going to stop facetiously suggesting that `[` shouldn't exist. – chepner Jan 08 '21 at 15:52
  • @SimonRechermann, btw, as an aside, `dmesg | tail -n1` requires `dmesg` to write all its output to a FIFO, whereafter `tail` reads all those lines and copies out the last one. If by contrast you have something like `/var/log/dmesg` (as a seekable regular file), `tail -n1 /var/log/dmesg` can skip right to the end of the file and only read the last block, and thus can be faster to run. – Charles Duffy Jan 08 '21 at 17:02
  • *'unlink $(which [)'* should disable busybox applet on runtime – alecxs Jan 19 '21 at 22:53

1 Answers1

-1

thanks to Charles Duffy! I had to check the box "Builtin version of 'test'" in the shell section of the make menuconfig menu of busybox, to enable string comparison. Now the code works. Since I had grep activated I first tried another solution which also worked but has a bad performance for sure:

#!/bin/sh
msg=$(dmesg | tail -n1)
echo "$msg"
tasklet="Tasklet grp12"
if ( echo "$msg" | grep "^$tasklet$" )
then
    echo "Test was successful, Strings are equal."
else
    echo "Test failed, Strings are not equal."
fi
Simon Rechermann
  • 467
  • 3
  • 18