Skip to content

Commit

Permalink
Version 3.0.3 update
Browse files Browse the repository at this point in the history
  • Loading branch information
kigster committed Aug 1, 2022
1 parent 50f17b3 commit 5fba3d4
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[separator=—]
= Bashmatic® — BASH-based DSL helpers for humans, sysadmins, and fun.
// vim: ft=asciidoc
:author: Version v3.0.2
:author: Version v3.0.3
:doctype: book
:source-highlighter: rouge
:rouge-style: base16.monokai
Expand Down
Binary file modified README.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v3.0.3](https://github.com/kigster/bashmatic/tree/v3.0.3) (2022-07-11)

[Full Changelog](https://github.com/kigster/bashmatic/compare/v3.0.2...v3.0.3)

## [v3.0.2](https://github.com/kigster/bashmatic/tree/v3.0.2) (2022-06-14)

[Full Changelog](https://github.com/kigster/bashmatic/compare/v3.0.1...v3.0.2)
Expand Down
164 changes: 148 additions & 16 deletions doc/FUNCTIONS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* xref:#module-asciidoc[asciidoc]
* xref:#module-audio[audio]
* xref:#module-aws[aws]
* xref:#module-background[background]
* xref:#module-bashit[bashit]
* xref:#module-bashmatic[bashmatic]
* xref:#module-brew[brew]
Expand Down Expand Up @@ -912,6 +913,70 @@ aws.s3.upload ()
----


=== Module `background`

==== `background.log`

[source,bash]
----
background.log ()
{
local index="${1:-0}"
printf "${background_log/{{index}}/${index}}"
}
----

==== `background.run`

[source,bash]
----
background.run ()
{
for command in "$@"
do
local index=${#running_jobs[@]}
local log="$(background.log "${index}")"
( ${command} 2>&1 | tee -a "${log}" ) & running_jobs[$!]=-1
done
}
----

==== `background.sigchld`

[source,bash]
----
background.sigchld ()
{
for pid in "${!running_jobs[@]}"
do
if [ ! -d "/proc/$pid" ]; then
wait "${pid}"
local code="$?"
running_jobs["${pid}"]=${code}
fi
done
}
----

==== `background.wait`

[source,bash]
----
background.wait ()
{
local timeout
echo Starting background processes with pidS ${!pidS[@]}
echo Starting dd
timeout 15s dd if=/dev/zero of=/dev/null
echo dd terminated
}
----


=== Module `bashit`

==== `bashit-activate`
Expand Down Expand Up @@ -4166,6 +4231,30 @@ file.ask.if-exists ()
----

==== `file.count.lines`

[source,bash]
----
file.count.lines ()
{
[[ -f "$1" ]] || return 1
wc -l "$1" | awk '{print $1}' | tr -d '\n'
}
----

==== `file.count.words`

[source,bash]
----
file.count.words ()
{
[[ -f "$1" ]] || return 1
wc -w "$1" | awk '{print $1}' | tr -d '\n'
}
----

==== `file.exists-and-newer-than`

[source,bash]
Expand Down Expand Up @@ -4442,6 +4531,21 @@ file.size ()
----

==== `file.size.gb`

[source,bash]
----
file.size.gb ()
{
local file="$1"
shift
local s=$(file.size "${file}")
local gb=$(echo $((s / 10 / 1024 / 1024 )) | sedx 's/([0-9][0-9])$/.\1/g')
printf "%.1f Gb" "${gb}"
}
----

==== `file.size.mb`

[source,bash]
Expand All @@ -4451,7 +4555,7 @@ file.size.mb ()
local file="$1"
shift
local s=$(file.size "${file}")
local mb=$(echo $((s / 10000)) | sedx 's/([0-9][0-9])$/.\1/g')
local mb=$(echo $((s / 10/ 1024)) | sedx 's/([0-9][0-9])$/.\1/g')
printf "%.2f MB" "${mb}"
}
Expand Down Expand Up @@ -4507,18 +4611,6 @@ file.strip.extension ()
----

==== `file.sync`

[source,bash]
----
file.sync ()
{
local from="$1"
local to="$2"
}
----

==== `file.temp`

[source,bash]
Expand Down Expand Up @@ -4592,13 +4684,24 @@ files.map.shell-scripts ()
----

==== `ls.gb`

[source,bash]
----
ls.gb ()
{
du -g -d 1 "$@" | sort -rn
}
----

==== `ls.mb`

[source,bash]
----
ls.mb ()
{
du -k | grep -v '\''./.*\/'\' | sort -n | awk '{ printf("%20.1fMb %s\n", $1/1024, $2 )}' | tail -10
du -m -d 1 "$@" | sort -rn
}
----
Expand Down Expand Up @@ -13348,6 +13451,35 @@ pause.short ()
----

==== `system.uname`

[source,bash]
----
system.uname ()
{
command -v uname && return 0
if [[ -x /bin/uname ]]; then
printf -- "/bin/uname"
else
if [[ -x /usr/bin/uname ]]; then
printf -- "/usr/bin/uname"
else
if [[ -x /sbin/uname ]]; then
printf -- "/sbin/uname"
else
if [[ -x /usr/sbin/uname ]]; then
printf -- "/usr/sbin/uname"
else
echo "Can't find uname, aborting..." 1>&2
exit 1
fi
fi
fi
fi
}
----

==== `util.append-to-init-files`

[source,bash]
Expand Down Expand Up @@ -13388,7 +13520,7 @@ util.append-to-init-files ()
----
util.arch ()
{
echo -n "${AppCurrentOS}-$(/usr/bin/uname -m)-$(/usr/bin/uname -p)" | /usr/bin/tr '[:upper:]' '[:lower:]'
echo -n "${AppCurrentOS}-$($(system.uname) -m)-$($(system.uname) -p)" | /usr/bin/tr '[:upper:]' '[:lower:]'
}
----
Expand Down Expand Up @@ -13616,7 +13748,7 @@ util.lines-in-folder ()
----
util.os ()
{
export AppCurrentOS="${AppCurrentOS:-$(/usr/bin/uname -s | /usr/bin/tr '[:upper:]' '[:lower:]')}"
export AppCurrentOS="${AppCurrentOS:-$( $(system.uname) -s | /usr/bin/tr '[:upper:]' '[:lower:]')}"
}
----
Expand Down
30 changes: 30 additions & 0 deletions doc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,20 @@ Grab the version from `postgres` binary in the PATH and remove fractional sub-ve
---
## File `lib/7z.sh`
# lib/7z.sh
p7zip conversions routines.
---
Expand All @@ -1458,6 +1472,8 @@ Grab the version from `postgres` binary in the PATH and remove fractional sub-ve
### `dir.with-file()`
Returns the first folder above the given that contains a file.
#### Arguments
* @arg1 file without the path to search for, eg ".evnrc"
Expand Down Expand Up @@ -1823,6 +1839,20 @@ Prints values of all variables starting with prefixes in args
---
## File `lib/background.sh`
# lib/background.sh
Run a bunch of jobs on the background and wait for their completion
---
Expand Down
Binary file modified doc/USAGE.pdf
Binary file not shown.
63 changes: 63 additions & 0 deletions lib/background.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash
# vim: ft=bash
# @copyright © 2016-2022 Konstantin Gredeskoul, All rights reserved
# @license MIT License.
#
# @file lib/background.sh
# @description Run a bunch of jobs on the background and wait for their completion

declare -a running_jobs
declare -a completed_jobs

declare background_log=$(mktemp -t "bashmatic.bg-{{index}}.log")

function background.log() {
local index="${1:-0}"
printf "${background_log/{{index}}/${index}}"
}

function background.sigchld() {
for pid in "${!running_jobs[@]}"; do
if [ ! -d "/proc/$pid" ]; then
wait "${pid}"
local code="$?"
running_jobs["${pid}"]=${code}
fi
done
}

function background.run() {
for command in "$@"; do
local index=${#running_jobs[@]}
local log="$(background.log "${index}")"
(${command} 2>&1 | tee -a "${log}") &
running_jobs[$!]=-1
done
}

function background.wait() {
local timeout
echo Starting background processes with pidS ${!pidS[@]}
echo Starting dd
timeout 15s dd if=/dev/zero of=/dev/null
echo dd terminated

}

trap background.sigchld SIGCHLD

(
sleep 9
exit 44
) &
pidS[$!]=1
(
sleep 7
exit 43
) &
pidS[$!]=1
(
sleep 5
exit 42
) &
pidS[$!]=1

0 comments on commit 5fba3d4

Please sign in to comment.