Is there a package that introduces shell-like syntax using macros and the builtin pipeline?

I am thinking of changing my primary script language to Julia. The pipeline and backtick command construction are awesome, but their syntax is not as simple and concise as traditional shells. Is there a package dedicated to introducing syntactic sugar around scripting usage? Something like:

@s cat "somedir/somefile.txt" | head -n 23 | tail -n 10 | grep julia_var > "result.txt"

There are several packages that provide additional support for piping, most of which are enumerated here: https://github.com/JuliaLang/julia/issues/5571#issuecomment-205754539

1 Like

Yes, those are awesome, and I already use them. But they are for function chaining inside Julia. I want traditional shell piping that chains external processes. It doesn’t seem one exists though. I should write one when I get the time.

I assume you’re aware of these, but for discoverability and future reference I thought it would be worth mentioning some useful related resources:

Also, for REPL usage, there’s the shell mode.

2 Likes

Aren’t “external processes” also represented as a function or some other callable object? What prevents them from being piped?

If someone writes a wrapper around them, then yes, any external program can be turned into a Julia callable. In Python, there is from plumbum.cmd import cat, which autowraps external programs. I am not aware of such tools in Julia.

run(`echo hello, world` |> "test.txt") doesn’t work for me:

ERROR: MethodError: objects of type String are not callable
Stacktrace:
 [1] |>(::Cmd, ::String) at ./operators.jl:823
 [2] top-level scope at REPL[96]:1
run(pipeline(`echo hello, world`, stdout = "test1.txt"))`

according to the docs on external commands

I know, I am looking for syntactic sugar around these builtins.

1 Like
# → is \to<tab>

julia> →(args...) = pipeline(args...)

julia> run(`cat /etc/passwd` → `head -4` → `sort`)

julia> run(`cat "somedir/somefile.txt"` → `head -n 23` → `tail -n 10` → pipeline(`grep julia_var`,stdout="result.txt") )
2 Likes