[projects/rc-scripts] test that "local" variables work for sub-functions as well

glen glen at pld-linux.org
Sat Oct 10 01:32:31 CEST 2015


commit f44eb39ce58eef2724ea8636f178b7da7c0bd260
Author: Elan Ruusamäe <glen at delfi.ee>
Date:   Thu Oct 8 17:33:28 2015 +0300

    test that "local" variables work for sub-functions as well

 t/inherit.sh | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
---
diff --git a/t/inherit.sh b/t/inherit.sh
new file mode 100755
index 0000000..a84de96
--- /dev/null
+++ b/t/inherit.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+# test that variables local to function are inherited when another function is called out
+#
+# the whole program should output:
+#---
+#sub: var='var'
+#sub: subvar='subvar'
+#main: var='var'
+#main: subvar='subvar'
+#outer: var=''
+#outer: subvar=''
+#---
+
+sub() {
+	subvar=subvar
+	echo "sub: var='$var'"
+	echo "sub: subvar='$subvar'"
+}
+
+main() {
+	local var=var
+	local subvar
+
+	sub
+	echo "main: var='$var'"
+	echo "main: subvar='$subvar'"
+}
+
+main
+echo "outer: var='$var'"
+echo "outer: subvar='$subvar'"
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/rc-scripts.git/commitdiff/1a0c02d0983a76562fa875a1bab45ece246b26a6



More information about the pld-cvs-commit mailing list