Loading...
 

Crystal Shell Kommados mit Process

Hier zeigen wir, wie man in Crystal einfache Shell Commandos ausführen kann.

Wie hier üblich, handelt es sich um ein minimalistisches Beispiel ohne Fehlerabfrage oder dergleichen.

# Running a shell command on the system
puts "Running Shell Command"

# Create a Memory Buffer
my_output = IO::Memory.new
my_error = IO::Memory.new

# Run a process to perform a shell command
# run execute the comand and wait for it to complete
Process.run("ping", args: ["localhost"], output: my_output, error: my_error)

# new execute the command, but does not wait for it to complete
# Process.new("ping", args: ["localhost"], output: my_output, error: my_error)
# sleep 5

# get the position of the output
my_output.pos = my_output.pos
pp my_output.to_s
puts "----"

# some errors?
p! my_output.to_s

my_error.pos = my_error.pos
pp my_error.to_s

# free the memory
my_output.close
my_error.close