help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] starting a central bash script repository


From: Chris F.A. Johnson
Subject: Re: [Help-bash] starting a central bash script repository
Date: Sun, 7 Apr 2013 16:17:43 -0400 (EDT)
User-agent: Alpine 2.00 (LMD 1167 2008-08-23)

On Sun, 7 Apr 2013, Dan Douglas wrote:

On Saturday, April 06, 2013 06:55:09 PM Chris F.A. Johnson wrote:
On Sat, 6 Apr 2013, Peng Yu wrote:

Bash code is not reusable in any sane way.  You can't pass array names
to functions, for example

   One certainly can pass array names to functions. I have a library
   of functions that take array names as arguments:
   <http://cfajohnson.com/shell/?2013-01-08_bash_array_manipulation>.

(this is the #1 problem with writing "libraries" of bash code).

   I don't have any problem writing libraries of bash code; I have
   dozens of them.


The point is it's exceedingly difficult to do it safely. You'll never have a
really robust "library language" without some way around the dynamic scope
encapsulation problem. For example, I think this ksh93 function will never be
possible in Bash, even though this is perfectly valid Bash 4.3 code:

   Just because it's valid code doesn't mean it's the right way to do
   it.

   But is it even valid code?

   man bash:
     typeset
       ...
       -n ...
          The -n attribute cannot be applied to array variables.

  In any case, whatever you are trying to do, I'm sure it can be done
  perfectly safely in bash.

  And it's not even valid in ksh93 (remove the unnecessary
  'typeset -a' and it would be).

function f {
   case $1 in
       0)
           typeset -a x=(I\'m an x)
           f 1 x
           ;;
       1)
           typeset -n a=$2
           typeset -a x=(I\'m a different x)
           f 2 a x
           ;;
       2)
           typeset -n x=$2 y=$3
           typeset -p x y
           printf "<%s> " "${x[*]}" "${y[*]}"
           echo
   esac
}

f 0

# typeset -n x=x
# typeset -n y=x
# <I'm an x> <I'm a different x>



--
   Chris F.A. Johnson, <http://cfajohnson.com/>
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]