I'm trying to get a shell script written for busybox on one of our embedded devices to work. The line that's causing problems is below and I've included the output of running it in a shell with the -x flag.
if [ cut -d : -f 1-3 /sys/class/net/eth0/address | grep -q -i 01:02:03 ]; then
# Do stuff
# sh -x ./script
+ [ cut -d : -f 1-3 /sys/class/net/eth0/address
sh: missing ]
+ grep -q -i 01:02:03 ]
grep: ]: No such file or directory
The problem is that you're using
[ as if it were syntax, whereas in the shell it is a command that requires specific arguments to be passed to it.
I think that the fix should be as simple as removing the
[ and corresponding
] (which is the last argument that
if cut -d : -f 1-3 /sys/class/net/eth0/address | grep -q -i 01:02:03; then
In the shell,
if works by looking at the return code of a command, whether it be
[ or any other.
By the way, I don't think that the
-i option to
grep is doing anything useful in this case, so you can remove it.
Note that your condition can be simplified (thanks to chepner):
if grep -q '^01:02:03(:|$)' /sys/class/net/eth0/address; then
Match the start of the line, followed by the part you already had, followed by either a colon or the end of the line.
if [ "$(cut -d : -f 1-3 /sys/class/net/eth0/address)" = 01:02:03 ]; then
Do a direct string comparison on the output of the cut command, rather than piping to grep.