I often find myself writing something like

  if [ "$somevar" = 123 ] ; then ...

only to realize that that syntax doesn't work in U-Boot shell, and
must be spelled

  if test "$somevar" = 123 ; then

It only takes a few lines of code to support this POSIX-standardized
alias for test, and helps developers focus on their actual problems
instead of dealing with such unexpected quirks of the shell.

Signed-off-by: Rasmus Villemoes <[email protected]>
---
 cmd/test.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/cmd/test.c b/cmd/test.c
index a9ac07e6143..f0645ef98f1 100644
--- a/cmd/test.c
+++ b/cmd/test.c
@@ -63,6 +63,14 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
        char * const *ap;
        int i, op, left, adv, expr, last_expr, last_unop, last_binop;
 
+       if (!strcmp(argv[0], "[")) {
+               if (strcmp(argv[argc - 1], "]")) {
+                       printf("[: missing terminating ]\n");
+                       return 1;
+               }
+               argc--;
+       }
+
        /* args? */
        if (argc < 3)
                return 1;
@@ -212,6 +220,17 @@ U_BOOT_CMD(
        "[args..]"
 );
 
+/*
+ * This does not use the U_BOOT_CMD macro as [ can't be used in symbol names
+ */
+ll_entry_declare(struct cmd_tbl, lbracket, cmd) = {
+       "[",    CONFIG_SYS_MAXARGS, cmd_always_repeatable, do_test,
+       "alias for 'test'",
+#ifdef CONFIG_SYS_LONGHELP
+       " <test expression> ]"
+#endif /* CONFIG_SYS_LONGHELP */
+};
+
 static int do_false(struct cmd_tbl *cmdtp, int flag, int argc,
                    char *const argv[])
 {
-- 
2.53.0

Reply via email to