Skip to content

Commit 2b96f87

Browse files
Shell variable interaction (#276)
* Shell variable interaction * remove amber_test println * return type Bool now shelle_var_set and shell_unset return true or false * Update src/std/main.ab Co-authored-by: Phoenix Himself <[email protected]> * Update src/std/main.ab Co-authored-by: Phoenix Himself <[email protected]> * Update src/std/main.ab Co-authored-by: Phoenix Himself <[email protected]> * Update src/std/main.ab Co-authored-by: Phoenix Himself <[email protected]> * Update src/std/main.ab Co-authored-by: Phoenix Himself <[email protected]> * add shell_constant_get test * define set/get/unset shell_var function as faillabe --------- Co-authored-by: Phoenix Himself <[email protected]>
1 parent 4ec4486 commit 2b96f87

13 files changed

+76
-0
lines changed

β€Žsrc/std/main.abβ€Ž

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,30 @@ pub fun get_env_var(var: Text): Text {
239239
pub fun load_env_file(): Null {
240240
unsafe $export "\$(xargs < .env)" > /dev/null$
241241
}
242+
243+
pub fun shell_isset(name: Text): Bool {
244+
$[[ ! -z \$\{!{nameof name}+z} ]]$ failed {
245+
return false
246+
}
247+
return true
248+
}
249+
250+
pub fun shell_constant_set(name: Text, val: Text): Null {
251+
$readonly \${nameof name}="\${nameof val}" 2> /dev/null$?
252+
}
253+
254+
pub fun shell_constant_get(name: Text): Text {
255+
return $echo \$\{!{nameof name}}$?
256+
}
257+
258+
pub fun shell_var_set(name: Text, val: Text): Null {
259+
$declare -g \${nameof name}="\${nameof val}" 2> /dev/null$?
260+
}
261+
262+
pub fun shell_var_get(name: Text): Text {
263+
return $echo \$\{!{nameof name}}$?
264+
}
265+
266+
pub fun shell_unset(name: Text): Null {
267+
$unset {name}$?
268+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * from "std"
2+
main {
3+
unsafe shell_constant_set("test_shell_constant_get", "Hello Amber!")
4+
unsafe $echo "\$test_shell_constant_get"$
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello Amber!
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * from "std"
2+
main {
3+
unsafe shell_constant_set("test_shell_constant_set", "Hello Amber")
4+
let str = ""
5+
str += unsafe $echo "\$test_shell_constant_set"$
6+
shell_constant_set("test_shell_constant_set", "Hello Amber") failed {
7+
str += " Shell Constant!"
8+
}
9+
echo str
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello Amber Shell Constant!
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * from "std"
2+
main {
3+
unsafe $test_var_isset="test"$
4+
if shell_isset("test_var_isset") {
5+
echo "test_var_isset"
6+
}
7+
8+
if shell_isset("test_var_isset2") {
9+
echo "test_var_isset2"
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_var_isset
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * from "std"
2+
main {
3+
unsafe shell_var_set("test_shell_unset", "Hello Amber!")
4+
unsafe $echo \$test_shell_unset$
5+
unsafe shell_unset("test_shell_unset")
6+
unsafe $echo \$test_shell_unset$
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello Amber!
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * from "std"
2+
main {
3+
unsafe $declare -g test_shell_var_get="Hello Amber!"$
4+
echo unsafe shell_var_get("test_shell_var_get")
5+
}

0 commit comments

Comments
Β (0)