Building a string is easy, but building a Cmd is always a headache to me. So I write a small package to run strings as Cmd.
Here is the task that drives me to write the package: There are some sub-folders containing .png files. I want to convert them into videos and place these videos at the root folder. And here is the code
julia> using Shell
julia> for (r,d,f) in walkdir(".")
if any(x->endswith(x, ".png"), f)
run("convert $r/*.png $(basename(r)).mp4")
end
end
It is more intuitive than dealing Cmd objects (In fact, I still not able to write the correct Cmd for this task…). Hope this will help someone.
Note that doing this sort of thing has all of the problems that @StefanKarpinski described in his Shelling Out Sucks blog post. It will have unexpected results if basename(r) contains spaces, for example. (Maybe this is what @yuyichao was referring to with “buggy code”.) That’s why it would be better to learn to use Cmd objects.
I updated the code to address the problems mentioned in the comments. One is type piracy, and the other is the escaping of spaces. It might be buggy for complex jobs, but it is handy to run simple commands at my skill level.
After writting these codes, I start to wonder, maybe the default way (Cmd and Pipelines) is more concise…