简体   繁体   English

在Linux下,使用c ++中的system()执行命令的转义序列

[英]escape sequences for executing a command with system() in c++, under linux

I want to execute a command using system() . 我想使用system()执行命令。 The command needs a file name as a parameter. 该命令需要文件名作为参数。 Something like this: 像这样:

void f(string filename){
  strnig cmd = "path/to/my/command " + filename; 
  system(cmd.c_str());
  ... 
}

My question is what escape sequences could I use so that the string received in filename will be considered just the name of the file and nothing more. 我的问题是我可以使用什么转义序列,以便将在filename中接收的字符串仅视为文件名,仅此而已。 For example, if filename = "aname;rm *" , I don't want my command to be executed on "aname" and then "rm *" to be considered a new command and to delete everything from my current directory. 例如,如果filename = "aname;rm *" ,我不希望我的命令在"aname"上执行,然后不希望在"rm *"上执行新命令并删除当前目录中的所有内容。

Also, I wish that my filename to be able to contain special characters like: ' , " or space, without affecting the command execution. 另外,我希望我的文件名能够包含特殊字符,例如: '"或空格,而又不影响命令执行。

How do I do this in c++? 我该如何在C ++中做到这一点?

Thank you! 谢谢!

man system: 人系统:

system() executes a command specified in command by calling /bin/sh -c command system()通过调用/ bin / sh -c命令执行命令中指定的命令

so use the escape sequences your shell honors. 因此,请使用您的shell荣誉使用的转义序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM