| 1 | #! /bin/bash |
| 2 | # protect_literal.sh |
| 3 | |
| 4 | # set -vx |
| 5 | |
| 6 | :<<-'_Protect_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. |
| 14 | Bash will '/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 | _protect_literal_str 'Whatever string meets your ${fancy}' |
| 24 | Just echos the argument to standard out, hard quotes |
| 25 | restored. |
| 26 | |
| 27 | $(_protect_literal_str 'Whatever string meets your ${fancy}') |
| 28 | as the right-hand-side of an assignment statement. |
| 29 | |
| 30 | Does: |
| 31 | As the right-hand-side of an assignment, preserves the |
| 32 | hard quotes protecting the contents of the literal during |
| 33 | assignment. |
| 34 | |
| 35 | Notes: |
| 36 | The strange names (_*) are used to avoid trampling on |
| 37 | the user's chosen names when this is sourced as a |
| 38 | library. |
| 39 | |
| 40 | _Protect_Literal_String_Doc |
| 41 | |
| 42 | # The 'for illustration' function form |
| 43 | |
| 44 | _protect_literal_str() { |
| 45 | |
| 46 | # Pick an un-used, non-printing character as local IFS. |
| 47 | # Not required, but shows that we are ignoring it. |
| 48 | local IFS=$'\x1B' # \ESC character |
| 49 | |
| 50 | # Enclose the All-Elements-Of in hard quotes during assignment. |
| 51 | local tmp=$'\x27'$@$'\x27' |
| 52 | # local tmp=$'\''$@$'\'' # Even uglier. |
| 53 | |
| 54 | local len=${#tmp} # Info only. |
| 55 | echo $tmp is $len long. # Output AND information. |
| 56 | } |
| 57 | |
| 58 | # This is the short-named version. |
| 59 | _pls() { |
| 60 | local IFS=$'x1B' # \ESC character (not required) |
| 61 | echo $'\x27'$@$'\x27' # Hard quoted parameter glob |
| 62 | } |
| 63 | |
| 64 | # :<<-'_Protect_Literal_String_Test' |
| 65 | # # # Remove the above "# " to disable this code. # # # |
| 66 | |
| 67 | # See how that looks when printed. |
| 68 | echo |
| 69 | echo "- - Test One - -" |
| 70 | _protect_literal_str 'Hello $user' |
| 71 | _protect_literal_str 'Hello "${username}"' |
| 72 | echo |
| 73 | |
| 74 | # Which yields: |
| 75 | # - - Test One - - |
| 76 | # 'Hello $user' is 13 long. |
| 77 | # 'Hello "${username}"' is 21 long. |
| 78 | |
| 79 | # Looks as expected, but why all of the trouble? |
| 80 | # The difference is hidden inside the Bash internal order |
| 81 | #+ of operations. |
| 82 | # Which shows when you use it on the RHS of an assignment. |
| 83 | |
| 84 | # Declare an array for test values. |
| 85 | declare -a arrayZ |
| 86 | |
| 87 | # Assign elements with various types of quotes and escapes. |
| 88 | arrayZ=( zero "$(_pls 'Hello ${Me}')" 'Hello ${You}' "\'Pass: ${pw}\'" ) |
| 89 | |
| 90 | # Now list that array and see what is there. |
| 91 | echo "- - Test Two - -" |
| 92 | for (( i=0 ; i<${#arrayZ[*]} ; i++ )) |
| 93 | do |
| 94 | echo Element $i: ${arrayZ[$i]} is: ${#arrayZ[$i]} long. |
| 95 | done |
| 96 | echo |
| 97 | |
| 98 | # Which yields: |
| 99 | # - - Test Two - - |
| 100 | # Element 0: zero is: 4 long. # Our marker element |
| 101 | # Element 1: 'Hello ${Me}' is: 13 long. # Our "$(_pls '...' )" |
| 102 | # Element 2: Hello ${You} is: 12 long. # Quotes are missing |
| 103 | # Element 3: \'Pass: \' is: 10 long. # ${pw} expanded to nothing |
| 104 | |
| 105 | # Now make an assignment with that result. |
| 106 | declare -a array2=( ${arrayZ[@]} ) |
| 107 | |
| 108 | # And print what happened. |
| 109 | echo "- - Test Three - -" |
| 110 | for (( i=0 ; i<${#array2[*]} ; i++ )) |
| 111 | do |
| 112 | echo Element $i: ${array2[$i]} is: ${#array2[$i]} long. |
| 113 | done |
| 114 | echo |
| 115 | |
| 116 | # Which yields: |
| 117 | # - - Test Three - - |
| 118 | # Element 0: zero is: 4 long. # Our marker element. |
| 119 | # Element 1: Hello ${Me} is: 11 long. # Intended result. |
| 120 | # Element 2: Hello is: 5 long. # ${You} expanded to nothing. |
| 121 | # Element 3: 'Pass: is: 6 long. # Split on the whitespace. |
| 122 | # Element 4: ' is: 1 long. # The end quote is here now. |
| 123 | |
| 124 | # Our Element 1 has had its leading and trailing hard quotes stripped. |
| 125 | # Although not shown, leading and trailing whitespace is also stripped. |
| 126 | # Now that the string contents are set, Bash will always, internally, |
| 127 | #+ hard quote the contents as required during its operations. |
| 128 | |
| 129 | # Why? |
| 130 | # Considering our "$(_pls 'Hello ${Me}')" construction: |
| 131 | # " ... " -> Expansion required, strip the quotes. |
| 132 | # $( ... ) -> Replace with the result of..., strip this. |
| 133 | # _pls ' ... ' -> called with literal arguments, strip the quotes. |
| 134 | # The result returned includes hard quotes; BUT the above processing |
| 135 | #+ has already been done, so they become part of the value assigned. |
| 136 | # |
| 137 | # Similarly, during further usage of the string variable, the ${Me} |
| 138 | #+ is part of the contents (result) and survives any operations |
| 139 | # (Until explicitly told to evaluate the string). |
| 140 | |
| 141 | # Hint: See what happens when the hard quotes ($'\x27') are replaced |
| 142 | #+ with soft quotes ($'\x22') in the above procedures. |
| 143 | # Interesting also is to remove the addition of any quoting. |
| 144 | |
| 145 | # _Protect_Literal_String_Test |
| 146 | # # # Remove the above "# " to disable this code. # # # |
| 147 | |
| 148 | exit 0 |