5

I'm currently coding in Julia, and at some point, I have to run a .exe program at the Command Prompt. Let's name that program "x.exe". I decided to add the following line to my code for Julia to execute the program inside of the code:

run(pipeline('x.exe input.txt 5 500',stdout="output.txt"))

The code works perfectly, but I have to insert manually the values "5" and "500", which are respectively, the number of rows in the input.txt file and the number of items of each row in the input.txt file. They also are the number of rows and columns of an Array stored in Julia.

Is there a way for the code to read those numbers directly? I tried

writedlm("size.txt", transpose([size(Array)[1],size(Array)[2]])," ")

and then

run(pipeline('x.exe input.txt type size.txt',stdout="output.txt"))

but it don't work....

coolsv
  • 781
  • 5
  • 16

1 Answers1

2

You can use @sprintf, e.g. as follows:

julia> using Printf

julia> x = [[1,2,3], [4,5,6]]
2-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [4, 5, 6]

julia> a = @sprintf("%d", size(x)[1][1])
"2"

julia> b = @sprintf("%d", size(x[1])[1][1])
"3"

julia> run(pipeline(`x.exe input.txt $a $b`,stdout="output.txt"))

As you can see, Julia uses the same method to interpolate variables into commands as the shell does (see the Julia manual section on Running External Programs).

Simon
  • 10,679
  • 1
  • 30
  • 44
  • Thank you Simon! Unfortunately, I get the following error: `ERROR: MethodError: no method matching pipeline(::String; stdout="output.txt") Closest candidates are: pipeline(::Union{RawFD, WindowsRawSocket, FileRedirect, AbstractString, IO}, ::Base.AbstractCmd) a t process.jl:277 got unsupported keyword argument "stdout" pipeline(::Any, ::Any, ::Any, ::Any...) at process.jl:297 got unsupported keyword argument "stdout " pipeline(::Base.AbstractCmd; stdin, stdout, stderr, append) at process.jl:261 ... Stacktrace: [1] top-level scope at none:0` – coolsv Feb 10 '19 at 01:40
  • I'm using version 1.0.2 of Julia – coolsv Feb 10 '19 at 01:45
  • 1
    Sorry about that, @coolsv, I hadn't used `pipeline()` myself before and didn't realise it needed backticks around the command rather than using a string. I've updated my answer. – Simon Feb 10 '19 at 02:14
  • OMG!!! Wonderful, Simon!!!! It worked!!! Thank you a lot!!!!!!! Yes, for running cmd prompt, we need backticks :-) – coolsv Feb 10 '19 at 02:22
  • 2
    I think that without `@sprintf` the interpolation should also work, e.g. `x.exe input.txt $(size(x[1],1)) $(size(x[2],1))`. Note that if you interpolate expressions you have to wrap them in `(` and `)`. – Bogumił Kamiński Feb 10 '19 at 09:04
  • Actually , I must also run the same Julia program but in a Linux environment. I have the Linux version of the executable file, say Y. I tried spawn(pipeline('./Y input.txt $a $b', stdout="output.txt")) , but it doesn't work... Julia says "spawn not defined". Any suggestion ? – coolsv Feb 15 '19 at 18:52
  • 1
    @coolsv: you still need to use `run()` if you are in a Linux environment. `@spawn` runs a task in parallel so it works a bit differently. You could use that in any Julia environment. See [Parallel Computing](https://docs.julialang.org/en/v1/manual/parallel-computing/index.html) for more information. – Simon Feb 15 '19 at 22:21
  • Ok Simon, thank you! I tried to send you an email directly from here to ask you that question, but wasn't able to find it. I created a separated post for my question after that. I will try it with `run()`. I think I had an error, but I must see. – coolsv Feb 15 '19 at 22:33
  • I tried `run(pipeline('./Y input.txt $a $b',stdout="output.txt"))` where Y is the Linux executable command but I get the following error: `failed process: Process('./Y input.txt 2 3', ProcessSignaled(6)) [0]`. I put correctly the backticks inside of pipeline instead of ' – coolsv Feb 15 '19 at 22:51
  • 1
    A process signal of 6 is "Abort" (see [When does a process get SIGABRT (signal 6)?](https://stackoverflow.com/questions/3413166/when-does-a-process-get-sigabrt-signal-6)). That makes me think that your program `Y` might be aborting because it has a bug in it. Can you successfully run `./Y input.txt a b` with the right numbers substituted in for `a` and `b`, directly from the shell? – Simon Feb 15 '19 at 23:02
  • Yes, when I run the program on the MATE terminal separately and with the correct `a` and `b` it works – coolsv Feb 15 '19 at 23:57
  • 1
    I finally found the error! The name of the input.txt file was wrong. I corrected it and now it works! Thank you for your help anyway, because it was useful for me to do it on Linux :-) – coolsv Feb 16 '19 at 00:21