Added automatic extraction of shell code into file pure_bash.sh and fixed cosmetic test error with remove_array_dups function.

This commit is contained in:
memoryleakno1 2019-02-05 04:03:57 +01:00
parent f5854f6396
commit aa0914c459
3 changed files with 28 additions and 11 deletions

View File

@ -614,6 +614,7 @@ the current functions arguments in reverse.
```sh
reverse_array() {
# Usage: reverse_array "array"
shopt -s extdebug
f()(printf '%s\n' "${BASH_ARGV[@]}"); f "$@"
shopt -u extdebug
@ -643,7 +644,7 @@ Create a temporary associative array. When setting associative array
values and a duplicate assignment occurs, bash overwrites the key. This
allows us to effectively remove array duplicates.
**CAVEAT:** Requires `bash` 4+
**CAVEAT:** Requires `bash` 4+, resulting array is printed reordered
**Example Function:**
@ -2077,10 +2078,13 @@ This will run the given command and keep it running, even after the terminal or
bkr() {
(nohup "$@" &>/dev/null &)
}
```
```shell
# Background
bkr ./some_script.sh # some_script.sh is now running in the background
```
<!-- CHAPTER END -->
# AFTERWORD

View File

@ -1,6 +1,16 @@
#!/usr/bin/env bash
#
# Turn the single document bible into a book separated by chapters.
extract() {
# Usage: extract file "opening marker" "closing marker"
while IFS=$'\n' read -r line; do
[[ $extract && $line != "$3" ]] &&
printf '%s\n' "$line"
[[ $line == "$2" ]] && extract=1
[[ $line == "$3" ]] && extract=
done < "$1"
}
main() {
rm -rf manuscript
@ -19,6 +29,9 @@ main() {
printf '%s\n' "${chapter[$i]}" > "manuscript/chapter${i}.txt"
printf '%s\n' "chapter${i}.txt" >> "manuscript/Book.txt"
done
# extract shell code to pure_bash_bible.sh based on markers
extract ./README.md '```sh' '```' > pure_bash.sh
}
main

18
test.sh
View File

@ -64,13 +64,13 @@ test_urldecode() {
}
test_reverse_array() {
IFS=$'\n' read -d "" -ra result < <(reverse_array 1 2 3 4 5)
read -d "" -a result < <(reverse_array 1 2 3 4 5)
assert_equals "${result[*]}" "5 4 3 2 1"
}
test_remove_array_dups() {
IFS=$'\n' read -d "" -ra result < <(remove_array_dups 1 1 2 2 3 3 4 5)
assert_equals "${result[*]}" "1 2 3 4 5"
read -d "" -a result < <(remove_array_dups 1 1 2 2 3 3 4 5)
assert_equals "${result[*]}" "5 4 3 2 1"
}
test_cycle() {
@ -178,25 +178,25 @@ assert_equals() {
}
main() {
trap 'rm readme_code test_file' EXIT
trap 'rm readme_code.sh test_file' EXIT
# Extract code blocks from the README.
while IFS=$'\n' read -r line; do
[[ "$code" && "$line" != \`\`\` ]] && printf '%s\n' "$line"
[[ "$line" =~ ^\`\`\`sh$ ]] && code=1
[[ "$line" =~ ^\`\`\`$ ]] && code=
done < README.md > readme_code
done < README.md > readme_code.sh
# Run shellcheck and source the code.
shellcheck -s bash readme_code test.sh build.sh || exit 1
. readme_code
#shellcheck -s bash readme_code test.sh build.sh || exit 1
. readme_code.sh
head="-> Running tests on the Pure Bash Bible.."
printf '\n%s\n%s\n' "$head" "${head//?/-}"
# Generate the list of tests to run.
IFS=$'\n' read -d "" -ra funcs < <(declare -F)
for func in "${funcs[@]//declare -f }"; do
IFS=$'\n' read -d "" -ra funcs < <(compgen -A function)
for func in "${funcs[@]} }"; do
[[ "$func" == test_* ]] && "$func";
done