首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >执行两个shell命令并为第一个命令传递参数

执行两个shell命令并为第一个命令传递参数
EN

Stack Overflow用户
提问于 2021-02-21 15:48:52
回答 1查看 31关注 0票数 0

我想查一查IP列表,然后查grep country

我有:

代码语言:javascript
复制
const { spawn } = require('child_process')

const ip_list = [
  '192.168.1.1',
  '192.168.1.2',
  '192.168.1.3'
]

const process = spawn('whois', ip_list)
process.stdout.on('data', (data) => {
  console.log(data)
})

,这相当于运行whois ip1 ip2 ip3 ...

但我想要的相当于运行whois ip1 ip2 ip3 ... | grep country

在Node怎么做?

我尝试在ip_list的末尾添加"grep country“或仅仅”grep country“作为args,但这给了我一个查询错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-21 16:32:11

好的,所以我必须将ip_list转换成所有in的字符串,用空格分隔,然后使用Using a pipe character | with child_process spawn中提到的方法。

代码语言:javascript
复制
const str1 = ip_list.join(' ')
const process = spawn('sh', ['-c', 'whois ' + str1 + ' | grep country'])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66304109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档