OILS / ysh / testdata / array-rewrite-2.sh View on Github | oilshell.org

55 lines, 23 significant
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
8argv() { spec/bin/argv.py "$@"; }
9
10#
11# OLD, BASH-COMPATIBLE WAY
12#
13# This style can't handle paths with spaces
14
15CONFIG_HAVE_FOO=yes
16path='/etc/path with spaces'
17flags=''
18
19
20if [ -n "${CONFIG_HAVE_FOO}" ]; then
21 flags="${flags} --foo=$path"
22fi
23
24if [ -n "${CONFIG_HAVE_BAR}" ]; then
25 flags="${flags} --bar"
26fi
27
28argv ${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
40set -o errexit
41shopt -s parse_at simple_word_eval
42
43setvar CONFIG_HAVE_FOO = "yes" # TODO: change to single quotes
44setvar path = "/etc/path with spaces"
45setvar flags = %()
46
47if test -n $CONFIG_HAVE_FOO; then
48 append -- --foo=$path (flags)
49fi
50
51if test -n $CONFIG_HAVE_BAR; then
52 append -- --bar (flags)
53fi
54
55argv @flags