Unix shell commands for Node.js
可以使shell脚本不依赖于unix环境。
命令行
shelljs/shx
命令行使用的
$ shx mkdir -p foo
$ shx touch foo/bar.txt
$ shx rm -rf foo
实例
var shell = require('shelljs');
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git');
shell.exit(1);
}
// Copy files to release dir
shell.rm('-rf', 'out/Release');
shell.cp('-R', 'stuff/', 'out/Release');
// Replace macros in each .js file
shell.cd('lib');
shell.ls('*.js').forEach(function (file) {
shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file);
shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file);
});
shell.cd('..');
// Run external tool synchronously
if (shell.exec('git commit -am "Auto-commit"').code !== 0) {
shell.echo('Error: Git commit failed');
shell.exit(1);
}
命令
- 除非另外说明,否则所有命令都是同步运行。
- 接受bash的全局字符(
*
、?
等) - 和node global模块兼容
cat
cat([options,] file [, file ...])
cat([options,] file_array)
可用options:
-v
显示所有行的行号
var str = cat('file*.txt');
var str = cat('file1', 'file2');
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
以递归方式更改
chmod(755, '/Users/brandon');
chmod('755', '/Users/brandon'); // same as above
chmod('u+x', '/Users/brandon');
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
cp('file1', 'dir1');
cp('-R', 'path/to/dir/', '~/newCopy/');
cp('-Rf', '/tmp/*', '/usr/local/*', '/home/tmp');
cp('-Rf', ['/tmp/*', '/usr/local/*'], '/home/tmp'); // same as above
pushd
pushd([options,] [dir | '-N' | '+N'])
可用选项:
-n
-q
参数:
dir
:+N
:-N
:
// process.cwd() === '/usr'
pushd('/etc'); // Returns /etc /usr
pushd('+1'); // Returns /usr /etc
将当前目录放入最顶层堆栈,并进入dir目录。
如果没有参数,pushd交换最顶层的两个目录,并返回堆栈中队列。
popd
popd([options,] ['-N' | '+N'])
options:
- -n
- -q
echo(process.cwd()); // '/usr'
pushd('/etc'); // '/etc /usr'
echo(process.cwd()); // '/etc'
popd(); // '/usr'
echo(process.cwd()); // '/usr'
dirs
dirs([options | '+N' | '-N'])
options:
- -c
- -q
echo
echo([options,] string [, string ...])
options:
- -e
- -n
echo('hello world');
var str = echo('hello world');
echo('-n', 'no newline at end');
exec
exec(command [, options] [, callback])
options:
async
:异步执行。如果设置了callback,该选项将被设置为true,默认为falsesilent
:不进行回显,默认为falseencoding
:使用字符编码。影响返回到 stdout 和 stderr 的值,以及在不处于静默模式时写入到 stdout 和 stderr 的内容(默认值: ‘ utf8’)。- 以及nodejs中
child_process.exec()
所有可用的选项
var version = exec('node --version', {silent:true}).stdout;
var child = exec('some_long_running_process', {async:true});
child.stdout.on('data', function(data) {
/* ... do something with data ... */
});
exec('some_long_running_process', function(code, stdout, stderr) {
console.log('Exit code:', code);
console.log('Program output:', stdout);
console.log('Program stderr:', stderr);
});
find
find(path [, path ...])
find(path_array)
find('src', 'lib');
find(['src', 'lib']); // same as above
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
grep('-v', 'GLOBAL_VARIABLE', '*.js');
grep('GLOBAL_VARIABLE', '*.js');