[shell] Add several functions

This commit is contained in:
Ivan Malison 2023-08-19 16:50:42 -06:00
parent 8e35937523
commit 99d9d843b2
5 changed files with 46 additions and 2 deletions

View File

@ -0,0 +1,7 @@
#!/usr/bin/env sh
function add_to_path {
path_lines "$2" | { cat -; echo "$1" } | path_from_lines
}
add_to_path "$@"

View File

@ -0,0 +1,22 @@
#!/usr/bin/env sh
function join_by_char {
delimiter="$1"
if [ -z "$delimiter" ]; then
echo "Please specify a delimiter."
return 1
fi
while IFS= read -r line; do
# Append the line to the result with the delimiter, but not for the first line
if [ -z "$result" ]; then
result="$line"
else
result="$result$delimiter$line"
fi
done
echo "$result"
}
join_by_char "$@"

View File

@ -0,0 +1,7 @@
#!/usr/bin/env sh
function nixpkgs_source {
nix eval --expr '<nixpkgs>' --impure
}
nixpkgs_source

View File

@ -0,0 +1,7 @@
#!/usr/bin/env sh
function path_from_lines {
join_by_char ":"
}
path_from_lines "$@"

View File

@ -1,7 +1,8 @@
#!/usr/bin/env sh
function path_lines {
split_by_char ":" "$PATH"
var="${1:-PATH}"
split_by_char ":" "${!var}"
}
path_lines
path_lines "$@"