forked from Vonng/Configuration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.sh
More file actions
50 lines (44 loc) · 1.46 KB
/
Copy pathstring.sh
File metadata and controls
50 lines (44 loc) · 1.46 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
#!/usr/bin/env bash
#==============================================================#
# Author: Vonng(fengruohang@outlook.com) #
# Desc : Bash String Util #
# Dep : None #
#==============================================================#
# module name
declare -g -r __MODULE_STRING="string"
#==============================================================#
# encoding #
#==============================================================#
# lower/upper
# Usage: `upper abc` or echo abc | upper
function upper() {
if [ -t 0 ]; then # argument
tr '[:lower:]' '[:upper:]' <<< "$*"
else # pipe
tr '[:lower:]' '[:upper:]'
fi;
}
function lower() {
if [ -t 0 ]; then # argument
tr tr '[:upper:]' '[:lower:]' <<< "$*"
else # pipe
tr tr '[:upper:]' '[:lower:]'
fi;
}
#--------------------------------------------------------------#
# str length
# arg 1 : string
# ret : char count in string
# also archive by `${#1}` in correct locale
function len(){
declare -i charLen=$(echo -n ${1} | wc -m)
echo ${charLen}
}
# arg 1 : string
# ret : byte count in string
# also achieve by `LANG=C LC_ALL=C ${#1}` in locale C
function blen(){
declare -i charLen=$(echo -n ${1} | wc -c)
echo ${charLen}
}
#--------------------------------------------------------------#