Browse code

Renamed lib.sh to helpers, refactor and cleaning

Blackbeard authored on 2018-09-14 22:48:35
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,154 @@
1
+#!/bin/sh
2
+####################
3
+# name: lib.sh
4
+####################
5
+
6
+#**
7
+# https://stackoverflow.com/questions/1831527/simple-way-to-colour-alternate-output-lines-in-bash
8
+# LIST -> COLORIZED LIST
9
+# outputs a list with differnt colors for even and odd lines
10
+function helpers_colorOutput
11
+{
12
+    while read line
13
+    do
14
+	echo -e "\033[1;31m $line"
15
+	read line
16
+	echo -e "\033[1;33m $line"
17
+    done
18
+    echo -en "\033[0m"
19
+}
20
+
21
+#**
22
+# URL -> stdout 
23
+# downloads URL as text to standard output
24
+function helpers_downloadStdout
25
+{
26
+	if whereis "curl" > /dev/null
27
+## ohhh, you could use the above function!
28
+	then
29
+		curl -s "$1"
30
+	elif whereis "wget" > /dev/null
31
+	then
32
+		wget -qO- "$1"
33
+	elif whereis "ftp" > /dev/null
34
+	then
35
+		ftp -o - -V "$1"
36
+	fi
37
+}
38
+
39
+#**
40
+# usage: if_nonreal_create root_path filenames[]
41
+# ROOTPATH LISTofFILENAMES -> FILES
42
+# Checks if FILENAME(s) exist in ROOT_PATH--
43
+# if they don't, create them. If they do, ignore.
44
+function helpers_fileCreate
45
+{
46
+	root="$1"
47
+	files="$(echo "$@" | sed 's/'"$1"'//')"
48
+
49
+	for name in "$files"
50
+	do
51
+		if is_dir "$name"
52
+		then
53
+			mkdir "${root}/${name}"
54
+		else
55
+			touch "${root}/${name}"
56
+		fi
57
+	done
58
+}
59
+
60
+#**
61
+# usage: is_dir filenames[]
62
+# LIST of FILENAMES -> 0 or 1
63
+# Returns 0 if FILENAME(s) is name of directory
64
+# Returns 1 if not directory
65
+function helpers_isDirectory
66
+{
67
+	for name in "$@"
68
+	do 
69
+		if echo "$name" | grep "/$" > /dev/null
70
+		then
71
+			return 0
72
+		else
73
+			return 1
74
+		fi
75
+	done
76
+}
77
+
78
+
79
+#**
80
+# VALUE NUMBER -> NUMBER
81
+# returns a number
82
+function helpers_validNumber {
83
+    case "$1" in
84
+    ''|*[!0-9]*) echo "$2" ;;
85
+    *) echo "$1" ;;
86
+    esac
87
+}
88
+
89
+#**
90
+### WRITE THIS
91
+# VALUE STRING -> STRING
92
+# returns a string
93
+#function return_valid_string {
94
+#    case "$1" in
95
+#    ''|*[!0-9]*) echo "$2" ;;
96
+#    *) echo "$1" ;;
97
+#    esac
98
+#}
99
+
100
+
101
+
102
+#**
103
+# MULTI-LINE_STRING NUMBER -> SINGLE-LINE_STRING
104
+# return line number of string
105
+function helpers_getLine {
106
+	line_num="$1"
107
+	string="$2"
108
+#	echo "$string" | head -1
109
+	echo "$string" | head "-$line_num" | tail -1
110
+}
111
+
112
+
113
+
114
+
115
+#**
116
+function helpers_tabSeperate {
117
+    	printf "%s\t%s\n" "$1" "$2"
118
+
119
+}
120
+
121
+#**
122
+# NUMBER NUMBER -> NUMBER
123
+# increment a number by $2
124
+function helpers_incrementBy {
125
+	number="$1"
126
+	if test -z "$2"
127
+	then
128
+		increment="1"
129
+	else
130
+		increment="$2"
131
+	fi
132
+
133
+	echo "$number + $increment" | bc
134
+}
135
+
136
+#**
137
+# STRINGS -> TAB_SEPERATED_LIST
138
+# combine two strings (equal length) into a single tab-seperated list
139
+function helpers_tabStrings {
140
+        local string_a="$1"
141
+        local string_b="$2"
142
+	local length="$(echo "$string_a" | wc -l)"
143
+
144
+        local i="0"
145
+	while [ "$i" -lt "$length" ]
146
+        do
147
+	    i="$(increment $i)"
148
+	    tab_seperate \
149
+	      	"$(get_line "$i" "$string_a")" \
150
+	      	"$(get_line "$i" "$string_b")"
151
+        done
152
+
153
+}
154
+