-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-utilities.sh_.txt
More file actions
96 lines (79 loc) · 2.49 KB
/
Copy pathstring-utilities.sh_.txt
File metadata and controls
96 lines (79 loc) · 2.49 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env bash
# Bash Utility
#
# Copyright (C) 2018 Alessandro Alfonsi
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
## Examples
#
STRING="Abcdez"
DEC="97"
URL="mtp:host=%5Busb%3A001%2C009%5D"
## Function
#
count_str() {
# count_str() - returns the lentgh of a string
local str="$1"
echo "${#str}"
}
ord() {
# ord() - converts ASCII character to its decimal value
local char="$1"
printf "%d" "'${char}"
}
chr() {
# chr() - converts decimal value to its ASCII character representation
local num="$1"
printf '%b' "$(printf '\%03o' "${num}")"
}
to_upper() {
# to_upper() - returns an uppercase string
local str="$1"
echo "${str}" | tr '[:lower:]' '[:upper:]'
}
to_lower() {
# to_lower() - returns a lowercase string
local str="$1"
echo "${str}" | tr '[:upper:]' '[:lower:]'
}
rot1() {
# rot1() - encrypt a string with rot1 (replace each letter with the letter 1 position after)
local str="$1"
echo "${str}" | tr 'a-zA-Z' 'b-zaB-ZA'
}
rot13() {
# rot13() - encrypt a string with rot13 (replace each letter with the letter 13 position after)
local str="$1"
echo "${str}" | tr 'a-zA-Z' 'n-za-mN-ZA-M'
}
urldecode() {
# urldecode() - decode a URL
: "${*//+/ }"; echo -e "${_//%/\\x}";
}
## Main
#
FIRST_CHR="${STRING:0:1}"
echo "String: ${STRING}"
echo "String length: $(count_str "${STRING}")"
echo "First character of the string: ${FIRST_CHR}"
echo "Ord of character ${FIRST_CHR}: $(ord "${FIRST_CHR}")"
echo "Chr of number ${DEC}: $(chr "${DEC}")"
echo "Convert a string to uppercase: $(to_upper "${STRING}")"
echo "Convert a string to lowercase: $(to_lower "${STRING}")"
echo "Encrypt a string with ROT1: $(rot1 "${STRING}")"
echo "Encrypt a string with ROT13: $(rot13 "${STRING}")"
echo "Decode a URL: $(urldecode "${URL}")"
# vim: ts=2 sw=2 noet ai nohls