[PATCH 0/5] cmd: Add support for command substitution

Sean Anderson seanga2 at gmail.com
Mon Mar 1 00:47:13 CET 2021


This series adds support for command substitution using $(). The motivation
for this is that we have many commands which have a version which prints
some information, and one which sets an environmental variable with that
info. This is a bit clunky, since every time someone wants to grab info
from a command (e.g. [1], [2]), they have to add a version which sets a
variable. Every command also has subtle differences in the syntax of how
the environmental is specified.

The classic way to do this is to redirect the output of the command into a
variable using command substitution. Unfortunately, this approach will not
work in U-Boot for a few reasons. First, we don't have any jobs, so we
can't use pipelines to filter output. In addition, the command output tends
to be very "human-oriented" and would be hard to parse, even if something
like console recording were used. Instead, commands just set
gd->cmd_result. This result is then printed by the caller of cmd_process.

Unfortunately, this means that to support command substitution, commands
must be modified to set cmd_result. However, they already would have needed
modification to support setting an environmental variable.

Printing out the result of the command may not be the best approach here.
If a command which prints out many messages already returns a result, just
printing the result on its own does not clearly convey what the result
means. Perhaps a prefix should be added to printed results? Though this
would require echo to be changed.

This initial series just converts two commands: echo and part uuid. This
showcases the manner in which this new functionality could be implemented.
I think that some helper functions like for env may be useful so that
commands don't have to do some much bookkeeping (like in the current echo
command).

This series depends on [3].

[1] https://patchwork.ozlabs.org/project/uboot/patch/20210226181733.19307-1-farhan.ali@broadcom.com/
[2] https://patchwork.ozlabs.org/project/uboot/patch/20201215165439.13165-1-m.szyprowski@samsung.com/
[3] https://patchwork.ozlabs.org/project/uboot/patch/20210228212951.1175231-1-seanga2@gmail.com/


Sean Anderson (5):
  hush: Print syntax error line with DEBUG_SHELL
  cmd: Add support for (limited) command substitution
  cmd: Convert echo to use cmd_result
  test: hush: Add test for command substitution
  cmd: Convert part uuid to use cmd_result

 cmd/echo.c                        | 33 +++++++++++++++----
 cmd/part.c                        | 18 ++++++++---
 common/cli_hush.c                 | 53 +++++++++++++++++++++++++------
 common/cli_simple.c               |  8 ++++-
 common/command.c                  |  3 ++
 include/asm-generic/global_data.h |  4 +++
 test/cmd/test_echo.c              | 10 +++++-
 7 files changed, 107 insertions(+), 22 deletions(-)

-- 
2.30.1



More information about the U-Boot mailing list