您如何使用"system()"执行bash脚本?文件路径中何时有空格? [英] How do you execute a bash script with "system()" when there are spaces in the file path?

查看:35
本文介绍了您如何使用"system()"执行bash脚本?文件路径中何时有空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的bash脚本,名为"myscript.h",我给了它一个.h扩展名,原因是我在这里不作介绍.这个bash脚本位于"/var/ftp/有空格的东西"中.

I created a simple bash script called "myscript.h" I gave it a .h extensions for reasons that I won't disclose here. This bash script lives in "/var/ftp/something with spaces".

在终端上,我可以输入"/var/ftp/somespaces/myscript.h",该脚本可以正常运行.

From the terminal, I can type in "/var/ftp/something with spaces/myscript.h" and the script works perfectly.

但是,在我的C程序中,我输入

However, from within my C program, I type in

system("/var/ftp/something with spaces/myscript.h")

,它抱怨找不到"/var/ftp/something".我已将系统调用更改为以下带有正斜杠的内容:

and it complains that "/var/ftp/something" is not found. I've changed my system call to the following with forward slashes:

system("/var/ftp/something\ with\ spaces/myscript.h")

但是,它仍然抱怨找不到"/var/ftp/something".假设我无法更改目录名称,该如何解决?

However, it still complains that "/var/ftp/something" is not found. Assuming I can't change the directory names, how can I get around this?

谢谢!

推荐答案

要运行单个脚本,您可以避免使用 waitpid(2) ...,由 system(3)的实现使用),或者将脚本名称传递给

To run a single script, you might avoid the system(3) library function (and use lower level system calls like fork(2), execve(2), waitpid(2)... which are used by the implementation of system(3)), or you could quote the script name when passing it to system(3).

有关更多详细信息,请阅读 高级Linux编程 .

For more details, read Advanced Linux Programming.

在Linux上, system(3)被记录为派生/bin/sh -c 进程.参见 sh(1p).并且该POSIX外壳具有一些引用规则.您可以适当地使用双引号和反斜杠.因此,您可以构造(并检查)传递给 system(3)的字符串(也许使用 asprintf(3)或<带有 care 的a href ="http://man7.org/linux/man-pages/man3/snprintf.3.html" rel ="noreferrer"> snprintf(3)).请注意,C编译器还具有(不同)字符串文字的引用约定.

On Linux, system(3) is documented to fork a /bin/sh -c process. See sh(1p). And that POSIX shell has some quoting rules. You might use double-quotes and backslashes appropriately. So you would construct (and perhaps check) the string passed to system(3) (perhaps using asprintf(3) or snprintf(3) with care). Be aware that the C compiler also has (different) quoting conventions for string literals.

通常,您应该避免代码注入 (想象一个顽皮的用户提供一些 a; rm -rf $ HOME& 输入作为目录"名称;您不想运行字符串)

In general, you should avoid code injection (imagine a naughty user giving some a; rm -rf $HOME &; input as a "directory" name; you don't want to run system(3) on the weird
"/var/ftp/a; rm -rf $HOME &;/myscript.h" string)

在您的特定情况下,建议您使用 fork(2) execve(2)(也许是经过仔细选择的 exec(3)函数),

In your particular case, I recommend using fork(2), execve(2) (perhaps thru some carefully choosen exec(3) function), waitpid(2)... appropriately. This has the slight advantage to avoid depending on and running /bin/sh so could be slightly faster (by a millisecond).

进一步了解 Unix shell 的作用;例如,在 bash (它们与POSIX sh 所要求的类似),并且要注意 glob(7)

Understand more the role of an Unix shell; for example, read about the various shell expansions in bash (they are similar to those mandated by POSIX sh) and be aware of globbing. See glob(7)

这篇关于您如何使用"system()"执行bash脚本?文件路径中何时有空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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