Re: [SLUG] Bash Help Needed

From: Dylan William Hardison (dylan@hardison.net)
Date: Sat May 17 2008 - 16:11:24 EDT


Spake Michael Fisher on Saturday, May 17, 2008 at 01:13PM -0400:
> I am learning to do some bash programming on my new EmperorLinux lenovo T61
> (prideful plug) and am having a problem in attempting to determine if an
> argument was included when the bash file was started. I can perform a printf
> statement and it prints whatever I add as an argument but I am attempting to
> do an 'if/then' statement.

Notes for the test command ('[ .... ]')

1 -eq is numeric equality, not string equality.
  Use "=" (and sometimes ==, depending on shell) for string equality.
  (If you ever learn perl, remember that it's the opposite.)

2) -z returns true if a string is empty

3) -n is the logical negation of -z

4) use [[ ]] instead of [ ], as the former handles empty strings in a more sane way

5) variables can be assigned a default value, like so: var=${1:-"default value"}

6) It's irrelevant, but someone mentioned to quote "$1", that's a good idea.
   Bash and similar shells expand variables in a really moronic way:
   foo="one two three"
   echo $foo
   ## echo gets three arguments, not one
   echo "$foo"
   ## echo gets one argument.

   (my login shell, zsh, however, always treats $foo as a single argument -- unless
   it's an array.)

-- 
He who knows not and knows that he knows not is ignorant.  Teach him.
He who knows not and knows not that he knows not is a fool.  Shun him.
He who knows and knows not that he knows is asleep.  Wake him.
-
GPG Fingerprint: 412C CCE9 DDA2 4FE9 C34F  754B 0863 0EA6 712E BBE1
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS).  Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 15:51:36 EDT