running multiple commands

338 views
Skip to first unread message

Davide Lasagna

unread,
Nov 7, 2014, 3:03:52 PM11/7/14
to julia...@googlegroups.com
Hi,

just wondering why I cannot chain these kind of multiple commands in julia.

Example: the command run(`mkdir $tmp && touch $file`) creates the directories $tmp, && touch and $file, while I only want the second part after && to run if first command is successful. Similarly if I use the ";" to create a sequence of commands, e.g. run(`mkdir $tmp; touch $file`).

I could run multiple commands separately, and do the checks in julia, but there might be an easier way to achieve that.

Davide




Stefan Karpinski

unread,
Nov 8, 2014, 5:17:15 AM11/8/14
to Julia Users
Julia doesn't use the shell to execute commands, but parses "shell commands" itself. Since Julia has its own control flow constructs, we don't duplicate those at the "shell" level. See http://julia.readthedocs.org/en/latest/manual/running-external-programs/#pipelines for more information. 

Davide Lasagna

unread,
Nov 8, 2014, 9:38:06 AM11/8/14
to julia...@googlegroups.com
Thanks Stefan,

As far as I can see from the docs, the pipe |> can be used to chain commands where one would use a true pipe in the shell. So the examples like 
run(`cut -d: -f3 /etc/passwd` |> `sort -n` |> `tail -n5`)
do make sense to me. However, it appears that if one wants to use operators like ; && || in commands, one needs to implement the logic at the julia level. That is fine.

However, I tried to cheat and used the pipe |> to chain commands even tough I do not need or want redirection to occur. For instance:
run(`touch a` |> `sleep 3` |> `touch b`)
This example  runs, but the three commands are executed "at the same time" as I see the two files a and b appearing at the same time in the file manager. Hence I deduce that |> cannot be used to chain all king of commands. Am I right?
Davide

Toivo Henningsson

unread,
Nov 8, 2014, 11:05:58 AM11/8/14
to julia...@googlegroups.com
It seems that implementing the logic at the Julia level could be as simple as

success(`touch a`) && success(`sleep 3`) && success (`touch b`)

Slightly more verbose, but not so much. Overloading && is not possible (without a macro) due to the sort circuit characteristics.

Reply all
Reply to author
Forward
0 new messages