Unix shell commands for Node.js
可以使shell脚本不依赖于unix环境。

命令行

shelljs/shx命令行使用的

  1. $ shx mkdir -p foo
  2. $ shx touch foo/bar.txt
  3. $ shx rm -rf foo

实例

  1. var shell = require('shelljs');
  2. if (!shell.which('git')) {
  3. shell.echo('Sorry, this script requires git');
  4. shell.exit(1);
  5. }
  6. // Copy files to release dir
  7. shell.rm('-rf', 'out/Release');
  8. shell.cp('-R', 'stuff/', 'out/Release');
  9. // Replace macros in each .js file
  10. shell.cd('lib');
  11. shell.ls('*.js').forEach(function (file) {
  12. shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
  13. shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file);
  14. shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file);
  15. });
  16. shell.cd('..');
  17. // Run external tool synchronously
  18. if (shell.exec('git commit -am "Auto-commit"').code !== 0) {
  19. shell.echo('Error: Git commit failed');
  20. shell.exit(1);
  21. }

命令

  • 除非另外说明,否则所有命令都是同步运行。
  • 接受bash的全局字符(*?等)
  • 和node global模块兼容

cat

cat([options,] file [, file ...])
cat([options,] file_array)

可用options:

  • -v显示所有行的行号
  1. var str = cat('file*.txt');
  2. var str = cat('file1', 'file2');
  3. var str = cat(['file1', 'file2']); // same as above

cd

cd([dir])

chmod

chmod([options,] octal_mode || octal_string, file)
chmod([options,] symbolic_mode, file)

可用option:

  • -v输出文件诊断信息
  • -c在文件变更时才输出诊断信息
  • -R以递归方式更改
  1. chmod(755, '/Users/brandon');
  2. chmod('755', '/Users/brandon'); // same as above
  3. chmod('u+x', '/Users/brandon');
  4. chmod('-R', 'a-w', '/Users/brandon');

cp

cp([options,] source [, source ...], dest)
cp([options,] source_array, dest)

可用option:

  • -f
  • -n
  • -u
  • -r-R
  • -L
  • -P
  1. cp('file1', 'dir1');
  2. cp('-R', 'path/to/dir/', '~/newCopy/');
  3. cp('-Rf', '/tmp/*', '/usr/local/*', '/home/tmp');
  4. cp('-Rf', ['/tmp/*', '/usr/local/*'], '/home/tmp'); // same as above

pushd

pushd([options,] [dir | '-N' | '+N'])

可用选项:

  • -n
  • -q

参数:

  • dir
  • +N
  • -N
  1. // process.cwd() === '/usr'
  2. pushd('/etc'); // Returns /etc /usr
  3. pushd('+1'); // Returns /usr /etc

将当前目录放入最顶层堆栈,并进入dir目录。
如果没有参数,pushd交换最顶层的两个目录,并返回堆栈中队列。

popd

popd([options,] ['-N' | '+N'])

options:

  • -n
  • -q
  1. echo(process.cwd()); // '/usr'
  2. pushd('/etc'); // '/etc /usr'
  3. echo(process.cwd()); // '/etc'
  4. popd(); // '/usr'
  5. echo(process.cwd()); // '/usr'

dirs

dirs([options | '+N' | '-N'])

options:

  • -c
  • -q

echo

echo([options,] string [, string ...])

options:

  • -e
  • -n
  1. echo('hello world');
  2. var str = echo('hello world');
  3. echo('-n', 'no newline at end');

exec

exec(command [, options] [, callback])

options:

  • async:异步执行。如果设置了callback,该选项将被设置为true,默认为false
  • silent:不进行回显,默认为false
  • encoding:使用字符编码。影响返回到 stdout 和 stderr 的值,以及在不处于静默模式时写入到 stdout 和 stderr 的内容(默认值: ‘ utf8’)。
  • 以及nodejs中child_process.exec()所有可用的选项
  1. var version = exec('node --version', {silent:true}).stdout;
  2. var child = exec('some_long_running_process', {async:true});
  3. child.stdout.on('data', function(data) {
  4. /* ... do something with data ... */
  5. });
  6. exec('some_long_running_process', function(code, stdout, stderr) {
  7. console.log('Exit code:', code);
  8. console.log('Program output:', stdout);
  9. console.log('Program stderr:', stderr);
  10. });

find

find(path [, path ...])
find(path_array)

  1. find('src', 'lib');
  2. find(['src', 'lib']); // same as above
  3. find('.').filter(function(file) { return file.match(/\.js$/); });

grep

grep([options,] regex_filter, file [, file ...])
grep([options,] regex_filter, file_array)

options:

  • -v
  • -l
  • -i
  1. grep('-v', 'GLOBAL_VARIABLE', '*.js');
  2. grep('GLOBAL_VARIABLE', '*.js');