new: add leaf_add_shell and leaf_remove_shell

This commit is contained in:
2025-12-25 05:12:25 -05:00
parent 67de5552e2
commit e8309233e7

40
leaf
View File

@@ -607,4 +607,44 @@ leaf_unpack_package() {
echo -e "${GREEN_COLOR}* Package ${PKG_PREFIX}/${PKG_NAME} has been installed${CLEAR_COLOR}\n"
}
leaf_add_shell() {
# usage: leaf_add_shell <abs_shell_path>
local shell="$1" f="/etc/shells" tmp
# ensure file exists
if [ ! -f "$f" ]; then
install -m 0644 -o root -g root /dev/null "$f" || return 1
fi
# already present? (exact whole-line match)
grep -Fxq -- "$shell" "$f" && return 0
tmp="$(mktemp "${f}.leaf.XXXXXX")" || return 1
cat "$f" > "$tmp" || { rm -f "$tmp"; return 1; }
# append with newline
printf '%s\n' "$shell" >> "$tmp" || { rm -f "$tmp"; return 1; }
# atomic replace
install -m 0644 -o root -g root "$tmp" "$f" || { rm -f "$tmp"; return 1; }
rm -f "$tmp"
}
leaf_remove_shell() {
# usage: leaf_remove_shell <abs_shell_path>
local shell="$1" f="/etc/shells" tmp
[ -f "$f" ] || return 0
# if not present, nothing to do
grep -Fxq -- "$shell" "$f" || return 0
tmp="$(mktemp "${f}.leaf.XXXXXX")" || return 1
# delete exact matching lines only
grep -Fvx -- "$shell" "$f" > "$tmp" || true
install -m 0644 -o root -g root "$tmp" "$f" || { rm -f "$tmp"; return 1; }
rm -f "$tmp"
}
main $@