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. | |
508c8c87 | 6 | # Version: 0.0.2 |
55a0bbf5 SBS |
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\+&@#/%=~_|]' | |
508c8c87 SBS |
13 | arg1="$1"; |
14 | if [[ $arg1 =~ $regex ]]; then | |
55a0bbf5 SBS |
15 | return 0; |
16 | else | |
508c8c87 | 17 | return 1; |
55a0bbf5 SBS |
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 |