-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrSlice
More file actions
executable file
·61 lines (58 loc) · 976 Bytes
/
strSlice
File metadata and controls
executable file
·61 lines (58 loc) · 976 Bytes
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
#!/usr/bin/env bash
if [ "$1" = '--help' ]; then
exit 1
elif [ "$1" = '--version' ]; then
exit 1
fi
# TODO: Inclusive/exclusive flags
# declare -i start
# declare -i end
# declare string
# shopt -s extglob globasciiranges
# while (($# > 0)); do
# case "$1" in
# ^-?[[:digit:]]+$)
# if [ -n "$start" ]; then
# if [ -n "$end" ]; then
# string+="$1"
# else
# end="$1"
# fi
# else
# start="$1"
# fi
# ;;
# *)
# string+="$1"
# ;;
# esac
# shift
# done
declare -i start=0
declare -i end
declare string
shopt -s extglob globasciiranges
if [[ "$1" =~ ^-?[[:digit:]]+$ ]]; then
((start = $1))
shift
else
exit 1
fi
if [ "$1" = '--' ]; then
unset end
shift
elif [[ "$1" =~ ^-?[[:digit:]]+$ ]]; then
((end = $1))
declare -r end
shift
else
unset end
fi
declare -r start string="$*"
# TODO: Option to enable escape sequences
if [ -z "$end" ]; then
echo -nE "${string: $start}"
else
echo -nE "${string: $start:$end}"
fi
exit