为什么mkdir的行为在不同的Linux发行版上有所不同? [英] Why does the behavior of mkdir differs on different Linux distros?
问题描述
我试图创建名为1 2 3 4的文件夹,使用下面的C ++程序。我可以在 RHEL 中成功实现。
但是,在 ubuntu 13.10 中创建了一个名为{1..4}的文件夹。
I was trying to create folders named 1 2 3 4, using the C++ program below. I can successfully achieve that in RHEL. However it created a folder named {1..4} in ubuntu 13.10.
为什么会发生这种情况?谢谢您帮助我!
Why does this happen? Thank you for helping me!
#include <cstdlib>
int main()
{
std::system("mkdir {1..4}");
}
这是我们产品中CPP单元测试的一部分。是的,它的丑陋。但我恐怕在这种情况下可以做的事很少。
It's a part of CPP unit test in our product. Yes, it's ugly. But I am afraid very few thing can be done in this situation.
你是对的。
在RHEL中,
sh -c'echo {1..4}'
1 2 3 4
You are right. In RHEL, sh -c 'echo {1..4}' 1 2 3 4
在Ubuntu
sh -c'echo {1..4}'
{1..4}
In Ubuntu sh -c 'echo {1..4}' {1..4}
所以我使用下面的程序。有用!
#include
So I use the program below instead. It works! #include
int main()
{
std::system("bash -c 'mkdir {1..4}'");
}
似乎系统默认使用sh ....谢谢你的回答!
seems system use sh by default....Thank you for your answer!
推荐答案
有一点术语:Linux有文件系统中的%28个计算%29rel =nofollow>目录,不是文件夹(文件夹可以在桌面上可视化显示,但这是桌面细节)。
A bit of terminology: Linux has directories in its file systems, not "folders" (folders may appear visually on the desktop, but that is a desktop detail).
您不需要使用 system(3)(正在运行 sh
不 bash
!)。
和POSIX sh
不知道 {1..4}
符号,因此 {1..4}
字符串传递到 / bin / mkdir
命令a href =http://man7.org/linux/man-pages/man1/mkdir.1.html =nofollow> mkdir(1) ...)。
You don't need to use system(3) (which is running sh
not bash
!).
And POSIX sh
don't know the {1..4}
notation, hence the {1..4}
string is passed verbatim to /bin/mkdir
command (see mkdir(1) ...).
执行
sh -c 'echo {1..4}'
来测试 sh
c $ c> {1..4} 符号。
to test that sh
don't understand the {1..4}
notation.
(所以这是一个在旧RHEL中的错误, $ c> / bin / sh 是 / bin / bash
的符号链接,而在Debian和Ubuntu上,它是符合Posix标准的符号链接,更快 / bin / dash
)
(so it is a bug in your old RHEL, where perhaps /bin/sh
is a symlink to /bin/bash
while on Debian and Ubuntu it is a symlink to the more Posix compliant and faster /bin/dash
)
只需使用 mkdir(2)系统调用和代码
Just use the mkdir(2) syscall and code
#include <cstdlib>
#include <cstdio>
#include <sys/stat.h>
#include <sys/types.h>
int main() {
for (int i=1; i<=4; i++) {
char buf[8];
snprintf(buf, sizeof(buf), "%d", i);
if (mkdir(buf, 0755))
{ perror("mkdir"); exit(EXIT_FAILURE); };
}
}
我希望你不想创建单个目录 1 2 3 4
。这是可能和容易,但它真的是味道差。为了您的心理安全,在目录名称中只能使用字母,数字和下划线 _
。
I hope you don't want to create a single directory named 1 2 3 4
. It is possible and easy, but it really is poor taste. For your mental safety, use only letters, digits and underscores _
in directory names.
href =http://man7.org/linux/man-pages/man3/snprintf.3.html =nofollow> snprintf(3)将int转换为字符缓冲区。使用 C ++ 11 ,您可以使用 std :: to_string 和 c_str ...
I am using snprintf(3) to convert an int to a character buffer. With C++11 you could use std::to_string and c_str ...
阅读高级Linux编程 ...
使用 mkdir(2)
syscall而不是通过 system(3)
调用的命令有几个重要的优点:
Using the mkdir(2)
syscall instead of going thru a command invoked by system(3)
has several important advantages:
- 更快,您不需要 fork(2)一个
/ bin / sh -c
shell如system(3)
应该这样做。 - 它使用更少的资源,因为没有额外的进程是
fork
,因此您的程序将仍然运行,当你达到你的极限(见 setrlimit(2) ...) - 它更可靠。应该
mkdir(2)
失败,你可以(而且应该)很好地处理故障。请参见 errno(3)和 strerror(3) ....
- it is much faster, you don't need to fork(2) a
/bin/sh -c
shell likesystem(3)
should do. - it uses much less resources, since no additional process is
fork
-ed, so your program will still run when you have reached your limits (see setrlimit(2) ...) - it is more reliable. Should
mkdir(2)
fail you could (and should) handle the failure nicely. See errno(3) and strerror(3) ....
这篇关于为什么mkdir的行为在不同的Linux发行版上有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!