| 1 | #!/usr/bin/env bash | 
| 2 | # | 
| 3 | # Example of rewriting string-based argv processing to use arrays. | 
| 4 | # | 
| 5 | # Adapted from ./linux-4.8.7/scripts/link-vmlinux.sh | 
| 6 |  | 
| 7 | # Show args to a command | 
| 8 | argv() { spec/bin/argv.py "$@"; } | 
| 9 |  | 
| 10 | # | 
| 11 | # OLD, BASH-COMPATIBLE WAY | 
| 12 | # | 
| 13 | # This style can't handle paths with spaces | 
| 14 |  | 
| 15 | CONFIG_HAVE_FOO=yes | 
| 16 | path='/etc/path with spaces' | 
| 17 | flags='' | 
| 18 |  | 
| 19 |  | 
| 20 | if [ -n "${CONFIG_HAVE_FOO}" ]; then | 
| 21 | flags="${flags} --foo=$path" | 
| 22 | fi | 
| 23 |  | 
| 24 | if [ -n "${CONFIG_HAVE_BAR}" ]; then | 
| 25 | flags="${flags} --bar" | 
| 26 | fi | 
| 27 |  | 
| 28 | argv ${flags}  # unquoted splitting | 
| 29 |  | 
| 30 |  | 
| 31 | # | 
| 32 | # NEW OIL WAY | 
| 33 | # | 
| 34 | # - no quoting is necessary because of static-word-eval | 
| 35 | # - splice arrays with @ | 
| 36 | # - builtin 'append' for appending | 
| 37 | #   - I might want to change the ignored delimiter character _ to something like | 
| 38 | #     : or :: or \\ .  Opinions? | 
| 39 |  | 
| 40 | set -o errexit | 
| 41 | shopt -s parse_at simple_word_eval | 
| 42 |  | 
| 43 | setvar CONFIG_HAVE_FOO = "yes"  # TODO: change to single quotes | 
| 44 | setvar path = "/etc/path with spaces" | 
| 45 | setvar flags = %() | 
| 46 |  | 
| 47 | if test -n $CONFIG_HAVE_FOO; then | 
| 48 | append -- --foo=$path (flags) | 
| 49 | fi | 
| 50 |  | 
| 51 | if test -n $CONFIG_HAVE_BAR; then | 
| 52 | append -- --bar (flags) | 
| 53 | fi | 
| 54 |  | 
| 55 | argv @flags |