Skip to content

Commit

Permalink
docs: developer/bash formatting updates
Browse files Browse the repository at this point in the history
Small formatting updates.  Use "bash" as source type so it syntax highlights.
  • Loading branch information
ianw authored and LaszloGombos committed Oct 17, 2024
1 parent 7b8bb0a commit 03d8951
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions doc_site/modules/ROOT/pages/developer/bash.adoc
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
= BASH Notes
= Bash Notes

== basename

Don't use `basename`, use:

[,shell]
[,bash]
----
file=${path##*/}
file=${path##*/}
----

== dirname

Don't use `dirname`, use:

[,shell]
[,bash]
----
dir=${path%/*}
dir=${path%/*}
----

== shopt

If you set `shopt` in a function, reset to its default state with `trap`:

[,shell]
[,bash]
----
func() {
trap "$(shopt -p globstar)" RETURN
Expand All @@ -37,7 +37,7 @@ Try to use `globstar` and `nullglob` or null byte terminated strings.

Instead of:

[,shell]
[,bash]
----
func() {
for file in $(find /usr/lib* -type f -name 'lib*.a' -print0 ); do
Expand All @@ -48,7 +48,7 @@ func() {

use:

[,shell]
[,bash]
----
func() {
trap "$(shopt -p nullglob globstar)" RETURN
Expand All @@ -63,7 +63,7 @@ func() {

Or collect the filenames in an array, if you need them more than once:

[,shell]
[,bash]
----
func() {
trap "$(shopt -p globstar)" RETURN
Expand All @@ -80,7 +80,7 @@ func() {

Or, if you really want to use `find`, use `-print0` and an array:

[,shell]
[,bash]
----
func() {
mapfile -t -d '' filenames < <(find /usr/lib* -type f -name 'lib*.a' -print0)
Expand All @@ -94,7 +94,7 @@ NOTE: `-d ''` is the same as `-d $'\0'` and sets the null byte as the delimiter.

or:

[,shell]
[,bash]
----
func() {
find /usr/lib* -type f -name 'lib*.a' -print0 | while read -r -d '' file; do
Expand All @@ -105,7 +105,7 @@ func() {

or

[,shell]
[,bash]
----
func() {
while read -r -d '' file; do
Expand All @@ -120,7 +120,7 @@ Use the tool options for null terminated strings, like `-print0`, `-0`, `-z`, et

Instead of:

[,shell]
[,bash]
----
func() {
other-cmd $(for k in "$@"; do echo "prefix-$k"; done)
Expand All @@ -129,7 +129,7 @@ func() {

do

[,shell]
[,bash]
----
func() {
other-cmd "${@/#/prefix-}"
Expand All @@ -138,7 +138,7 @@ func() {

or suffix:

[,shell]
[,bash]
----
func() {
other-cmd "${@/%/-suffix}"
Expand All @@ -149,18 +149,18 @@ func() {

Here we have an associate array `_drivers`, where we want to print the keys separated by ',':

[,shell]
[,bash]
----
if [[ ${!_drivers[*]} ]]; then
echo "rd.driver.pre=$(IFS=, ;echo "${!_drivers[*]}")" > "${initdir}"/etc/cmdline.d/00-watchdog.conf
fi
if [[ ${!_drivers[*]} ]]; then
echo "rd.driver.pre=$(IFS=, ;echo "${!_drivers[*]}")" > "${initdir}"/etc/cmdline.d/00-watchdog.conf
fi
----

== Optional parameters to commands

If you want to call a command `cmd` with an option, if a variable is set, rather than doing:

[,shell]
[,bash]
----
func() {
local param="$1"
Expand All @@ -175,7 +175,7 @@ func() {

do it like this:

[,shell]
[,bash]
----
func() {
local param="$1"
Expand All @@ -195,7 +195,7 @@ func

If you want to specify the option even with an empty string do this:

[,shell]
[,bash]
----
func() {
local -a special_params
Expand Down Expand Up @@ -225,7 +225,7 @@ func

Or more simple, if you only have to set an option:

[,shell]
[,bash]
----
func() {
if [[ ${1+_} ]]; then
Expand Down

0 comments on commit 03d8951

Please sign in to comment.