广酉快乐双彩开奖结果:JavaScript shell scripting

September 3rd, 2009. Tagged: 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 ??

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:

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.

JSC test run

JSC (JavaScriptCore) is well hidden in

/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:

$ 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.

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

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.

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:

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
  • print() prints to the console
  • quit() exits JSC
  • load() loads and executes an external file

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

$ 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.

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

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 ??

Tell your friends about this post: Facebook, Twitter, Google+

Sorry, comments disabled and hidden due to excessive spam.

Meanwhile, hit me up on twitter @stoyanstefanov


  • 邀你参赛世界杯《热血足球》正版手游iOS公测开启 2019-06-22
  • 【理上网来喜迎十九大】推动中国经济乘风破浪行稳致远 2019-06-22
  • 向“份子钱”开刀 广州的士再改革 2019-06-18
  • 不是从天上掉下来的,难道是从“文革”中走来的吗? 2019-06-12
  • 把黄土高坡变绿的造林人 2019-06-12
  • 缅甸国务资政昂山素季会见魏凤和 2019-06-06
  • 2017年第96届全国糖酒会在成都圆满落幕 2019-06-06
  • 杂志榜三甲大洗牌,“国家人文历史”夺榜首今日头条媒体榜 2019-05-25
  • 76人参加高山滑雪跨界跨项选材测试 或进冬奥备战梯队 2019-05-18
  • 不是特效!上港队长一脚踢中海鸥 这脚法简直坑爹 2019-05-08
  • 新疆坚决打好污染防治攻坚战 2019-05-07
  • 高考在即,晋中市招生考试管理中心提醒广大考生及家长要把握好高考“四个趋势” 2019-04-25
  • 运城市在长三角招商引资149.9亿元 2019-04-08
  • 济南五胞胎雪虎宝宝亮相 四雌一雄萌态十足 2019-04-08
  • 去产能迎年中考 煤炭、钢铁企业债务问题依然存在 2019-04-07
  • 彩票走势图双色球推荐 广西快三是骗人的吗 稳赚包平特肖乛三期 靠谱的老时时彩怎么玩法||www.bgwxfz.com.cn/ 足彩竞猜网 排列5杀号定胆必赢 安徽时时彩中奖规则 浙江快乐彩玩法 海南飞鱼180803057 国际娱乐城网 大乐透走势图9188论坛 十赌九赢一波中特 双色球复式中六等奖 辽宁11选5走势图表定牛 海南飞鱼游戏历史开奖