I’m almost certainly missing something obvious here. The goal is the issue a shell command that includes an argument based on a variable I’ve defined. So, having defined theString as “abcde”, I’d like to run “echo abcde” and capture its output. Is there a more natural way to include the value of the variable in the command than this?
(define theString "abcde")
(define theOutput
(with-output-to-string
(lambda ()
(system (format "echo ~a" theString)))))
(display theOutput)
Thank you!
@ghoetker Using format
is typical; it’s been in Racket “forever”. More recently, racket/format
also provides a function ~a
. (~a "echo " the-string)
would be a shorter way of saying (format "echo ~a" the-string)
[BTW #lang racket
requires a bunch of modules — such as racket/format
— that #lang racket/base
doesn’t. When using #lang racket/base
, you’d need to (require racket/format)
yourself.]
Speaking of “typical” or “natural”, we tend to kebab-case
names not camelCase
. :slightly_smiling_face:
I think one way to do it differently would be to write (system* (find-executable-path "echo") the-string)
.
echo
might be a shell command instead of an executable. But otherwise using system*
might be preferable as you don’t have to worry about shell quoting conventions.
Yes, system
will use the builtin whereas system*
will use some executable.
I’m not sure echo
is an executable file that can be found, on every OS. But that’s less important than your point that system*
is usually a better idea.
Maybe there’s an executable alias for builtins in every OS since ye olden times and I’m just old :slightly_smiling_face:
I believe POSIX specifies echo
must exist as an executable, most shells just provide a builtin to add features and/or improve performance.
What is this new-fangled POSIX of which you speak
Anyway sorry to get into the weed on #beginners
. The system*
is a good thing to know about.
Erm I meant get into the weeds plural. Getting into the weed is… oh never mind.
Thank you @greg @lexi.lambda I used echo as an easy example, but was able to easily adjust to my need.
kebab-case
is a new one to me. I have kebabized my names. Thanks.
As part of a program I’m writing, I find myself calling (system (format "echo ~a \| base64 -D \| plutil -convert xml1 -o - -- -" a-string)
about, oh, 7000 times. (I’m dependent on Apples plutil program, unless someone knows a better way to convert Apples binary plist format to human-readable XML format). The program takes a really long time, I assume because calling (system )
has “start-up” time. Messing with the rest of my code has me fairly convinced that (system )
is the slow part.
Can any point me towards a more efficient way to repeatedly invoke a system command? Suggestions for a smarter approach?
The fact that I have this program running at all stuns me and is only possible because of help I’ve received here and on the mailing list. Thank you!