ctarbi.de - scsh·ipc·fork·exec·waitpid·run

This is a continuation of the examples shown in scsh examples.

To run them yourself, put the code in a file.scm then run scsh -s file.scm.

See the references below for more information.

Examples

hello

code:

(display "hello world!\n")
(run (echo "hello world!"))
(run (/bin/echo "hello world!"))
(run (perl -le"print q{hello world!}"))
(run (perl -le"print 'hello world!'"))

output:

hello world!
hello world!
hello world!
hello world!
hello world!

run

code:

(run (seq 3))

(run (begin
    (display 1)(newline)
    (display 2)(newline)
    (display 3)(newline)))

(define (sequence a b)
    (if (<= a b)
        (begin
            (display a)
            (newline)
            (sequence (+ a 1) b))
        (values)))

(sequence 1 3)

(run (begin (sequence 1 3)))

(run (| (seq 100) (perl -lne"$s+=$_}{print$s")))

(run (| (begin (sequence 1 100)) (perl -lne"$s+=$_}{print$s")))

(run (| (begin (sequence 1 100)) (begin (apply exec-path '(perl -lne"$s+=$_}{print$s")))))

(define (fold-left op acc xs)
    (if (null? xs)
        acc
        (fold-left op (op acc (car xs)) (cdr xs))))

(define (sum-slurp)
    (define (read-lines) (port->string-list (current-input-port)))
    (let ((lines (read-lines)))
        (display (fold-left + 0 (map string->number lines)))
        (newline)))

(run (| (begin (sequence 1 100)) (begin (sum-slurp))))

(define (sum-no-slurp)
    (define (doit reader port)
        (let lp ((acc 0))
            (let ((x (reader port)))
                (if (eof-object? x)
                    acc
                    (lp (+ acc (string->number x)))))))
    (display (doit read-line (current-input-port)))
    (newline))

(run (| (begin (sequence 1 100)) (begin (sum-no-slurp))))

output:

1
2
3
1
2
3
1
2
3
1
2
3
5050
5050
5050
5050
5050

fork-waitpid-exec

code:

(define (show p v)
    (format #t "~a~s~%" p v))
(define (slurp-to-string epf)
    (port->string (run/port* (lambda () (apply exec-path epf)))))
(define (slurp-to-list epf)
    (port->string-list (run/port* (lambda () (apply exec-path epf)))))

(show "(seq 10): " (port->string-list (run/port (seq 10))))
(show "(seq 10): " (run/strings (seq 10)))
(show "(seq 10): " (slurp-to-list '(seq 10)))
(show "(seq 10): " (run/string (seq 10)))
(show "(seq 10): " (slurp-to-string '(seq 10)))

(& (sh -c "sleep 1; date"))

(run (| (seq 100) (perl -lne"$s+=$_}{print$s")))

(exec-epf (date))

(display "Will never happen, due to 'execve' system call above.")

output

(seq 10): ("1" "2" "3" "4" "5" "6" "7" "8" "9" "10")
(seq 10): ("1" "2" "3" "4" "5" "6" "7" "8" "9" "10")
(seq 10): ("1" "2" "3" "4" "5" "6" "7" "8" "9" "10")
(seq 10): "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
(seq 10): "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
5050
Mon Mar 18 14:39:32 UTC 2024
Mon Mar 18 14:39:33 UTC 2024

References

These are the the combined references from the scripts above.

More details in the link below.

This page was last modified on February 16, 2024 at 18:07:46 UTC.