ฉันต้องการเขียนฟังก์ชัน JavaScript ซึ่งจะดำเนินการคำสั่งเชลล์ระบบ (lsตัวอย่าง) และส่งคืนค่า
ฉันจะบรรลุเป้าหมายนี้ได้อย่างไร?
ฉันต้องการเขียนฟังก์ชัน JavaScript ซึ่งจะดำเนินการคำสั่งเชลล์ระบบ (lsตัวอย่าง) และส่งคืนค่า
ฉันจะบรรลุเป้าหมายนี้ได้อย่างไร?
คำตอบ:
คำตอบอื่น ๆ อีกมากมายที่นี่ดูเหมือนจะแก้ไขปัญหานี้จากมุมมองของฟังก์ชัน JavaScript ที่ทำงานในเบราว์เซอร์ ฉันจะยิงและตอบโดยสมมติว่าเมื่อผู้ถามพูดว่า "Shell Script" เขาหมายถึง JavaScript แบ็กเอนด์ Node.js อาจใช้Commander.jsเพื่อใช้เฟรมโค้ดของคุณ :)
คุณสามารถใช้โมดูลchild_processจาก API ของโหนด ฉันวางโค้ดตัวอย่างด้านล่าง
var exec = require('child_process').exec, child;
child = exec('cat *.js bad_file | wc -l',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
child();
หวังว่านี่จะช่วยได้!
... ไม่กี่ปีต่อมา ...
ES6 ได้รับการยอมรับว่าเป็นมาตรฐานและ ES7 อยู่ใกล้ ๆ ดังนั้นจึงสมควรได้รับคำตอบที่อัปเดต เราจะใช้ ES6 + async / await กับ nodejs + babel เป็นตัวอย่างข้อกำหนดเบื้องต้นคือ:
foo.jsไฟล์ตัวอย่างของคุณอาจมีลักษณะดังนี้:
import { exec } from 'child_process';
/**
* Execute simple shell command (async wrapper).
* @param {String} cmd
* @return {Object} { stdout: String, stderr: String }
*/
async function sh(cmd) {
return new Promise(function (resolve, reject) {
exec(cmd, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
}
});
});
}
async function main() {
let { stdout } = await sh('ls');
for (let line of stdout.split('\n')) {
console.log(`ls: ${line}`);
}
}
main();
ตรวจสอบให้แน่ใจว่าคุณมี Babel:
npm i babel-cli -g
ติดตั้งค่าที่ตั้งไว้ล่าสุด:
npm i babel-preset-latest
เรียกใช้ผ่าน:
babel-node --presets latest foo.js
ฉันไม่รู้ว่าทำไมคำตอบก่อนหน้านี้จึงให้คำตอบที่ซับซ้อนทุกประเภท หากคุณต้องการดำเนินการคำสั่งด่วนเช่นlsคุณไม่จำเป็นต้อง async / await หรือ callbacks หรืออะไรเลย นี่คือทั้งหมดที่คุณต้องการ - execSync :
const execSync = require('child_process').execSync;
// import { execSync } from 'child_process'; // replace ^ if using ES modules
const output = execSync('ls', { encoding: 'utf-8' }); // the default is 'buffer'
console.log('Output was:\n', output);
สำหรับการจัดการข้อผิดพลาดให้เพิ่มtry/catchบล็อกรอบ ๆ คำสั่ง
หากคุณกำลังเรียกใช้คำสั่งที่ใช้เวลาดำเนินการนานใช่ให้ดูที่ฟังก์ชันอะซิงโครนัสexec
สิ่งนี้ขึ้นอยู่กับสภาพแวดล้อม JavaScript ทั้งหมด กรุณาอธิบายให้ละเอียด
ตัวอย่างเช่นใน Windows Scripting คุณทำสิ่งต่างๆเช่น:
var shell = WScript.CreateObject("WScript.Shell");
shell.Run("command here");
โดยสังเขป:
// Instantiate the Shell object and invoke its execute method.
var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\\Winnt\\Notepad.exe";
if (inputparms != "") {
var commandParms = document.Form1.filename.value;
}
// Invoke the execute method.
oShell.ShellExecute(commandtoRun, commandParms, "", "open", "1");
ด้วย NodeJS ก็ง่ายเช่นนั้น! และหากคุณต้องการเรียกใช้สคริปต์นี้ในการบูตเซิร์ฟเวอร์แต่ละครั้งคุณสามารถดูแอปพลิเคชันบริการตลอดไปได้ !
var exec = require('child_process').exec;
exec('php main.php', function (error, stdOut, stdErr) {
// do what you want!
});
หมายเหตุ: คำตอบเหล่านี้มาจากไคลเอนต์ที่ใช้เบราว์เซอร์ไปจนถึงเว็บเซิร์ฟเวอร์ที่ใช้ Unix
รันคำสั่งบนไคลเอนต์
โดยพื้นฐานแล้วคุณทำไม่ได้ ความปลอดภัยระบุว่าทำงานภายในเบราว์เซอร์เท่านั้นและการเข้าถึงคำสั่งและระบบไฟล์มี จำกัด
รัน ls บนเซิร์ฟเวอร์
คุณสามารถใช้การเรียก AJAX เพื่อดึงเพจไดนามิกที่ส่งผ่านพารามิเตอร์ของคุณผ่าน GET
โปรดทราบว่านี่เป็นการเปิดความเสี่ยงด้านความปลอดภัยเช่นกันเนื่องจากคุณต้องทำบางอย่างเพื่อให้แน่ใจว่าแฮ็กเกอร์ mrs rouge ไม่ได้รับแอปพลิเคชันของคุณที่จะบอกว่า run: / dev / null && rm -rf / ......
ดังนั้นในช่วงเวลาสั้น ๆ การวิ่งจาก JS เป็นเพียงความคิดที่ไม่ดีและไม่ดี .... YMMV
โพสต์อื่นในหัวข้อนี้พร้อมโซลูชัน jQuery / Ajax / PHP ที่ดี:
ใน IE คุณสามารถทำได้:
var shell = new ActiveXObject("WScript.Shell");
shell.run("cmd /c dir & pause");
นี่คือคำสั่งง่ายๆที่รันifconfigคำสั่งเชลล์ของ Linux
var process = require('child_process');
process.exec('ifconfig',function (err,stdout,stderr) {
if (err) {
console.log("\n"+stderr);
} else {
console.log(stdout);
}
});
function exec(cmd, handler = function(error, stdout, stderr){console.log(stdout);if(error !== null){console.log(stderr)}})
{
const childfork = require('child_process');
return childfork.exec(cmd, handler);
}
ฟังก์ชันนี้สามารถใช้งานได้ง่ายเช่น:
exec('echo test');
//output:
//test
exec('echo test', function(err, stdout){console.log(stdout+stdout+stdout)});
//output:
//testtesttest
ด้วยnashornคุณสามารถเขียนสคริปต์ดังนี้:
$EXEC('find -type f');
var files = $OUT.split('\n');
files.forEach(...
...
และเรียกใช้:
jjs -scripting each_file.js
หากคุณใช้ npm คุณสามารถใช้แพ็คเกจ shelljs
ติดตั้ง: npm install [-g] shelljs
var shell = require('shelljs');
shell.ls('*.js').forEach(function (file) {
// do something
});
ดูเพิ่มเติม: https://www.npmjs.com/package/shelljs