Skip to contentSkip to conversation

cmd_nvedit.c: Add env exists command

PATCH In review Andrew Ruder 4 participants 4 messages Oct 23, 2013 – Nov 8, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd_nvedit.c: Add env exists command

env exists is a way to test (in hush) if an environment variable exists. A workaround existed using printenv but this new command doesn't require all the stdout/stderr redirection to prevent printing information to the screen.

Example:

$ set testexists 1
$ env exists testexists && echo "yes"
yes
$ env exists testexists || echo "no"
$ set testexists
$ env exists testexists && echo "yes"
$ env exists testexists || echo "no"
no
$
---
 README              |  1 +
 common/cmd_nvedit.c | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)
README +1 -0
index 09662a4..0718459 100644
@@ -843,6 +843,7 @@ The following options need to be configured:
 		CONFIG_CMD_ELF		* bootelf, bootvx
 		CONFIG_CMD_ENV_CALLBACK	* display details about env callbacks
 		CONFIG_CMD_ENV_FLAGS	* display details about env flags
+		CONFIG_CMD_ENV_EXISTS	* check existence of env variable
 		CONFIG_CMD_EXPORTENV	* export the environment
 		CONFIG_CMD_EXT2		* ext2 command support
 		CONFIG_CMD_EXT4		* ext4 command support
common/cmd_nvedit.c +23 -0
index ba9ba16..0d4d02c 100644
@@ -1059,6 +1059,23 @@ sep_err:
 }
 #endif
 
+#if defined(CONFIG_CMD_ENV_EXISTS)
+static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
+		       char * const argv[])
+{
+	ENTRY e, *ep;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	e.key = argv[1];
+	e.data = NULL;
+	hsearch_r(e, FIND, &ep, &env_htab, 0);
+
+	return (ep == NULL) ? 1 : 0;
+}
+#endif
+
 /*
  * New command line interface: "env" command with subcommands
  */
@@ -1094,6 +1111,9 @@ static cmd_tbl_t cmd_env_sub[] = {
 	U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
 #endif
 	U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
+#if defined(CONFIG_CMD_ENV_EXISTS)
+	U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
+#endif
 };
 
 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
@@ -1136,6 +1156,9 @@ static char env_help_text[] =
 #if defined(CONFIG_CMD_EDITENV)
 	"env edit name - edit environment variable\n"
 #endif
+#if defined(CONFIG_CMD_ENV_EXISTS)
+	"env exists name - tests for existence of variable\n"
+#endif
 #if defined(CONFIG_CMD_EXPORTENV)
 	"env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
 #endif
Signature · 1 line
1.8.4.rc3
#1 lore raw

PATCH 1/1 cmd_nvedit.c: Add env exists command

Dear Andrew Ruder,

Quoted 4 lines · Andrew Ruder

env exists is a way to test (in hush) if an environment variable exists. A workaround existed using printenv but this new command doesn't require all the stdout/stderr redirection to prevent printing information to the screen.

I was aware of the printenv workaround, and have used it. I share your misgivings about the stdout/stderr redirection, it seems clumsy, at best.

I have, more recently, been using scripting of the form

	if test "X" != "X${var}"; then
		echo defined
	else
		echo undefined
	fi
Quoted 11 lines

Example:

$ set testexists 1
$ env exists testexists && echo "yes"
yes
$ env exists testexists || echo "no"
$ set testexists
$ env exists testexists && echo "yes"
$ env exists testexists || echo "no"
no
$

I've often thought that a "env exists" command might be a nice, consistent, addition.

Thanks for adding it.

Jim

Quoted 68 lines
---
  README              |  1 +
  common/cmd_nvedit.c | 23 +++++++++++++++++++++++
  2 files changed, 24 insertions(+)
README +1 -0
index 09662a4..0718459 100644
@@ -843,6 +843,7 @@ The following options need to be configured:
  		CONFIG_CMD_ELF		* bootelf, bootvx
  		CONFIG_CMD_ENV_CALLBACK	* display details about env callbacks
  		CONFIG_CMD_ENV_FLAGS	* display details about env flags
+		CONFIG_CMD_ENV_EXISTS	* check existence of env variable
  		CONFIG_CMD_EXPORTENV	* export the environment
  		CONFIG_CMD_EXT2		* ext2 command support
  		CONFIG_CMD_EXT4		* ext4 command support
common/cmd_nvedit.c +23 -0
index ba9ba16..0d4d02c 100644
@@ -1059,6 +1059,23 @@ sep_err:
  }
  #endif
 
+#if defined(CONFIG_CMD_ENV_EXISTS)
+static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
+		       char * const argv[])
+{
+	ENTRY e, *ep;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	e.key = argv[1];
+	e.data = NULL;
+	hsearch_r(e, FIND, &ep, &env_htab, 0);
+
+	return (ep == NULL) ? 1 : 0;
+}
+#endif
+
  /*
   * New command line interface: "env" command with subcommands
   */
@@ -1094,6 +1111,9 @@ static cmd_tbl_t cmd_env_sub[] = {
  	U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  #endif
  	U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
+#if defined(CONFIG_CMD_ENV_EXISTS)
+	U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
+#endif
  };
 
  #if defined(CONFIG_NEEDS_MANUAL_RELOC)
@@ -1136,6 +1156,9 @@ static char env_help_text[] =
  #if defined(CONFIG_CMD_EDITENV)
  	"env edit name - edit environment variable\n"
  #endif
+#if defined(CONFIG_CMD_ENV_EXISTS)
+	"env exists name - tests for existence of variable\n"
+#endif
  #if defined(CONFIG_CMD_EXPORTENV)
  	"env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
  #endif
Signature · 3 lines
Jim Chargin
AJA Video Systems                       jimc at aja.com
(530) 271-3334                          http://www.aja.com
#2 lore raw

PATCH 1/1 cmd_nvedit.c: Add env exists command

Quoted 7 lines · James Chargin

I have, more recently, been using scripting of the form

	if test "X" != "X${var}"; then
		echo defined
	else
		echo undefined
	fi

Thanks for the feedback.

I was attempting to do something like that originally but was hitting problems related to the expansion which I'm still tracking down:

WRONG:

   $ set var
   $ test "x${var}" = "x" && echo undefined
   undefined
   $ set var 1
   $ test "x${var}" = "x" && echo undefined
   $ set var "1; 2; 3"
   $ test "x${var}" = "x" && echo undefined
Quoted 1 line
Quoted 1 line

undefined

It DOES seem to work a lot better with != as you mentioned above though...

   RIGHT:
   $ set var
   $ test "x${var}" != "x" && echo defined
   $ set var 1
   $ test "x${var}" != "x" && echo defined
   defined
   $ set var "1; 2; 3"
   $ test "x${var}" != "x" && echo defined
   defined

- Andy

#3 lore raw
In reply to Andrew Ruder
Quoted 16 lines · Andrew Ruder

env exists is a way to test (in hush) if an environment variable exists. A workaround existed using printenv but this new command doesn't require all the stdout/stderr redirection to prevent printing information to the screen.

Example:

$ set testexists 1
$ env exists testexists && echo "yes"
yes
$ env exists testexists || echo "no"
$ set testexists
$ env exists testexists && echo "yes"
$ env exists testexists || echo "no"
no
$
Quoted 1 line

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131108/1007c3d8/attachment.pgp>