aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRatakor <ratakor@disroot.org>2023-05-10 17:14:03 +0200
committerRatakor <ratakor@disroot.org>2023-05-10 17:14:03 +0200
commit873d54c2fc9acbb6d89b85aca69f9abce2c5bf1e (patch)
tree5ecb6d06c760acce01f95cd71018fdccbe3db9b6
parentce44e92b4581bdce355aef1cffc4629fc7baceb0 (diff)
Add forc, array and hextodec to sh.snippets
-rw-r--r--snippets/sh.snippets44
1 files changed, 44 insertions, 0 deletions
diff --git a/snippets/sh.snippets b/snippets/sh.snippets
index fbc59ab..976a58c 100644
--- a/snippets/sh.snippets
+++ b/snippets/sh.snippets
@@ -247,6 +247,15 @@ snippet forn Loop over a variable range of numbers
start=$((start+1))
done
+snippet forc Iterate over characters in a string
+ str=${1:string}
+ while [ -n "$arraytr" ]; do
+ rest=${str#?}
+ first_char=${str%"$rest"}
+ printf "%s\n' "$first_char"
+ str=$rest
+ done
+
snippet read Loop over the contents of a file
while IFS= read -r line || [ -n "\$line" ]; do
printf '%s\n' "\$line"
@@ -330,3 +339,38 @@ snippet isint Check if a number is an integer
# Usage: isint "number"
printf %d "\$1" >/dev/null 2>&1
}
+
+snippet hextodec Hexadecimal to Decimal
+ printf '%s\n' "$((0xFF))"
+ # returns 255
+
+snippet array How to manipulate IFS to have some sort of arrays in POSIX sh
+ array="${1:A|B|C|D}"
+ IFS='|'
+ set -f
+
+ getNth() {
+ shift "$(( \$1 + 1 ))"
+ printf '%s\n' "\$1"
+ }
+
+ getLast() {
+ getNth "$(( $(length "$@") - 1 ))" "$@"
+ }
+
+ len() {
+ echo "$#"
+ }
+
+snippet array2
+ s2='t1 t2 t3 t4'
+ set -f
+ IFS=' '
+
+ for c in \${s2}; do
+ set +f
+ unset IFS
+ echo "\$c"
+ done
+ set +f
+ unset IFS