8f71efc09b42d69cc053649fade92485d282a561

Author
mapc <musikmichael@web.de>
Committer
mapc <musikmichael@web.de>
Date

Message

Add helper to easily define default values for variables and env variables.

Diff

 1diff --git a/lib/functions.zsh b/lib/functions.zsh
 2index b4d89a64a5076dfdd10c9e0953707f4acd2aa70a..3770d4e82ec3fc480c8a21d7ba520e887559cd7f 100644
 3--- a/lib/functions.zsh
 4+++ b/lib/functions.zsh
 5@@ -44,4 +44,32 @@ function alias_value() {
 6 #
 7 function try_alias_value() {
 8     alias_value "$1" || echo "$1"
 9+}
10+
11+#
12+# Set variable "$1" to default value "$2" if "$1" is not yet defined.
13+#
14+# Arguments:
15+#    1. name - The variable to set
16+#    2. val  - The default value 
17+# Return value:
18+#    0 if the variable exists, 3 if it was set
19+#
20+function default() {
21+    test `typeset +m "$1"` && return 0
22+    typeset -g "$1"="$2"   && return 3
23+}
24+
25+#
26+# Set enviroment variable "$1" to default value "$2" if "$1" is not yet defined.
27+#
28+# Arguments:
29+#    1. name - The env variable to set
30+#    2. val  - The default value 
31+# Return value:
32+#    0 if the env variable exists, 3 if it was set
33+#
34+function env_default() {
35+    env | grep -q "^$1=" && return 0 
36+    export "$1=$2"       && return 3
37 }