From bac8a5d2822835cf47175d1162030653fadd5c09 Mon Sep 17 00:00:00 2001
From: johannst
vim -d <(grep foo bar) <(grep foo moose)
+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.
+# 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
+
getopts
The getopts
builtin uses following global variables:
#!/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 $BASH_REMATCH
variable:
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
Caution: When specifying a regex
in the [[ ]]
block directly, quotes will be treated as part of the pattern.
-[[ $INPUT =~ "foo" ]]
will match against "foo"
not foo
!
+[[ $INPUT =~ "foo" ]]
will match against "foo"
not foo
!
Completion
The complete
builtin is used to interact with the completion system.
@@ -358,11 +371,11 @@ against words generated by option
.
# -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"
Skeleton to copy/paste for writing simple completions.
@@ -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 } -- cgit v1.2.3