11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

# # # Remove the above "# " to <strong>di</strong>sable this code. # # #<br />

Appen<strong>di</strong>ce A. Script aggiuntivi<br />

_pls() {<br />

local IFS=$’x1B’ # \ESC character (not required)<br />

echo $’\x27’$@$’\x27’ # Hard quoted parameter glob<br />

}<br />

# Declare an array for test values.<br />

declare -a arrayZ<br />

# Assign elements with various types of quotes and escapes.<br />

arrayZ=( zero "$(_pls ’Hello ${Me}’)" ’Hello ${You}’ "\’Pass: ${pw}\’" )<br />

# Now make an assignment with that result.<br />

declare -a array2=( ${arrayZ[@]} )<br />

# Which yielded:<br />

# - - Test Three - -<br />

# Element 0: zero is: 4 long # Our marker element.<br />

# Element 1: Hello ${Me} is: 11 long # Intended result.<br />

# Element 2: Hello is: 5 long # ${You} expanded to nothing.<br />

# Element 3: ’Pass: is: 6 long # Split on the whitespace.<br />

# Element 4: ’ is: 1 long # The end quote is here now.<br />

# set -vx<br />

# Initialize ’Me’ to something for the embedded ${Me} substitution.<br />

# This needs to be done ONLY just prior to evaluating the<br />

#+ protected string.<br />

# (This is why it was protected to begin with.)<br />

Me="to the array guy."<br />

# Set a string variable destination to the result.<br />

newVar=$(_upls ${array2[1]})<br />

# Show what the contents are.<br />

echo $newVar<br />

# Do we really need a function to do this?<br />

newerVar=$(eval echo ${array2[1]})<br />

echo $newerVar<br />

# I guess not, but the _upls function gives us a place to hang<br />

#+ the documentation on.<br />

# This helps when we forget what a # construction like:<br />

#+ $(eval echo ... ) means.<br />

# What if Me isn’t set when the protected string is evaluated?<br />

unset Me<br />

newestVar=$(_upls ${array2[1]})<br />

echo $newestVar<br />

669

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!