javascript脚本_JavaScript Shell脚本

javascript脚本

javascript脚本

As you probably know, JavaScript is not limited to the browser. There's server-side JavaScript, JS for various extensions, you can script Photoshop operations with JavaScript if you feel like it. Or compile Windows executables. You see where I'm going with this. JavaScript is everywhere 🙂

如您所知,JavaScript不仅限于浏览器。 服务器端JavaScript,各种扩展的JS,如果愿意,可以使用JavaScript编写Photoshop操作脚本。 或编译Windows可执行文件。 你知道我要去哪里。 JavaScript无处不在🙂

And yes, you can do shell scripting in JavaScript. On any platform you can use Rhino to run your scripts. On Windows there's this WSH, Windows Scripting Host you can benefit from, built right into the OS so you don't have to install anything. You can run your JavaScript shell scripts with the cscript utility like:

是的,您可以使用JavaScript编写Shell脚本。 在任何平台上,您都可以使用Rhino来运行脚本。 在Windows上,您可以受益于此WSH,Windows脚本宿主直接内置在OS中,因此您无需安装任何东西。 您可以使用cscript实用程序运行JavaScript shell脚本,例如:

C:\> cscript jslint.js

And on the Mac there's JavaScriptCore by WebKit. WebKit is not limited to Safari, it's used all over the place on the Mac. So there's a utility called jsc which can run your scripts.

在Mac上有WebKitJavaScriptCore。 WebKit不仅限于Safari,它在Mac上的各处都已使用。 因此,有一个名为jsc的实用程序可以运行您的脚本。

JSC测试运行 (JSC test run)

JSC (JavaScriptCore) is well hidden in

JSC(JavaScriptCore)很好地隐藏在

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc

Check it out, it should be there. And if it is, why not use it via a "shortcut"? So step 1:

检查出来,它应该在那里。 如果是的话,为什么不通过“快捷方式”使用它呢? 所以第一步:

$ sudo ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /bin/jsc

Step 2... no, there's no step 2, just give it a shot:

步骤2 ...不,没有步骤2,只需试一试:

$ jsc -h
Usage: jsc [options] [files] [-- arguments]
  -d         Dumps bytecode (debug builds only)
  -e         Evaluate argument as script code
  -f         Specifies a source file (deprecated)
  -h|--help  Prints this help message
  -i         Enables interactive mode (default if no files are specified)
  -s         Installs signal handlers that exit on a crash (Unix platforms only)

So you can just run any JavaScript code on the command line. You can also use jsc as a JavaScript console to type away stuff. Probably not that useful, because you have Firebug console to type into. But still, an option.

因此,您可以在命令行上运行任何JavaScript代码。 您还可以将jsc用作JavaScript控制台来键入内容。 可能不是那么有用,因为您可以输入Firebug控制台。 但仍然是一种选择。

$ jsc
> var a = 1;
undefined
> a++
1
> a
2
> 

Shell版本的在线工具(Shell version of an online tool)

Here's an example. Last night I worked on a quick hack to experiment with minifying CSS. There's an online tool over here as a result. Can I run this tool on the command line? Sure.

这是一个例子。 昨晚,我研究了一种快速破解方法,以尝试最小化CSS。 这里有一个在线工具,在这里作为一个结果。 我可以在命令行上运行此工具吗? 当然。

So imagine you have a JavaScript that parses CSS that looks like this. It defines an object called cssparse. You can use it in a browser-based tool, but also in the command-line version, without any changes. All you need is to create a new file that will be the shell version of the tool, say csspsh.js. In it put the something like:

所以,想象一下你有一个JavaScript代码解析CSS是这个样子的。 它定义了一个称为cssparse的对象。 您可以在基于浏览器的工具中使用它,也可以在命令行版本中使用它,而无需进行任何更改。 您需要做的就是创建一个新文件,该文件将是该工具的外壳版本,例如csspsh.js 。 在其中放入类似的内容:

if (!arguments[0]) {
    print('usage:\n $ jsc csspsh.js "`cat parseme.css`"');
    quit();
}
 
load('cssp.js');
 
print(cssparse.parse(arguments[0]));

You can probably guess but:

您可能会猜测,但:

  • arguments[] array-like object contains command-line arguments

    arguments[]类似数组的对象包含命令行参数

  • print() prints to the console

    print()打印到控制台

  • quit() exits JSC

    quit()退出JSC

  • load() loads and executes an external file

    load()加载并执行一个外部文件

How do you pass arguments to your shell script? After a -- delimiter, like so:

如何将参数传递给Shell脚本? 在-分隔符之后,如下所示:

$ jsc csspsh.js -- one two three

And since this particular script works with contents of files, I can use cat to read the file and pass it to the script.

而且由于此特定脚本可处理文件的内容,所以我可以使用cat读取文件并将其传递给脚本。

$ jsc csspsh.js -- "`cat my.css`"

Shell脚本走了!(Shell-script away!)

So there you have it. Shell scripting with JavaScript is at your fingertips, either Mac or Windows, or anywhere with Rhino. If you have some cool scripts that you want to run the on the command line, like cron jobs or some automated process, there has never been a better time 🙂

所以你有它。 在Mac或Windows上,或在任何Rhino上,使用JavaScript编写Shell脚本都唾手可得。 如果您有一些很酷的脚本要在命令行上运行,例如cron作业或某些自动化过程,那么从来没有比现在更好的时间了🙂

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/javascript-shell-scripting/

javascript脚本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值