120 points · by dexterlagan · 8y ago · 56 comments
Here's an example (https://github.com/tonyg/racket-something/blob/master/exampl...):
#lang something/shell
// Simple demos
ls -la $HOME | grep "^d" | fgrep -v "."
ls -la | wc -l | read-line |> string-split |> car |> string->number |> \
printf "There are ~a lines here." | sed -e "s: are : seem to be :"
(newline)
def ps-output
pipeline
ps -wwwax
preserve-header 1 {: grep "racket" }
space-separated-columns [string->number]
|> csv-expr->table
print ps-output
def message-box text:
whiptail --title "Testing" --ok-button "OK" --msgbox text 8 50
message-box "This is pretty cool."
`ls`, `grep`, `whiptail` and so on are unbound Racket variables, and the macros forming part of the `something/shell` dialect replace them with calls to the external programs, sorting out the plumbing, pipes and so on. `read-line`, `car`, `string-split`, etc are ordinary Racket functions from the standard libraries.(EDIT): Here's a screencast of an interactive session with the shell. Very simple, but shows some of the basics: https://asciinema.org/a/83450
So in that regard I don't think it's fair to compare 'cd' to 'cat' nor 'touch' where people will be used to the GNU / whatever coreutils.
In which case, uh, you want the current functionality.
(Whenever I share a snippet with this idiom, I get told by one of my cow-orkers to stop abusing kittens...)
If you prefer to have the command arguments at the end of the line (so it's easier to amend), you can do e.g:
< file.txt grep pattern
instead of grep pattern file.txt
or cat file.txt | grep pattern
There are also some related useless uses, like useless use of grep | awk: grep pattern file | awk '{print $3}'
which can instead be written as awk '/pattern/ {print $3}' fileFrom the readme I don’t quite understand the purpose of lsh, though. Usually, a shell allows you to run programs and manipulate their environment. However, lsh seems to reimplement some commands like find or rm and does not provide operators to manipulate the runtime environment like redirecting output. Maybe you can write in the readme your goal: play with racket, have a shell that accepts racket forms or something different.
E.g., the README states that 'cd/' is an alias for '(cd "/")' which seems to imply that one has to wrap every command in '()' and quote every filename... not very practical for a shell!
(pmap (lambda (f) (run (+ "[some-command] " f))) (directory-list))
...and run any binary against all files in the current directory, in parallel. (directory-list) returns a list of files and one can run any Racket function against that list. It's also possible to gather any command output and pipe it back to Racket for processing. I'm working on a more polished build with better docs.
I'd like to cover the minimum (like running binaries directly instead of having to use run). I've only begun to think about everything that's needed to bring this to its full potential.
I'll also check the other projects I hadn't heard of, maybe some of them are already far better than mine :)Did not know that. That’s a neat trick! Do feel free to share more if you have any :)
I did have to add a lot to my tramp path. Can't remember why, right off. I just know that it helps find a lot of things that don't make the standard path for some reason.
$ (defun sq (x) (* x x))
$ sq 5
25
More often than not it's easiest to just use the Common Lisp functions for creating directories and interacting with the system, but when that doesn't work, I have a function similar to this one in my .sbclrc file:
(defun run (cmd) (with-output-to-string (outs) (uiop:run-program cmd :output outs)))
At work, I've even created a small Common Lisp library for calling our JSON APIs over HTTPS and running commands on our test clusters using SSH. It's all tightly integrated into Emacs, and I can use it do things like open remote files in my local Emacs.
Note: I am not the author
Of course I'm also happy for anyone to make a new shell that they like and want to use themselves. I did it, after all...
(set-macro-character
#\$ #'(lambda (stream char)
(declare (ignore char))
`(uiop:getenv ,(symbol-name (read stream)))))
This lets you read environment variables like $home, and write to them like ordinary variables, eg (setf $message "hello"). It reads the variable name as a Lisp symbol, so by default it will be upcased; this happens to be convenient for environment variables, but as with any symbols in Lisp, you can escape lowercase characters using either \ before the character or |'s around the whole symbol, or you could change the readtable case, or just preserve case initially in the read macro."Who should I thank? My so-called "colleagues," who laugh at me behind my back, all the while becoming famous on my work? My worthless graduate students, whose computer skills appear to be limited to downloading bitmaps off of netnews? My parents, who are still waiting for me to quit "fooling around with computers," go to med school, and become a radiologist? My department chairman, a manager who gives one new insight into and sympathy for disgruntled postal workers?"
and concludes:
"Oh yes, the acknowledgements. I think not. I did it. I did it all, by myself."
http://philip.greenspun.com/wtr/dead-trees/acknowledgments.h...