Initial commit
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+60
@@ -0,0 +1,60 @@
|
||||
shell-escape
|
||||
============
|
||||
|
||||
Escape and stringify an array of arguments to be executed on the shell
|
||||
|
||||
Install
|
||||
-------
|
||||
|
||||
npm install shell-escape
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
### simple
|
||||
|
||||
``` js
|
||||
var shellescape = require('shell-escape');
|
||||
|
||||
var args = ['curl', '-v', '-H', 'Location;', '-H', 'User-Agent: dave#10', 'http://www.daveeddy.com/?name=dave&age=24'];
|
||||
|
||||
var escaped = shellescape(args);
|
||||
console.log(escaped);
|
||||
```
|
||||
|
||||
yields
|
||||
|
||||
```
|
||||
curl -v -H 'Location;' -H 'User-Agent: dave#10' 'http://www.daveeddy.com/?name=dave&age=24'
|
||||
```
|
||||
|
||||
A command suitable for being executed by the shell
|
||||
|
||||
### advanced
|
||||
|
||||
``` js
|
||||
var shellescape = require('shell-escape');
|
||||
|
||||
var args = ['echo', 'hello!', 'how are you doing $USER', '"double"', "'single'"];
|
||||
|
||||
var escaped = shellescape(args);
|
||||
console.log(escaped);
|
||||
```
|
||||
|
||||
yields
|
||||
|
||||
```
|
||||
echo 'hello!' 'how are you doing $USER' '"double"' \''single'\'
|
||||
```
|
||||
|
||||
and when run on the shell
|
||||
|
||||
```
|
||||
$ echo 'hello!' 'how are you doing $USER' '"double"' \''single'\'
|
||||
hello! how are you doing $USER "double" 'single'
|
||||
```
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
MIT
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "shell-escape",
|
||||
"version": "0.2.0",
|
||||
"description": "Escape and stringify an array of arguments to be executed on the shell",
|
||||
"author": [
|
||||
"Martin PANEL <martin@xorax.info> (http://xorax.info)",
|
||||
"Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)"
|
||||
],
|
||||
"main": "./shell-escape.js",
|
||||
"scripts": {
|
||||
"test": "for f in test/*; do echo \"$f\"; node \"$f\" || exit 1; echo; done; echo Passed; exit 0"
|
||||
},
|
||||
"repository": "git://github.com/xxorax/node-shell-escape.git",
|
||||
"license": "MIT",
|
||||
"dependencies": {},
|
||||
"keywords": [
|
||||
"shell",
|
||||
"escape",
|
||||
"bash",
|
||||
"escapeshellarg"
|
||||
]
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
module.exports = shellescape;
|
||||
|
||||
// return a shell compatible format
|
||||
function shellescape(a) {
|
||||
var ret = [];
|
||||
|
||||
a.forEach(function(s) {
|
||||
if (!/^[A-Za-z0-9_\/-]+$/.test(s)) {
|
||||
s = "'"+s.replace(/'/g,"'\\''")+"'";
|
||||
s = s.replace(/^(?:'')+/g, '') // unduplicate single-quote at the beginning
|
||||
.replace(/\\'''/g, "\\'" ); // remove non-escaped single-quote if there are enclosed between 2 escaped
|
||||
}
|
||||
ret.push(s);
|
||||
});
|
||||
|
||||
return ret.join(' ');
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
var shellescape = require('../');
|
||||
|
||||
var assert = require('assert');
|
||||
|
||||
var args = ['echo', 'hello!', 'how are you doing $USER', '"double"', "'single'"];
|
||||
|
||||
var escaped = shellescape(args);
|
||||
assert.strictEqual(escaped, "echo 'hello!' 'how are you doing $USER' '\"double\"' \\''single'\\'");
|
||||
console.log(escaped);
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
var shellescape = require('../');
|
||||
|
||||
var assert = require('assert');
|
||||
|
||||
var d = {
|
||||
"echo 'hello\\nworld'": ['echo', 'hello\\nworld'],
|
||||
"echo 'hello\\tworld'": ['echo', 'hello\\tworld'],
|
||||
"echo '\thello\nworld'\\'": ['echo', '\thello\nworld\''],
|
||||
"echo 'hello world'": ['echo', 'hello world'],
|
||||
"echo hello world": ['echo', 'hello', 'world'],
|
||||
"echo 'hello\\\\'\\' \\''\\\\'\\''world'": ["echo", "hello\\\\'", "'\\\\'world"],
|
||||
"echo hello 'world\\'": ["echo", "hello", "world\\"]
|
||||
};
|
||||
|
||||
Object.keys(d).forEach(function(s) {
|
||||
var escaped = shellescape(d[s]);
|
||||
assert.strictEqual(escaped, s);
|
||||
console.log(s);
|
||||
});
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
var shellescape = require('../');
|
||||
|
||||
var assert = require('assert');
|
||||
|
||||
var args = ['curl', '-v', '-H', 'Location;', '-H', 'User-Agent: dave#10', 'http://www.daveeddy.com/?name=dave&age=24'];
|
||||
|
||||
var escaped = shellescape(args);
|
||||
|
||||
assert.strictEqual(escaped, "curl -v -H 'Location;' -H 'User-Agent: dave#10' 'http://www.daveeddy.com/?name=dave&age=24'");
|
||||
console.log(escaped);
|
||||
Reference in New Issue
Block a user