From 28c1c89f194ff91ae20bb579df38540115f44041 Mon Sep 17 00:00:00 2001 From: johannst Date: Wed, 20 Jan 2021 19:15:48 +0000 Subject: deploy: 77d44129ad468581e860720ccdb7643d5bb25601 --- tools/bash.html | 39 +++++++++++++++++++++++++++++---------- tools/zsh.html | 16 ++++++++++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) (limited to 'tools') diff --git a/tools/bash.html b/tools/bash.html index cf5a5ed..fc868bb 100644 --- a/tools/bash.html +++ b/tools/bash.html @@ -241,7 +241,7 @@ command 2>&1 >file
getopts <optstring> <param> [<args>]
 
@@ -258,20 +258,39 @@ command 2>&1 >file

Example

#!/bin/bash
 function parse_args() {
-	while getopts "f:c" PARAM; do
-		case $PARAM in
-			f) echo "GOT -f $OPTARG";;
-			c) echo "GOT -c";;
-			*) echo "ERR: print usage"; exit 1;;
-		esac
-	done
-	# users responsibility to reset OPTIND
-	OPTIND=0
+    while getopts "f:c" PARAM; do
+        case $PARAM in
+            f) echo "GOT -f $OPTARG";;
+            c) echo "GOT -c";;
+            *) echo "ERR: print usage"; exit 1;;
+        esac
+    done
+    # users responsibility to reset OPTIND
+    OPTIND=0
 }
 
 parse_args -f xxx -c
 parse_args -f yyy
 
+

Regular Expressions

+

Bash supports regular expression matching with the binary operator =~. +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
+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!

+

Completion

The complete builtin is used to interact with the completion system.

complete                    # print currently installed completion handler
diff --git a/tools/zsh.html b/tools/zsh.html
index 2a540ab..6ca18af 100644
--- a/tools/zsh.html
+++ b/tools/zsh.html
@@ -272,6 +272,22 @@ echo $bar[2]          # 2
 echo ${(L)foo}        # aabb
 echo ${(U)foo}        # AABB
 
+

Regular Expressions

+

Zsh supports regular expression matching with the binary operator =~. +The match results can be accessed via the $MATCH variable and +$match indexed array:

+ +
INPUT='title foo : 1234'
+REGEX='^title (.+) : ([0-9]+)$'
+if [[ $INPUT =~ $REGEX ]]; then
+    echo "$MATCH"       # title foo : 1234
+    echo "$match[1]"    # foo
+    echo "$match[2]"    # 1234
+fi
+

Completion

Installation

Completion functions are provided via files and need to be placed in a location -- cgit v1.2.3