整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

各编程语言调用Shell方法

各编程语言调用Shell方法

avascript


语法:

child_process.exec(command[, options][, callback])

例子:

hell脚本是一个命令语言,面向的是操作系统执行。如果写过shell脚本的话,应该体会过编写过程的痛苦。因为shell并不是一个编程语言,并不支持常见的数组,JSON等数据结构,也不支持面向对象编程的开发方法,因此对开发人员很不友好。

目前针对这种情况,大家一般会用shell调用node执行JS脚本,真正的处理逻辑放在JS脚本中处理。现在谷歌推出了 ZX NPM包,它能够用JS编写shell脚本。


那如何使用呢?


1.全局安装zx

npm install -g zx

安装完后,在终端中输入 zx 命令检查安装是否成功。

2.创建一个简单的脚本

新建zx脚本文件:test.mjs

#!/usr/bin/env zx
const branch=await $`git branch --show-current`
console.log(`Current branch: ${branch}`)

第一行是指定脚本的执行器。

$ 是内置的函数,能够执行命令并配合 await 返回执行结果。其他的写法都和JS毫无差别。

3.执行脚本

zx ./test.mjs

或者:

chmod +x ./test.mjs
./test.mjs

控制台就会输出当前的分支。

上面只是小试牛刀,zx 的强大远不止如此。由于 zx 在内部实现了 Bash 的解释器,所以可以执行全部的shell命令。另外 zx 还内置很多nodejs模块,比如 fs, os,fetch等。所以可以直接在脚本中使用这些模块。

另外作为TS编写的库,全部的JS语法都能够支持。包括但不限于 数组,Promise,class等。

下面再举一个例子:

let resp=await fetch('http://wttr.in')
if (resp.ok) {
  console.log(await resp.text())
}

let hosts=[...]
await Promise.all(hosts.map(host=>
  $`rsync -azP ./src ${host}:/var/www`  
))

try {
  await $`exit 1`
} catch (p) {
  console.log(`Exit code: ${p.exitCode}`)
  console.log(`Error: ${p.stderr}`)
}


总结一下,zx 的最大优点是结合了Bash和JavaScript,解决了shell脚本复杂逻辑编程的问题。同时也让对shell不熟悉的开发者也能用JS完成shell脚本的开发,而且更加灵活高效。


如果你还有更多问题,可以参考NPM仓库 zx 包的介绍,或者访问其github地址。

欢迎帮忙点赞,评论,转发~

. os.system()

help(os.system)

1.1. demo

#os.system(command):该方法在调用完shell脚本后,返回一个16位的二进制数,
#低位为杀死所调用脚本的信号号码,高位为脚本的退出状态码,
#即脚本中exit 1的代码执行后,os.system函数返回值的高位数则是1,如果低位数是0的情况下,
#则函数的返回值是0x0100,换算为十进制得到256。
#要获得os.system的正确返回值,可以使用位移运算(将返回值右移8位)还原返回值:
>>> import os
>>> os.system("./test.sh")
hello python!
hello world!
256
>>> n>>8
1

2. os.popen()

help(os.system)

2.1 demo

#os.popen(command):这种调用方式是通过管道的方式来实现,函数返回一个file对象,
#里面的内容是脚本输出的内容(可简单理解为echo输出的内容),使用os.popen调用test.sh的情况

>> import os
>>> os.popen("./test.sh")
<open file './test.sh', mode 'r' at 0x7f6cbbbee4b0>
>>> f=os.popen("./test.sh")
>>> f
<open file './test.sh', mode 'r' at 0x7f6cbbbee540>
>>> f.readlines()
['hello python!\n', 'hello world!\n']

3. commands模块

(1)commands.getstatusoutput(cmd),其以字符串的形式返回的是输出结果和状态码,即(status,output)。

(2)commands.getoutput(cmd),返回cmd的输出结果。

(3)commands.getstatus(file),返回ls -l file的执行结果字符串,调用了getoutput,不建议使用此方法

4. subprocess

subprocess模块,允许创建很多子进程,创建的时候能指定子进程和子进程的输入、输出、错误输出管道,执行后能获取输出结果和执行状态。

(1)subprocess.run():python3.5中新增的函数, 执行指定的命令, 等待命令执行完成后返回一个包含执行结果的CompletedProcess类的实例。

(2)subprocess.call():执行指定的命令, 返回命令执行状态, 功能类似os.system(cmd)。

(3)subprocess.check_call():python2.5中新增的函数, 执行指定的命令, 如果执行成功则返回状态码, 否则抛出异常。

