-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_string_utils.sh
More file actions
executable file
·72 lines (50 loc) · 2.28 KB
/
Copy pathlib_string_utils.sh
File metadata and controls
executable file
·72 lines (50 loc) · 2.28 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
#!/usr/bin/env bash
#### Description: String utils function library
#### The script defines functions to convert spinal case to camel case, lower case, and upper case, and a function to replace key-value pairs in a file.
#### The script is intended to be sourced by other scripts that need string manipulation utilities.
#### Written by: Guillermo de Ignacio - gdeignacio on 01-2023
# Revision 2024-08-01
# Revision 2024-08-01: Updated to define functions to convert spinal case to camel case, lower case, and upper case, and a function to replace key-value pairs in a file.
# Revision 2026-05-26: Minor updates and improvements
#### THIS FILE USED TO BE SOURCED. THINK TWICE BEFORE UPDATE.
#### EXECUTING BY YOURSELF WILL ONLY TAKE EFFECT IN YOUR CURRENT SHELL.
###################################
### STRING UTILS ###
###################################
##################################################################
##################################################################
lib_string_utils.spinal_to_camelcase() {
IFS=- read -ra str <<<"$1"
printf '%s' "${str[@]^}"
}
##################################################################
##################################################################
lib_string_utils.spinal_to_lower() {
IFS=- read -ra str <<<"$1"
printf '%s' "${str[@],,}"
}
##################################################################
##################################################################
lib_string_utils.spinal_to_upper() {
IFS=- read -ra str <<<"$1"
printf '%s' "${str[@]^^}"
}
##################################################################
##################################################################
lib_string_utils.replace_key_value(){
local key=$1
local old_value=$2
local new_value=$3
local file=$4
old_key_value=$(echo $key\=$old_value)
new_key_value=$(echo $key\=$new_value)
echo "Value substitution from $old_key_value to $new_key_value"
# Better use double quoting for sedstring'
sed -i "s/${old_key_value}/${new_key_value}/" ${file}
echo $(cat $file)
}
##################################################################
##################################################################
lib_string_utils.loaded(){
echo lib_string_utils.sh loaded
}