-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc.sh
executable file
·79 lines (66 loc) · 1.77 KB
/
rfc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
#
# @author Anakin Childerhose
#
# This script is to download rfcs easily from the command line so that they
# don't need to be viewed in the browser. I prefer downloading the .txt files
# so I can browse them in Vim.
# List of dependencies
dependencies=(wget parallel)
missing_dependencies=()
# Check for dependencies
for dep in "${dependencies[@]}"; do
command -v "$dep" &> /dev/null || missing_dependencies+=("$dep")
done
# 80 red #'s
red_banner () {
printf "\e[31m%.0s#\e[0m" {1..80}
}
export -f red_banner
print_missing_dep () {
red_banner
printf "\n\e[31mMissing Dependencies:\n"
for dep in "${missing_dependencies[@]}"; do
printf "\t$dep\n"
done
printf "\e[0m"
red_banner
echo
exit 2
}
# Tell user what dependencies they are missing if any
[ ${#missing_dependencies[@]} -eq 0 ] || print_missing_dep
usage () {
printf "Download RFC text files by number\n\n"
printf "Usage:\n\trfc RFC-number...\n\n"
printf "Examples:\n\trfc 1945\n\trfc 1945 7230 7540\n\n"
exit
}
# Exit if no args were passed to script
[ ! "$#" -eq 0 ] || usage
err_invalid_num () {
red_banner
printf "\n\e[31mInvalid argument: $1\e[0m\n"
red_banner
echo
exit 2
}
export -f err_invalid_num
err_download_failed () {
red_banner
printf "\n\e[31mDownload failed for $1\e[0m\n"
red_banner
echo
exit 2
}
export -f err_download_failed
download () {
# Check if an arg is a number for soft error checking
[[ "$1" =~ ^[0-9]{1,5}$ ]] || err_invalid_num "$1"
# Download RFC or error out
wget "https://www.rfc-editor.org/rfc/rfc$1.txt" || err_download_failed "$1"
}
# Make the function visible to parallel
export -f download
# Run the download function for each argument passed
parallel download ::: "$@"