| 1 | #! /bin/bash |
| 2 | # unprotect_literal.sh |
| 3 | |
| 4 | # set -vx |
| 5 | |
| 6 | :<<-'_UnProtect_Literal_String_Doc' |
| 7 | |
| 8 | Copyright (c) Michael S. Zick, 2003; All Rights Reserved |
| 9 | License: Unrestricted reuse in any form, for any purpose. |
| 10 | Warranty: None |
| 11 | Revision: $ID$ |
| 12 | |
| 13 | Documentation redirected to the Bash no-operation. Bash will |
| 14 | '/dev/null' this block when the script is first read. |
| 15 | (Uncomment the above set command to see this action.) |
| 16 | |
| 17 | Remove the first (Sha-Bang) line when sourcing this as a library |
| 18 | procedure. Also comment out the example use code in the two |
| 19 | places where shown. |
| 20 | |
| 21 | |
| 22 | Usage: |
| 23 | Complement of the "$(_pls 'Literal String')" function. |
| 24 | (See the protect_literal.sh example.) |
| 25 | |
| 26 | StringVar=$(_upls ProtectedSringVariable) |
| 27 | |
| 28 | Does: |
| 29 | When used on the right-hand-side of an assignment statement; |
| 30 | makes the substitions embedded in the protected string. |
| 31 | |
| 32 | Notes: |
| 33 | The strange names (_*) are used to avoid trampling on |
| 34 | the user's chosen names when this is sourced as a |
| 35 | library. |
| 36 | |
| 37 | |
| 38 | _UnProtect_Literal_String_Doc |
| 39 | |
| 40 | _upls() { |
| 41 | local IFS=$'x1B' # \ESC character (not required) |
| 42 | eval echo $@ # Substitution on the glob. |
| 43 | } |
| 44 | |
| 45 | # :<<-'_UnProtect_Literal_String_Test' |
| 46 | # # # Remove the above "# " to disable this code. # # # |
| 47 | |
| 48 | |
| 49 | _pls() { |
| 50 | local IFS=$'x1B' # \ESC character (not required) |
| 51 | echo $'\x27'$@$'\x27' # Hard quoted parameter glob |
| 52 | } |
| 53 | |
| 54 | # Declare an array for test values. |
| 55 | declare -a arrayZ |
| 56 | |
| 57 | # Assign elements with various types of quotes and escapes. |
| 58 | arrayZ=( zero "$(_pls 'Hello ${Me}')" 'Hello ${You}' "\'Pass: ${pw}\'" ) |
| 59 | |
| 60 | # Now make an assignment with that result. |
| 61 | declare -a array2=( ${arrayZ[@]} ) |
| 62 | |
| 63 | # Which yielded: |
| 64 | # - - Test Three - - |
| 65 | # Element 0: zero is: 4 long # Our marker element. |
| 66 | # Element 1: Hello ${Me} is: 11 long # Intended result. |
| 67 | # Element 2: Hello is: 5 long # ${You} expanded to nothing. |
| 68 | # Element 3: 'Pass: is: 6 long # Split on the whitespace. |
| 69 | # Element 4: ' is: 1 long # The end quote is here now. |
| 70 | |
| 71 | # set -vx |
| 72 | |
| 73 | # Initialize 'Me' to something for the embedded ${Me} substitution. |
| 74 | # This needs to be done ONLY just prior to evaluating the |
| 75 | #+ protected string. |
| 76 | # (This is why it was protected to begin with.) |
| 77 | |
| 78 | Me="to the array guy." |
| 79 | |
| 80 | # Set a string variable destination to the result. |
| 81 | newVar=$(_upls ${array2[1]}) |
| 82 | |
| 83 | # Show what the contents are. |
| 84 | echo $newVar |
| 85 | |
| 86 | # Do we really need a function to do this? |
| 87 | newerVar=$(eval echo ${array2[1]}) |
| 88 | echo $newerVar |
| 89 | |
| 90 | # I guess not, but the _upls function gives us a place to hang |
| 91 | #+ the documentation on. |
| 92 | # This helps when we forget what a # construction like: |
| 93 | #+ $(eval echo ... ) means. |
| 94 | |
| 95 | # What if Me isn't set when the protected string is evaluated? |
| 96 | unset Me |
| 97 | newestVar=$(_upls ${array2[1]}) |
| 98 | echo $newestVar |
| 99 | |
| 100 | # Just gone, no hints, no runs, no errors. |
| 101 | |
| 102 | # Why in the world? |
| 103 | # Setting the contents of a string variable containing character |
| 104 | #+ sequences that have a meaning in Bash is a general problem in |
| 105 | #+ script programming. |
| 106 | # |
| 107 | # This problem is now solved in eight lines of code |
| 108 | #+ (and four pages of description). |
| 109 | |
| 110 | # Where is all this going? |
| 111 | # Dynamic content Web pages as an array of Bash strings. |
| 112 | # Content set per request by a Bash 'eval' command |
| 113 | #+ on the stored page template. |
| 114 | # Not intended to replace PHP, just an interesting thing to do. |
| 115 | ### |
| 116 | # Don't have a webserver application? |
| 117 | # No problem, check the example directory of the Bash source; |
| 118 | #+ there is a Bash script for that also. |
| 119 | |
| 120 | # _UnProtect_Literal_String_Test |
| 121 | # # # Remove the above "# " to disable this code. # # # |
| 122 | |
| 123 | exit 0 |