[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash help please



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, Jun 09, 2016 at 10:41:27PM -0400, Gene Heskett wrote:
> Greetings; 
> 
> A bash script that has worked most of a decade now refuses.
> 
> For instance, assume that var InMail is = "gene", and can be echoed from 
> the command line using $InMail like this.
> gene@coyote:~$ echo $InMail
> gene
> But I'll be switched if I can get a result from a line of code resembling 
> this from the command line while attempting to troubleshoot a 110 line 
> bash script: which asks "if test [${InMail} = "gene"]
> 			  then
> 				-----
> 			 elif (another name)
> 				yadda yadda
> 
> gene@coyote:~$ echo `test [${InMail} = "gene"]`

Most has been answered in this thread here and there, but I think some
things bear repeating:

test and []

  Basically, 'test' and '[' are the same -- well, more or less. There's
  even an executable /usr/bin/[ (no kidding) which in former times was
  just a hardlink to /usr/bin/test (I lost track of why it ain't these
  days). Those days test and/or [ are mostly shell builtins.
  
  So in
  
    if test [ ... ] ; then
  
  either the test or the [] is redundant. Better spell as
  
    if test ...
  
  or
  
    if [ ... ]
  
  depending on your taste.

exit value vs stdout vs command line args

  echo just echoes its command line args. So

    echo foo

  will output foo to stdout

    echo $InMail

  will output 'gene", because the shell will get its first pass through
  the line, will replace $InMail with gene and echo will see 'gene' as
  its first command line arg and do its thing. In

    echo `test [${InMail} = "gene"]`

  the shell again will have a first go. The `` will tell it to interpret
  the thing inside (i.e. test ...) and literally replace its *standard
  output* in place. Test doesn't output anything, thus the whole
  construct `...` gets replaced by nothing, echo sees an empty arg
  and outputs nothing (followed by a newline, which echo does by
  default).

  This is the empty line you are seeing.

  But, you will ask, where's the result of the test gone? Test is
  called for its *exit value*, a number (which by convention is
  0 when everything went OK and different from 0 for some error
  condition). You can use this exit value in the "if" construct
  (also in "while" and so on), and you can extract it from the
  special variabl $?. Also, you can use it in chained logical
  constructs as in "foo && bar || baz" (e.g.:

    rm -f bumble || echo "can't remove this thing"

  would be a more concise way to spell

    if ! rm -f bumble ; then echo "darn" ; fi

Happy hacking!

regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAldaZ0cACgkQBcgs9XrR2kacVwCdEMB/VjiRMEHsOHR/f7YW3JCL
yUoAn2p2eeqDDcTZ1g3h6yGFXUY2TWpM
=axhF
-----END PGP SIGNATURE-----


Reply to: