• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

我可以从popen完成流打开的bash

用户头像
it1352
帮助1

问题说明

据该男子页的popen()我打开一个/bin/sh...Is有办法,我可以重载这种行为开/斌/ bash shell,然后用Bash shell脚本进行交互?或者我需要打开一个PTY式接口做到这一点?

According to the man page for popen() I am opening a /bin/sh...Is there a way I can overload this behavior to open a /bin/bash shell and interact with BASH shell scripts? Or do I need to open a pty style connection to do that?

正确答案

#1

如果你想在你传递给的popen 片段使用bash结构,可以调用的bash明确从SH。

If you want to use bash constructs in the snippet you pass to popen, you can call bash explicitly from sh.

f = popen("exec bash -c 'shopt -s dotglob; cat -- *'", "r");

如果您需要的是bash里面的片段单引号,将它们作为'\\''。所有其他字符字面过去了。

If you need single quotes inside the bash snippet, pass them as '\''. All other characters are passed literally.

如果您有想要运行现有的bash脚本,只需指定其路径,与任何脚本或其他程序。确保脚本是可执行文件和#开始!的/ usr / bin中/ env的庆典#!/斌/庆典。如果你有一个脚本,可能是非可执行文件(例如,由于便携性的Windows约束),则可以显式调用庆典:

If you have an existing bash script that you want to run, simply specify its path, same as any script or other program. Make sure that the script is executable and begins with #!/usr/bin/env bash or #!/bin/bash. If you have a script that may be non-executable (for example because of Windows portability constraints), you can invoke bash explicitly:

f = popen("exec bash myscript.bash", "r");

另外,编写自定义的的popen 样函数来调用你想要的(但这样做,只有当简单的解决方案是不理想的方案,因为它更难以得到对)。例如参见的popen()替代

Alternatively, write your own custom popen-like function that calls the program you want (but do that only if simpler solutions are not satisfactory, because it's more difficult to get right). See e.g. popen() alternative.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhchahhe
系列文章
更多 icon
同类精品
更多 icon
继续加载