diff options
Diffstat (limited to 'tools/bash.html')
-rw-r--r-- | tools/bash.html | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/tools/bash.html b/tools/bash.html index 580e941..91d44cb 100644 --- a/tools/bash.html +++ b/tools/bash.html @@ -192,7 +192,7 @@ bar=${foo:-some_val} # if $foo set, then bar=$foo else bar=some_val # alternate value -bar=${foo:+bla $foo} # if $foo set, then bar="bla $foo" else bar="" +bar=${foo:+bla $foo} # if $foo set, then bar="bla $foo" else bar="" # check param set bar=${foo:?msg} # if $foo set, then bar=$foo else exit and print msg @@ -274,6 +274,19 @@ contains some nice visualization to explain bash redirections.</p> once.</p> <pre><code class="language-bash">vim -d <(grep foo bar) <(grep foo moose) </code></pre> +<h2 id="command-grouping"><a class="header" href="#command-grouping">Command grouping</a></h2> +<p>Execute commands in a group with or without subshell. Can be used to easily +redirect stdout/stderr of all commands in the group into one file.</p> +<pre><code class="language-bash"># Group commands without subshell. +v=abc ; { v=foo; echo $v; } ; echo $v +# foo +# foo + +# Group commands with subshell. +v=abc ; ( v=foo; echo $v; ) ; echo $v +# foo +# abc +</code></pre> <h2 id="argument-parsing-with-getopts"><a class="header" href="#argument-parsing-with-getopts">Argument parsing with <code>getopts</code></a></h2> <p>The <code>getopts</code> builtin uses following global variables:</p> <ul> @@ -296,11 +309,11 @@ once.</p> <h3 id="example"><a class="header" href="#example">Example</a></h3> <pre><code class="language-bash">#!/bin/bash function parse_args() { - while getopts "f:c" PARAM; do + while getopts "f:c" PARAM; do case $PARAM in - f) echo "GOT -f $OPTARG";; - c) echo "GOT -c";; - *) echo "ERR: print usage"; exit 1;; + f) echo "GOT -f $OPTARG";; + c) echo "GOT -c";; + *) echo "ERR: print usage"; exit 1;; esac done # users responsibility to reset OPTIND @@ -320,14 +333,14 @@ The match results can be accessed via the <code>$BASH_REMATCH</code> variable:</ <pre><code class="language-bash">INPUT='title foo : 1234' REGEX='^title (.+) : ([0-9]+)$' if [[ $INPUT =~ $REGEX ]]; then - echo "${BASH_REMATCH[0]}" # title foo : 1234 - echo "${BASH_REMATCH[1]}" # foo - echo "${BASH_REMATCH[2]}" # 1234 + echo "${BASH_REMATCH[0]}" # title foo : 1234 + echo "${BASH_REMATCH[1]}" # foo + echo "${BASH_REMATCH[2]}" # 1234 fi </code></pre> <blockquote> <p><strong>Caution</strong>: When specifying a <code>regex</code> in the <code>[[ ]]</code> block directly, quotes will be treated as part of the pattern. -<code>[[ $INPUT =~ "foo" ]]</code> will match against <code>"foo"</code> not <code>foo</code>!</p> +<code>[[ $INPUT =~ "foo" ]]</code> will match against <code>"foo"</code> not <code>foo</code>!</p> </blockquote> <h2 id="completion"><a class="header" href="#completion">Completion</a></h2> <p>The <code>complete</code> builtin is used to interact with the completion system.</p> @@ -358,11 +371,11 @@ against words generated by <code>option</code>.</p> # -u generate list with users # -e generate list with exported variables -# compare "f" against words "foo" "foobar" "bar" and generate matches -compgen -W "foo foobar bar" "f" +# compare "f" against words "foo" "foobar" "bar" and generate matches +compgen -W "foo foobar bar" "f" -# compare "hom" against file/dir names and generate matches -compgen -d -f "hom" +# compare "hom" against file/dir names and generate matches +compgen -d -f "hom" </code></pre> <h3 id="example-1"><a class="header" href="#example-1">Example</a></h3> <p>Skeleton to copy/paste for writing simple completions.</p> @@ -374,12 +387,12 @@ compgen -d -f "hom" local curr=$2 local prev=$3 - local opts="-c -s -f -h" + local opts="-c -s -f -h" case $prev in - -c) COMPREPLY=( $(compgen -W "green red blue" -- $curr) );; - -s) COMPREPLY=( $(compgen -W "low high" -- $curr) );; + -c) COMPREPLY=( $(compgen -W "green red blue" -- $curr) );; + -s) COMPREPLY=( $(compgen -W "low high" -- $curr) );; -f) COMPREPLY=( $(compgen -f -- $curr) );; - *) COMPREPLY=( $(compgen -W "$opts" -- $curr) );; + *) COMPREPLY=( $(compgen -W "$opts" -- $curr) );; esac } |