PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 22, 2022

[FIXED] How to get the output of Process.exec in crystal-lang?

 July 22, 2022     crystal-lang, exec, process, shell     No comments   

Issue

I need to get the output of Process.exec (not Process.run) as a string in crystal. Can this be done?

I've tried

Process.exec base, args

But it only puts it to the console. I'd like to put it in a variable.


Solution

As already clarified in comments, you can't capture output of a process executed using Process.exec, but there are ways to execute a process and capture it's output.

The most straightforward one - backticks:

output = `echo "Hello world"`

In more complex scenarios (e.g. you need to capture standard output and standard error output separately, need to get also it's status, or to have greater control over it's execution) you can use something like this:

stdout = IO::Memory.new
process = Process.new("echo", ["Hello world"], output: stdout)
status = process.wait
output = stdout.to_s

or

stdout = IO::Memory.new
status = Process.run("echo", ["Hello world"], output: stdout)
output = stdout.to_s


Answered By - igneus
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing