Выполнить командную строку из Scheme (Guile)

Вопрос описан в заголовке, в основном я хотел бы выполнить командную строку из схемы, скажем, «ls» и получить вывод. Итак, мои вопросы:

  • Является ли это возможным?
  • Как?

Заранее большое спасибо!

Кстати, я использую Guille.


person pafede2    schedule 19.10.2015    source источник


Ответы (1)


Вам нужен один из этих system и system*.

Пример: (system "ls")

Из документации: справочник по Guile

— Scheme Procedure: system [cmd]
— C Function: scm_system (cmd)
Execute cmd using the operating system's “command processor”. Under Unix this is usually the default shell sh. The value returned is cmd's exit status as returned by waitpid, which can be interpreted using the functions above.

If system is called without arguments, return a boolean indicating whether the command processor is available.

— Scheme Procedure: system* . args
— C Function: scm_system_star (args)
Execute the command indicated by args. The first element must be a string indicating the command to be executed, and the remaining items must be strings representing each of the arguments to that command.

This function returns the exit status of the command as provided by waitpid. This value can be handled with status:exit-val and the related functions.

system* is similar to system, but accepts only one string per-argument, and performs no shell interpretation. The command is executed using fork and execlp. Accordingly this function may be safer than system in situations where shell interpretation is not required.

Example: (system* "echo" "foo" "bar")
person soegaard    schedule 19.10.2015
comment
Как потоки ввода, вывода и ошибок обрабатываются system? - person coredump; 19.10.2015
comment
Я не думаю, что они затронуты - Guile system вызывает posix system более или менее напрямую. github.com/cky/guile/blob/stable- 2.0/libguile/simpos.c#L65 - person soegaard; 19.10.2015