我可以从popen()完成流打开的bash? [英] Can I open bash from a popen() stream?

查看:276
本文介绍了我可以从popen()完成流打开的bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据该男子页的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?

推荐答案

如果你想在你传递给的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.

这篇关于我可以从popen()完成流打开的bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