Commit | Line | Data |
---|---|---|
55a0bbf5 SBS |
1 | #!/bin/bash |
2 | ||
3 | checkURL() { | |
4 | # Desc: Checks if string is a valid URL. | |
5 | # Warning: Does not correctly handle multi-byte characters. | |
6 | # Version: 0.0.1 | |
7 | # Input: arg1: string | |
8 | # Output: return code 0: string is a valid URL | |
9 | # return code 1: string is NOT a valid URL | |
10 | # Depends: Bash 5.0.3 | |
11 | # Ref/Attrib: https://stackoverflow.com/a/3184819 | |
12 | regex='(https?|ftp|file|ssh|git)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' | |
13 | string="$1"; | |
14 | if [[ $1 =~ $regex ]]; then | |
15 | return 0; | |
16 | else | |
17 | reutrn 1; | |
18 | fi; | |
19 | } | |
20 | ||
21 | #==BEGIN sample code | |
22 | testString="https://google.com/security.main.html" | |
23 | if checkURL "$testString"; then echo "Is a valid URL:$testString"; else echo "Is NOT a valid URL:$testString"; fi; | |
24 | testString="ssh://username@host.xz/absolute/path/to/repo.git/" | |
25 | if checkURL "$testString"; then echo "Is a valid URL:$testString"; else echo "Is NOT a valid URL:$testString"; fi; | |
26 | #==END sample code |