admin管理员组

文章数量:1123433

When attempting to spawn an interactive bash shell from within a Guile script using the system* function from the ice-9 popen module, the shell does not behave as expected. Specifically, the standard input and output do not seem to be redirected correctly, making the shell non-interactive and unusable. The bash shell opens but does not accept input or display output, which suggests that there might be an issue with how I/O is being handled in the Guile script. Additional guidance or examples on properly spawning an interactive shell with correct I/O handling in Guile would be greatly appreciated.

(use-modules (ice-9 popen)) ; For system*

;; Function to spawn an interactive bash shell
(define (spawn-shell)
  (format #t "Spawning an interactive bash shell...~%")
  (system* "bash") ; Spawn a bash shell and wait for it to exit
  (format #t "Returning to the REPL.~%"))

;; Example usage
(spawn-shell)

When attempting to spawn an interactive bash shell from within a Guile script using the system* function from the ice-9 popen module, the shell does not behave as expected. Specifically, the standard input and output do not seem to be redirected correctly, making the shell non-interactive and unusable. The bash shell opens but does not accept input or display output, which suggests that there might be an issue with how I/O is being handled in the Guile script. Additional guidance or examples on properly spawning an interactive shell with correct I/O handling in Guile would be greatly appreciated.

(use-modules (ice-9 popen)) ; For system*

;; Function to spawn an interactive bash shell
(define (spawn-shell)
  (format #t "Spawning an interactive bash shell...~%")
  (system* "bash") ; Spawn a bash shell and wait for it to exit
  (format #t "Returning to the REPL.~%"))

;; Example usage
(spawn-shell)
Share Improve this question asked 15 hours ago RainbRainb 2,3901 gold badge17 silver badges36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can spawn an interactive bash shell from within the Guile REPL by using low-level system calls to fork a new process and execute bash. Here's how you can do it:

(use-modules (ice-9 popen)
             (ice-9 rdelim)
             (ice-9 match)) ; For match-let

;; Function to spawn an interactive bash shell
(define (spawn-shell)
  (format #t "Spawning an interactive bash shell...~%")
  (let ((pid (primitive-fork)))
    (if (= pid 0)
        ;; Child process: replace current process with bash
        (begin
          ;; Replace stdin, stdout, and stderr with the current ones
          (dup2 0 0) ; stdin
          (dup2 1 1) ; stdout
          (dup2 2 2) ; stderr
          (execlp "bash" "bash") ; Replace current process with bash
          (format #t "Failed to spawn bash shell.~%")
          (exit 1))
        ;; Parent process: wait for the child to exit
        (begin
          (let ((status (waitpid pid)))
            (format #t "Returning to the REPL.~%")
            status)))))

;; Example usage
(spawn-shell)

本文标签: