在程序中执行系统命令并获得结果

Contents

输出结果

Current time like this

Mon Dec  9 07:08:08 JST 2019

程式码


JavaScript

const execSync = require('child_process').execSync;
let result = execSync('date').toString();
console.log(result);

PHP

<?php
exec('date', $rtn);
print($rtn[0]."\n");

Perl

my $result = `date`;
print($result."\n");

Python

import subprocess
result = subprocess.check_output(["date"], shell=True).decode("UTF-8")
print(result)

Ruby

result = `date`
print(result)

Shell

#!/bin/sh
RESULT=`date`;
echo $RESULT;