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 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'tools/bash.html') 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
-- 
cgit v1.2.3