11  System interface

Useful Scheme programs often need to interact with the underlying operating system.

11.1  Checking for and deleting files

file‑exists? checks if its argument string names a file. delete‑file deletes its argument file. These procedures are not part of the Scheme standard, but are available in most implementations. These procedures work reliably only for files that are not directories. (Their behavior on directories is dialect-specific.)

file‑or‑directory‑modify‑seconds returns the time when its argument file or directory was last modified. Time is reckoned in seconds from 12 AM GMT, 1 January 1970. E.g.,

(file-or-directory-modify-seconds "hello.scm")
=> 893189629

assuming that the file hello.scm was last messed with sometime on 21 April 1998.

11.2  Calling operating-system commands

The system procedure executes its argument string as an operating-system command.1 It returns true if the command executed successfully with an exit status 0, and false if it failed to execute or exited with a non-zero status. Any output generated by the command goes to standard output.

(system "ls") 
;lists current directory

(define fname "spot")

(system (string-append "test -f " fname)) 
;tests if file ‘spot’ exists

(system (string-append "rm -f " fname)) 
;removes ‘spot’

The last two forms are equivalent to

(file-exists? fname)

(delete-file fname)

11.3  Environment variables

The getenv procedure returns the setting of an operating-system environment variable. E.g.,

(getenv "HOME")
=> "/home/dorai"

(getenv "SHELL")
=> "/bin/bash"


1 MzScheme provides the system procedure via the process library. Use (require (lib "process.ss")) to load this library.