LoginSignup
12
13

More than 5 years have passed since last update.

JavaScriptでシェルスクリプト、ついでにCoffeeScriptでもシェルスクリプト

Last updated at Posted at 2014-04-20

準備

brewを使って装備を整える。

brew install spidermonkey
brew install node
npm install -g coffee-script

spidermonkey : JavaScriptをシェルスクリプトとして使えるやつ
node : これでnpmコマンドをゲットする
coffee-script : CoffeeScriptのコンパイラ

自分の使っているシェルの設定ファイルにaliasを追加

.zprofile
alias -s js='js'
alias -s coffee='(){coffee -p $1 | js}'

2行目の匿名関数はZShellで通る構文ですので他シェルの方は工夫してください。

テストする

まずはJavaScript

以下のテストファイルを作成し

test.js
print('Hello World');

実行する。

./test.js

すると

Hello World

ここまではノーマル。

次はCoffeeScript

テストファイルを書いて

test.coffee
print 'Hello World'

実行する。

./test.coffee

すると

Hello World

coffee -pのpにより標準出力のみにしているのでコンパイル後のjsファイルが残らないため、あたかもシェルスクリプトのように使える。

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13