Solved Loops with POSIX SH scripts

two examples,

cat /home/x/.shrc
Code:
# Function to safely prepend a directory to PATH if it exists and isn't already there
path_add() {
    if [ -d "$1" ]; then
        case ":$PATH:" in
            *":$1:"*) ;; # Already in PATH
            *) PATH="$1:$PATH" ;;
        esac
    fi
}

export PATH=""

CUSTOM_PATHS="
/home/x/ADA/gnat2022-15.1.1/bin
$JAVA_HOME/bin
/home/x/.local/share/gem/ruby/3.3/bin
/home/x/.local/bin
/home/x/.local/share/coursier/bin
/home/x/.cargo/bin
/home/x/.dotnet/tools
/home/x/.acme.sh
/home/x/.roswell/lisp/quicklisp/bin
/home/x/.nimble/bin
/home/x/git/alire/bin
/home/x/git/DCD/bin
/home/x/git/serve-d
/home/x/git/crystalline/bin
/home/x/git/cppfront/source
/home/x/git/c3-lsp/server/bin
/home/x/git/zls/zig-out/bin
/home/x/git/pony-language-server/build/release
/home/x/git/ols
/home/x/git/clojure-lsp
/home/x/git/zls/zig-out/bin
/home/x/git/clojure-lsp/cli
/home/x/ldc2/ldc2/bin
/home/x/git/Odin
/home/x/config/doomemacs/bin
/usr/local/gnat12/bin
/usr/local/gnat13/bin
/usr/local/gerbil/bin
/usr/local/zig/bin
/usr/local/nim/bin
/home/x/bin
"

for dir in $CUSTOM_PATHS; do
    path_add "$dir"
done

path_add "/usr/local/bin"
path_add "/usr/local/sbin"
path_add "/usr/bin"
path_add "/usr/sbin"
path_add "/usr/local/llvm21/bin"
path_add "/bin"
path_add "/sbin"

export PATH

Because we want lower boot times, cat /etc/rc.local
Code:
# 2. Define the list of services
SERVICES="
blackbox_exporter
moused
jail
nginx
apache24
minidlna
cupsd
blackbox_exporter
node_exporter
gstat_exporter
telegraf
prometheus
grafana
zabbix74_server
zabbix_agentd
zabbix_server
"

# 3. Loop through and start each service in the background
for srv in $SERVICES; do
    /usr/bin/nohup /usr/sbin/service "$srv" onestart &
done
 
You can do this:
Code:
SERVICES="blackbox_exporter moused jail nginx apache24 minidlna cupsd blackbox_exporter node_exporter gstat_exporter telegraf prometheus grafana zabbix74_server zabbix_agentd zabbix_server"
IFS is set to split on white space by default.

Code:
     IFS       Input Field Separators.  This is initialized at startup to
               ⟨space⟩, ⟨tab⟩, and ⟨newline⟩ in that order.  This value also
               applies if IFS is unset, but not if it is set to the empty
               string.  See the White Space Splitting section for more
               details.
 
While the services startup is fine, the incremental addition to the path is grossly inefficient.

export PATH="/path1:/path2:/path3" as one operation, is the most efficient way, not that a few microseconds or a few extra KB of ram make that much of a difference in shell scripts. So, the difference is more academic than useful.
 
Because we want lower boot times, cat /etc/rc.local
Code:
# 2. Define the list of services
SERVICES="
blackbox_exporter
moused
jail
nginx
apache24
minidlna
cupsd
blackbox_exporter
node_exporter
gstat_exporter
telegraf
prometheus
grafana
zabbix74_server
zabbix_agentd
zabbix_server
"

# 3. Loop through and start each service in the background
for srv in $SERVICES; do
    /usr/bin/nohup /usr/sbin/service "$srv" onestart &
done
A point I think is worth mentioning: background starting of services is dangerous because you have no direct control over whether their prerequisites have been started. I'm no fan of systemd, but that is a primary reason it was developed: to manage complex strartup trees in a concurrent fashion.
 
Essential services like, influx,mariadb&postgresql i start using rc.conf. These are all non-essential. No problem if they fail to start.
 
Back
Top