一个有趣的小工具batsh
Batsh 是一个简单的编程语言,可方便的编译成 Bash 和 Windows 批处理脚本。相当于你需要写一次脚本就可以在不同的平台上运行。而且没有任何依赖包。可以直接访问http://batsh.org 在线编译。这个脚本编译工具使用 OCaml编写,该工具采用类C语法,容易上手。
赋值:
a = 1;
b = “string”;
c = [1, 2, “str”, true, false];
表达式:
a = 1 + 2;
b = a * 7;
c = “Con” ++ “cat”;
d = c ++ b;
命令:
// On UNIX
output = ls();
// On Windows
output = dir();
// Platform independent
output = readdir();
// Test existence
ex = exists(“file.txt”);
If condition
a = 3;
if (a > 2) {
println(“Yes”);
} else {
println(“No”);
}
Loop
// Fibonacci
n = 0;
i = 0;
j = 1;
while (n < 60) {
k = i + j;
i = j;
j = k;
n = n + 1;
println(k);
}
函数定义:
v1 = “Global V1”;
v2 = “Global V2”;
function func(p) {
v1 = “Local ” ++ p;
global v2;
v2 = “V3 Modified.”;
}
func(“Var”);
Recursion
function fibonacci(num) {
if (num == 0) {
return 0;
} else if (num == 1) {
return 1;
} else {
return (fibonacci(num – 2) + fibonacci(num – 1));
}
}
println(fibonacci(8));
内置函数:
In order to make script cross-platform, Batsh provided some “built-in” functions that will compile to platform-dependent code. It is assumed that Bash script runs on Linux or Mac OS and Batch script runs on Windows (XP or higher), which means Cygwin or wine are not supported.
print(text, …)
Prints a text string to console without a newline.
println(text, …)
Prints a text string to console with a new line (LF for bash, CRLF for batch).
call(path, arg, …)
Runs command from path through shell.
bash(rawStatement)
Put rawStatement into compiled code for Bash. Ignore for Windows Batch.
batch(rawStatement)
Put rawStatement into compiled code for Windows Batch. Ignore for Bash.
readdir(path)
Equals to ls and dir /w.
exists(path)
Test existence of given path.
命令行用法
NAME
batsh – A language that compiles to Bash and Windows Batch.
SYNOPSIS
batsh COMMAND …
COMMANDS
bash
Compile to Bash script.
batsh
Format source file.
winbat
Compile to Windows Batch script.
OPTIONS
–help[=FMT] (default=pager)
Show this help in format FMT (pager, plain or groff).
–version
Show version information.
不错的小工具,但是好像用的人不多哦。不过微软的cmd工具已经停止开发了,现在主推powershell。