This blog records implementation methods from Bash scripts I’ve encountered, as an excerpt and summary. The purpose of writing it is absolutely not to encourage everyone to use shell scripts (quite the opposite) — only to show that shell scripts can do many operations.
bash two-dimensional arrays
Bash itself does not support two-dimensional arrays; this method is just a substitute.
1 | test_version_arrays=( |
Printing indices in a for loop
Copy from: https://stackoverflow.com/a/6723516/5563477
1 | foo=('s1' 's2' 's3') |
Deduplicating strings by md5 value
If we want to deduplicate and save many strings, the first method that comes to mind is deduplicating the strings first, then saving. Here we take a different approach: save the files first, then deduplicate the files.
1 | arr=('str1' 'str1' 'str1' 'str1' 'str2' 'str3') |
Sorting part of an array’s values with sed and sort
1 | arr=('version-1' 'version-2' 'version-3' 'version-39' 'version-13') |