Jelajahi Sumber

clean up shell functions as subshell example

George Jones 2 tahun lalu
induk
melakukan
e4ded945e8
1 mengubah file dengan 10 tambahan dan 13 penghapusan
  1. 10 13
      home/public/snippits/bash/functions-as-subshells.sh

+ 10 - 13
home/public/snippits/bash/functions-as-subshells.sh

@@ -1,26 +1,23 @@
 # https://cuddly-octo-palm-tree.com/posts/2021-10-31-better-bash-functions/
 f() (
     # code
-    var1=${1:-"default value"}
+    var1=${1:-"default"}
 
-    # Always return numeric status (that's all bash supports)
-    #
-    # 0 == succuess
-    # other == failure
+    
+    if [ "${var1}" == "default" ]; then
+        echo errors and warning messages to stderr 1>&2
+        return 1 # error, outut undefined
+    fi
 
-    echo errors and warning messages to stderr 1>&2
-
-    echo "string return value to stdout - var1 is $var1"
-    return 1 
+    echo "stirng value to stdout"
+    return 0 # success, output defined
 )
 
 
-f
+return=`f` && echo SUCCESS return value is $return  || echo FAILURE no return
 echo
 
-f foo
+return=`f FOO` && echo SUCCESS return value is $return  || echo FAILURE no return
 echo
 
-f bar
-echo