天天看点

shell引入脚本_引入ABS,一种用于Shell脚本编写的编程语言 引入ABS,一种用于Shell脚本编写的编程语言 (Introducing ABS, a programming language for shell scripting)

shell引入脚本

by Alex Nadalin

通过亚历克斯·纳达林

引入ABS,一种用于Shell脚本编写的编程语言 (Introducing ABS, a programming language for shell scripting)

Over the past few days I took some time to work on a project I had in mind for ages, a scripting alternative to Bash: let me introduce you to the ABS programming language.

在过去的几天里,我花了一些时间从事一个我很久以来一直怀念的项目,它是Bash的脚本替代品:让我向您介绍ABS编程语言 。

为什么 (Why)

Let me keep this brief: we all love shell programming — automating repetitive tasks without too much effort.

让我简要介绍一下:我们都喜欢shell编程-无需过多工作就可以自动执行重复性任务。

We might probably agree that shell programming is also kind of nuts in terms of syntax:

我们可能会同意,就语法而言,shell编程也是一种疯狂:

if [ -z $STRING ]; then    ...fi
           

Like, ehm, what the hell? fi? -z? brackets?

像,嗯, 到底是什么? fi? -z? 括号?

Fighting with Bash, or the common shell programming language, can get intense from time to time. Writing code such as:

与Bash或常见的Shell编程语言进行的战斗可能会不时加剧。 编写代码,例如:

if (this == that) {    parts = this.split("/").filter(...).map(...)}
           

will bring tears to your eyes if you’re using the shell.

如果您使用外壳,将会使您的眼睛流泪。

Now, you can do similar things with any mainstream programming languages (the example above is valid JavasScript): what these languages are not great at is their integration with the underlying system — a shell is simply much more coincise / powerful from that perspective.

现在,您可以使用任何主流编程语言(上面的示例是有效的JavasScript)做类似的事情:这些语言最不擅长的是它们与底层系统的集成-从这个角度来看, shell简直就是巧合/功能强大 。

Imagine you could run code like:

假设您可以运行如下代码:

host = $(hostname)
           
if (host == "johns_computer") {    ...}
           

Well, you don’t have to “imagine” no more: ABS is a language that combines quick and simple system commands with a more elegant syntax.

好吧,您不必再“想象”:ABS是一种将快速,简单的系统命令与更优雅的语法结合在一起的语言。

Think of it as the best thing since candy, only to remember this is the definition ABS’ author gave you. But seriously, it’s pretty darn convenient.

认为这是自糖果以来最好的事情,只是要记住这就是ABS的作者给您的定义。 但是说真的,这很方便。

Don’t believe me? Read on!

不相信我吗 继续阅读!

例子 (Examples)

I’m a firm believer in the “show me the code!” mantra, so let’s quickly get to it. Running shell commands is extremely easy in ABS:

我坚信“ 向我展示代码! 咒语,所以让我们快速入门。 在ABS中运行shell命令非常简单:

# Get the content of your hostfile$(cat /etc/hosts)
           

and pipes work too:

和管道也可以:

# Check if a domain is in your hostfile$(cat /etc/hosts | grep domain.com | wc -l)
           

At this point we can just capture the output of our command and script over it:

现在,我们可以捕获命令和脚本的输出:

# Check if a domain is in your hostfilematches = $(cat /etc/hosts | grep domain.com | wc -l)
           
# If so, print an awesome stringif matches.int() > 0 {  echo("We got ya!")}
           

It won’t happen, but let’s say that an error happens:

它不会发生,但是让我们说一个错误发生了:

# Check if a domain is in your hostfilematches = $(cat /etc/hosts | grep domain.com | wc -l)
           
if !matches.ok {    echo("How do you even...")}
           
# If so, print an awesome stringif matches.int() > 0 {  echo("We got ya!")}
           

We could make this a bit more general:

我们可以使它更加通用:

$ cat script.abs# Usage $ abs script.abs domain.com# Check if a domain is in your hostfiledomain = arg(2)matches = $(cat /etc/hosts | grep $domain | wc -l)
           
if !matches.ok {    echo("How do you even...")}
           
# If so, print an awesome stringif matches.int() > 0 {  echo("We got %s!", domain)}
           

Now, strings are fairly boring, so we can try something more fun:

现在,字符串相当无聊,因此我们可以尝试一些更有趣的事情:

# Say we're getting some JSON from a commandx = $(echo '{"some": {"dope": "json"}}')x.json().some.dope # "json"
           
# Arrays, you say?tz = $(cat /etc/timezone) # "Asia/Dubai"parts = tz.split("/") # ["Asia", "Dubai"]
           
# You better destructure the hell out of that![continent, city] = tz.split("/")
           

…and so on. There are loads of “regular” things you can do with ABS, so I won’t focus much on those — let me show you the weirder parts instead:

…等等。 您可以使用ABS进行很多“常规”操作,因此我不会过多地关注这些内容-让我向您展示一些奇怪的部分:

# Avoiding the bug that happened because# we forgot to compare strings case-insensitively"HELLO" ~ "hello" # true
           
# Just range1..3 # [1, 2, 3]
           
# Combined comparison operator (thanks Ruby!)5 <=> 5 # 05 <=> 6 # -16 <=> 5 # 1
           
# Classic short-circuiting1 && 2 # 21 || 2 # 1
           

You can skim through the whole documentation within 15 minutes: ABS’ aim is not to be a general-purpose, feature-loaded language, so the surface isn’t that wide. In addition, if you’ve worked with languages such as JavaScript, Python or Ruby you won’t have troubles getting used to ABS.

您可以在15分钟内浏览整个文档 :ABS的目标不是成为一种通用的,功能加载的语言,因此表面并不那么宽。 另外,如果您使用过JavaScript,Python或Ruby之类的语言,那么习惯ABS不会遇到麻烦。

现在会发生什么? (What’s going to happen now?)

You can head over to ABS’ website, and learn more about the language. The brave ones will instead make a trip to ABS’s github repo and download a release to install it locally.

您可以访问ABS的网站 ,并进一步了解该语言。 勇敢的人将转而访问ABS的github存储库,并下载一个发行版以将其本地安装。

The braver ones will just:

勇敢的人只会:

bash <(curl https://www.abs-lang.org/installer.sh)
           

(you might need to sudo right before that)

(在此之前,您可能需要sudo)

Which one will you be?

你会是哪一个?

Originally published at odino.org (25th December 2018).You can follow me on Twitter — rants are welcome! ?

最初发布于odino.org (2018年12月25日)。 您可以在Twitter上关注我-欢迎咆哮! ?

翻译自: https://www.freecodecamp.org/news/introducing-abs-a-programming-language-for-shell-scripting-dfbd737d621/

shell引入脚本