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}");
}
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.
You are right. In RHEL, sh -c 'echo {1..4}' 1 2 3 4
In Ubuntu sh -c 'echo {1..4}' {1..4}
So I use the program below instead. It works! #include
int main()
{
std::system("bash -c 'mkdir {1..4}'");
}
seems system use sh by default....Thank you for your answer!
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).
You don't need to use system(3) (which is running
sh
notbash
!). And POSIXsh
don't know the{1..4}
notation, hence the{1..4}
string is passed verbatim to/bin/mkdir
command (see mkdir(1) ...).Run
to test that
sh
don't understand the{1..4}
notation.(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
)Just use the mkdir(2) syscall and code
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.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 ...
Read Advanced Linux Programming...
Using the
mkdir(2)
syscall instead of going thru a command invoked bysystem(3)
has several important advantages:/bin/sh -c
shell likesystem(3)
should do.fork
-ed, so your program will still run when you have reached your limits (see setrlimit(2) ...)mkdir(2)
fail you could (and should) handle the failure nicely. See errno(3) and strerror(3) ....