shell 脚本和 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5705361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:40:51  来源:igfitidea点击:

shell scripting and jQuery

jqueryajaxshell

提问by user478636

I want to execute a linux command that returns the currently logged on user in linux.

我想执行一个 linux 命令,该命令返回 linux 中当前登录的用户。

Using jQuery and ajax, passing this command to the PHP, which will receive and execute this command using exec()function. The value should be stored and returned.

使用 jQuery 和 ajax,将此命令传递给 PHP,PHP 将使用exec()函数接收并执行此命令。该值应该被存储和返回。

Upon returning, jquery ajax should return it to a contents of a span tag.

返回时,jquery ajax 应将其返回到 span 标记的内容。

How can this be achieved?

如何做到这一点?

回答by Matt Ball

Use .load(). Something like this:

使用.load(). 像这样的东西:

JavaScript

JavaScript

$('#my-span-id').load('/path/to/page.php');

PHP

PHP

<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');
?>

回答by Juanma

This javascriptsample run a tailcommand using node.js

javascript示例tail使用以下命令运行命令node.js

#!/usr/bin/env node

var readline = require('readline');
var cp = require('child_process');
var tail = cp.spawn('tail', ['-500', 'mylogfile.log']);
var lineReader = readline.createInterface(tail.stdout, tail.stdin);

lineReader.on('line', function(line) {
    console.log('Line: ' + line);
});

tail.on('close', function(code, signal) {
    console.log('ls finished...');
});

Reference Tutorial

参考教程