ghoetker
2018-5-11 21:12:48

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!


greg
2018-5-11 22:12:57

@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.]


greg
2018-5-11 22:13:50

Speaking of “typical” or “natural”, we tend to kebab-case names not camelCase. :slightly_smiling_face:


lexi.lambda
2018-5-11 22:13:50

I think one way to do it differently would be to write (system* (find-executable-path "echo") the-string).


greg
2018-5-11 22:16:48

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.


lexi.lambda
2018-5-11 22:17:24

Yes, system will use the builtin whereas system* will use some executable.


greg
2018-5-11 22:18:32

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.


greg
2018-5-11 22:19:56

Maybe there’s an executable alias for builtins in every OS since ye olden times and I’m just old :slightly_smiling_face:


lexi.lambda
2018-5-11 22:20:59

I believe POSIX specifies echo must exist as an executable, most shells just provide a builtin to add features and/or improve performance.


greg
2018-5-11 22:21:57

What is this new-fangled POSIX of which you speak


greg
2018-5-11 22:22:58

Anyway sorry to get into the weed on #beginners. The system* is a good thing to know about.


greg
2018-5-11 22:23:22

Erm I meant get into the weeds plural. Getting into the weed is… oh never mind.


ghoetker
2018-5-11 23:03:58

Thank you @greg @lexi.lambda I used echo as an easy example, but was able to easily adjust to my need.


ghoetker
2018-5-11 23:35:08

kebab-case is a new one to me. I have kebabized my names. Thanks.


ghoetker
2018-5-12 01:49:45

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!