From: John Carter Date: 2003-10-03T10:16:53+09:00 Subject: backquotes, system, shell and ruby Somebody suggested I use zsh instead of bash for my commandline, so I looked at it, looked at the syntax for tweaking the profile files, and said... "Yuck! Another badly designed syntax! Why can't I just use Ruby?" And that got me thinking. Consider backquotes. a = `wc *.c` What did that do? Same as open("|wc *.c"). It ran bash! Yuck! system( "tar -xvzf foo.tgz foo") What did that do? It ran shell! Why? Why does a scripting language like Ruby keep invoking an old and bad scripting language call shell? Because... a) Ruby doesn't have a good syntax for invoking and dealing with processes and pipes between processes. b) Ruby doesn't have a built in understanding of ENV['PATH'] c) Ruby doesn't have such a simple syntax for globbing (beyond Dir['*.c']) Consider the "rename" facility that comes with perl these days. For example, to rename all files matching "*.bak" to strip the exten- sion, you might say rename 's/\.bak$//' *.bak To translate uppercase names to lower, you'd use rename 'y/A-Z/a-z/' * Ooh! Looky! Nice! Wouldn't it be nice to have the power of ruby intertwingled with the commandline. Ruby does have Shell.rb, which takes us halfway there. (If it had decent docs...) How hard would it be to extend the backtick syntax to be a pure ruby thing. ie. So proc_exec doesn't invoke bash, but invokes Shell.rb? Hmm. Could we even do this entirely in UserLand? ie. Couldn't we override proc_exec and replace it with ruby code that parses the string, handles the redirects, variable interpolation quoting and forks, then just directly execs the program without ever touching sh? John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand A Million Monkeys can inflict worse things than just Shakespeare on your system.