说明:subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, universal_newlines=False)

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

args:表示shell指令,若以字符串形式给出shell指令,如"ls -l “则需要使shell=Ture。否则默认已数组形式表示shell变量,如"ls”,"-l"。

当使用比较复杂的shell语句时,可以先使用shlex模块的shlex.split()方法来帮助格式化命令,然后在传递给run()方法或Popen。

4.1 demo

# Stubs for subprocess

# Based on http://docs.python.org/2/library/subprocess.html and Python 3 stub

from typing import Sequence, Any, Mapping, Callable, Tuple, IO, Union, Optional, List, Text

_FILE=Union[None, int, IO[Any]]
_TXT=Union[bytes, Text]
_CMD=Union[_TXT, Sequence[_TXT]]
_ENV=Union[Mapping[bytes, _TXT], Mapping[Text, _TXT]]

# Same args as Popen.__init__
def call(args: _CMD,
     bufsize: int=...,
     executable: _TXT=...,
     stdin: _FILE=...,
     stdout: _FILE=...,
     stderr: _FILE=...,
     preexec_fn: Callable[[], Any]=...,
     close_fds: bool=...,
     shell: bool=...,
     cwd: _TXT=...,
     env: _ENV=...,
     universal_newlines: bool=...,
     startupinfo: Any=...,
     creationflags: int=...) -> int: ...

def check_call(args: _CMD,
        bufsize: int=...,
        executable: _TXT=...,
        stdin: _FILE=...,
        stdout: _FILE=...,
        stderr: _FILE=...,
        preexec_fn: Callable[[], Any]=...,
        close_fds: bool=...,
        shell: bool=...,
        cwd: _TXT=...,
        env: _ENV=...,
        universal_newlines: bool=...,
        startupinfo: Any=...,
        creationflags: int=...) -> int: ...

# Same args as Popen.__init__ except for stdout
def check_output(args: _CMD,
         bufsize: int=...,
         executable: _TXT=...,
         stdin: _FILE=...,
         stderr: _FILE=...,
         preexec_fn: Callable[[], Any]=...,
         close_fds: bool=...,
         shell: bool=...,
         cwd: _TXT=...,
         env: _ENV=...,
         universal_newlines: bool=...,
         startupinfo: Any=...,
         creationflags: int=...) -> bytes: ...

PIPE=... # type: int
STDOUT=... # type: int

class CalledProcessError(Exception):
  returncode=0
  # morally: _CMD
  cmd=... # type: Any
  # morally: Optional[bytes]
  output=... # type: Any

  def __init__(self,
         returncode: int,
         cmd: _CMD,
         output: Optional[bytes]=...) -> None: ...

class Popen:
  stdin=... # type: Optional[IO[Any]]
  stdout=... # type: Optional[IO[Any]]
  stderr=... # type: Optional[IO[Any]]
  pid=0
  returncode=0

  def __init__(self,
         args: _CMD,
         bufsize: int=...,
         executable: Optional[_TXT]=...,
         stdin: Optional[_FILE]=...,
         stdout: Optional[_FILE]=...,
         stderr: Optional[_FILE]=...,
         preexec_fn: Optional[Callable[[], Any]]=...,
         close_fds: bool=...,
         shell: bool=...,
         cwd: Optional[_TXT]=...,
         env: Optional[_ENV]=...,
         universal_newlines: bool=...,
         startupinfo: Optional[Any]=...,
         creationflags: int=...) -> None: ...

  def poll(self) -> int: ...
  def wait(self) -> int: ...
  # morally: -> Tuple[Optional[bytes], Optional[bytes]]
  def communicate(self, input: Optional[_TXT]=...) -> Tuple[Any, Any]: ...
  def send_signal(self, signal: int) -> None: ...
  def terminate(self) -> None: ...
  def kill(self) -> None: ...
  def __enter__(self) -> 'Popen': ...
  def __exit__(self, type, value, traceback) -> bool: ...

# Windows-only: STARTUPINFO etc.

STD_INPUT_HANDLE=... # type: Any
STD_OUTPUT_HANDLE=... # type: Any
STD_ERROR_HANDLE=... # type: Any
SW_HIDE=... # type: Any
STARTF_USESTDHANDLES=... # type: Any
STARTF_USESHOWWINDOW=... # type: Any
CREATE_NEW_CONSOLE=... # type: Any
CREATE_NEW_PROCESS_GROUP=... # type: Any

5. 参考文献

https://www.jb51.net/article/186301.htm