forked from colonelpanic/dotfiles
[shell] Add several functions
This commit is contained in:
parent
8e35937523
commit
99d9d843b2
7
dotfiles/lib/functions/add_to_path
Executable file
7
dotfiles/lib/functions/add_to_path
Executable 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 "$@"
|
22
dotfiles/lib/functions/join_by_char
Executable file
22
dotfiles/lib/functions/join_by_char
Executable 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 "$@"
|
7
dotfiles/lib/functions/nixpkgs_source
Executable file
7
dotfiles/lib/functions/nixpkgs_source
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
function nixpkgs_source {
|
||||||
|
nix eval --expr '<nixpkgs>' --impure
|
||||||
|
}
|
||||||
|
|
||||||
|
nixpkgs_source
|
7
dotfiles/lib/functions/path_from_lines
Executable file
7
dotfiles/lib/functions/path_from_lines
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
function path_from_lines {
|
||||||
|
join_by_char ":"
|
||||||
|
}
|
||||||
|
|
||||||
|
path_from_lines "$@"
|
@ -1,7 +1,8 @@
|
|||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
function path_lines {
|
function path_lines {
|
||||||
split_by_char ":" "$PATH"
|
var="${1:-PATH}"
|
||||||
|
split_by_char ":" "${!var}"
|
||||||
}
|
}
|
||||||
|
|
||||||
path_lines
|
path_lines "$@"
|
||||||
|
Loading…
Reference in New Issue
Block a user