All pastes #171427 Raw Edit

Stuff

public text v1 · immutable
·published 2006-09-15 11:48 UTC
rendered paste body
bonsai@mainserver:~$ fooarr=(* "foo"$'\n'"bar")
bonsai@mainserver:~$ getarrdecl fooarr "${fooarr[@]}"
fooarr=([0]"bin"[1]"dcc"[2]"Desktop"[3]"devel"[4]"ekiga_development"[5]"etc"[6]"gtk-gnutella-downloads"[7]"icons"[8]"incoming"[9]"ip-minds.de"[10]"ircstats"[11]"latex"[12]"ldap.conf"[13]"log"[14]"Mail"[15]"mnt"[16]"net"[17]"News"[18]"plan9"[19]"sabine_elterns.txt"[20]"sources"[21]"Star Trek Enterprise - S01 E16 - Allein.avi"[22]"tacctd_devel"[23]"tests"[24]"torrentDL"[25]"foo
bar")

# getarrdecl(name,val[0],val[1]...)
# write a bash-consumable array declaration for the variable 'name' 
# with contents val0 val1...
# if '--verify' is first argument, then verify
getarrdecl() {
   local f="'" t="'\''"
   local verif=0;
   [ "$1" = "--verify" ] && {
      verif=1; shift;
      local -a args; args=( "$@" );
      local check='n=0 && while [ $# -ne 0 ]; do
         [ "$1" = "${'$1'[$n]}" ] || { echo bad $n; exit 1; } &&
         let n=n+1 && shift;
      done; echo "$n elements were good"; exit 0'
   }
   local x="${1}=(";
   shift;
   # append to x each argument with any ' replaced with '\''
   while [ $# -ne 0 ]; do x="${x} '${1//${f}/${t}}'"; shift; done
   x="${x} )"
   { [ $verif -eq 0 ] || bash -c "$x && $check" "${args[@]}"; } &&
      echo "$x"
}