Nodejs Script

可执行脚本

使用 Nodejs 写执行脚本

1
2
$ which node
/usr/local/bin/node

./hello.sh

1
2
#! /usr/local/bin/node
console.log('hello world');
1
2
$ ./hello.sh
hello world

命令行参数

./hello.sh

1
2
#! /usr/local/bin/node
console.log('hello ', process.argv[2]);
1
2
$ ./hello.sh Abel
hello Abel

shelljs

shelljs 模块重新包装了 child_process,调用系统命令更加方便。它需要安装后使用。

yargs

shelljs 只解决了如何调用 shell 命令,而 yargs 模块能够解决如何处理命令行参数。它也需要安装。

参考

感谢支持,让我安静的做蚂蚁梦!