基础

amber简而言之就是一门现代化的语言,用于生成bash脚本,解决bash脚本晦涩难懂且容易出错的问题。文档: https://docs.amber-lang.com/

安装&卸载

# 全局安装
bash <(curl -s "https://raw.githubusercontent.com/amber-lang/amber/master/setup/install.sh")

# 用户安装
bash -- --user <(curl -s "https://raw.githubusercontent.com/amber-lang/amber/master/setup/install.sh")

# 卸载
curl -s "https://raw.githubusercontent.com/Ph0enixKM/AmberNative/master/setup/uninstall.sh" | bash

语言基础

数据类型

Amber有以下几种数据类型:

  • Text:字符串。eg:“Welcome to the jungle”
  • Num:数字(整数、浮点数等)。eg:42、-123.4
  • Bool:布尔值。eg:true、false
  • Null:空值。eg:null
  • []:数组。eg:[Num]:[1,2,3],[Text]:[“Test string”]
    • 当前因为bash的限制,不支持多维数组。

表达式

表达式只对相同操作数生效。

加法操作符 +

对于不同的操作数类型得到的结果也是不同的。

12 + 42 // 54
"Hello " + "World!" // "Hello World!"
[1, 2] + [3, 4] // [1, 2, 3, 4]

算数操作符

仅在Num操作数能够使用

  • + Arithmetic sum
  • - Substraction/Negative on variable
  • * Multiplication
  • / Division
  • % Modulo operation
  • +=:和python中的相同

比较

==!=,可以用在两侧相同类型的操作数上。

"foo" != "bar"
42 == 42
true != false
"equal" == "equal"

逻辑

用在Bool上,有notorand

18 >= 12 and not false

文本格式化

let name = "Json"
let age = 18
echo "Hi, I'm {name}. I'm {age} years old."

变量

// 声明变量
let name = "Json"

// 变量再次赋值
name = "Test"

// 使用变量
echo name

声明覆盖

// 声明result为Num类型
let result = 123
// result覆盖成了Text类型
let result = "Hello my friend."

预留

Amber预留了双下划线__开头的变量一级关键词letif等。

条件

没看到elif

if

if age >= 16 {
	echo "Welcom"
}

// with else
if age >= 16 {
    echo "Welcome"
} else {
    echo "Entry not allowed"
}

// 使用:符号
if age >= 16: echo "Welcome"
else: echo "Entry not allowed"

// Or

if age >= 16:
    echo "Welcome"
else:
    echo "Entry not allowed"

If chain

if {
    drink == "water" {
        echo "Have a natural, mineralized water"
    }
    drink == "cola" {
        echo "Here is your fresh cola"
    }
    else {
        echo "Sorry, we have none of that"
    }
}

// Alternatively, as previously mentioned:

if {
    drink == "water": echo "Have a natural, mineralized water"
    drink == "cola": echo "Here is your fresh cola"
    else: echo "Sorry, we have none of that"
}

三元操作符

let candy = count > 1 then "candies" else "candy"