From 55a0bbf5c6169f7f0f129bd5be4d3c145d6bfebb Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Tue, 23 Mar 2021 10:28:44 +0000 Subject: [PATCH] feat(unitproc):Add bktemp-checkURL Bash function that determines if string is a valid URL --- unitproc/bktemp-checkURL | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 unitproc/bktemp-checkURL diff --git a/unitproc/bktemp-checkURL b/unitproc/bktemp-checkURL new file mode 100644 index 0000000..81f934d --- /dev/null +++ b/unitproc/bktemp-checkURL @@ -0,0 +1,26 @@ +#!/bin/bash + +checkURL() { + # Desc: Checks if string is a valid URL. + # Warning: Does not correctly handle multi-byte characters. + # Version: 0.0.1 + # Input: arg1: string + # Output: return code 0: string is a valid URL + # return code 1: string is NOT a valid URL + # Depends: Bash 5.0.3 + # Ref/Attrib: https://stackoverflow.com/a/3184819 + regex='(https?|ftp|file|ssh|git)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' + string="$1"; + if [[ $1 =~ $regex ]]; then + return 0; + else + reutrn 1; + fi; +} + +#==BEGIN sample code +testString="https://google.com/security.main.html" +if checkURL "$testString"; then echo "Is a valid URL:$testString"; else echo "Is NOT a valid URL:$testString"; fi; +testString="ssh://username@host.xz/absolute/path/to/repo.git/" +if checkURL "$testString"; then echo "Is a valid URL:$testString"; else echo "Is NOT a valid URL:$testString"; fi; +#==END sample code -- 2.30.2