Accidentally killing coreutils - an idea to use ruby?

Lately I managed to kill all binaries that typically belong to
coreutils. (Also had no net available on that machine and no backup)

I.e. rm cp sleep echo touch etc… were all suddenly unavailable.

I managed to recover thanks to busybox. So everything is fine. BUT, that
got me thinking…

Ruby still worked nicely and the tasks such as “mv”, “cp” etc… work in
pure ruby too. (FileUtils.mv FileUtils.cp_r and so on)

Now here is my fairly odd question - when I run something like
“./configure” and it is checking for something like “cp”, and if it can
not find it, it will state “cp: command not found”, or something, is
there a way to somehow use ruby instead of that?

For example, “cp” could perhaps be a ruby file and inside that file
could be

require ‘fileutils’
FileUtils.cp

Would that work?

Additionally, would there be a way to completely control all aspects of

system()

from within a script, so that I could associate something like “cp” with
a specific action using ruby code instead?

Yeah, I have fairly weird ideas sometimes. :slight_smile:

And no, I have not tested anything yet… but I figure that if ruby is
working somewhere, one could perhaps use pure ruby anyway … at least
for the basic file operations.

Would that work?

Not without some effort. To work with any configure script it would have
to handle all potential command line arguments for cp (such as recursive
copy, copy with all permissions in tact, etc.). Also anything specific
to the system (linux, bsd, solaris, etc). In other words I’m not sure
I’d recommend it.

Regards,
Chris W.
http://www.twitter.com/cwgem

Not without some effort. To work with any configure script it would have
to handle all potential command line arguments for cp (such as recursive
copy, copy with all permissions in tact, etc.). Also anything specific
to the system (linux, bsd, solaris, etc). In other words I’m not sure
I’d recommend it.

I am not sure it has to be recommended. :slight_smile:

As for BSD, Solaris etc… I don’t use these. I use 99% of my time Linux,
1% on windows.

I think my question is more rather “How could this be done?”

Recursive copy can be done in pure ruby, handling permissions can be
done in pure ruby.

What I however am not sure of doing is, how to replace those calls with
ruby calls instead …

On Fri, Aug 19, 2011 at 8:52 PM, Marc H. [email protected]
wrote:

Additionally, would there be a way to completely control all aspects of

system()

from within a script, so that I could associate something like “cp” with
a specific action using ruby code instead?

I don’t think you can realistically. You’d probably have to hack some
standard library and I would not even consider opening this can of
worms.

Yeah, I have fairly weird ideas sometimes. :slight_smile:

Don’t we all have from time to time? :slight_smile:

And no, I have not tested anything yet… but I figure that if ruby is
working somewhere, one could perhaps use pure ruby anyway … at least
for the basic file operations.

You could still use IRB with FileUtils loaded. That looks like a more
promising approach. You can’t reuse all your shell scripts though.
But for that to work a shell script interpreter in Ruby could help.
But then again: if you softlink /bin/sh to /usr/local/bin/ruby all
your interactive shell functionality is gone (or must be emulated by
Ruby). And that topic comes up from time to time but I do not think
it’s a worthwhile approach to pursue…

Kind regards

robert

Marc H. [email protected] wrote:

Ruby still worked nicely and the tasks such as “mv”, “cp” etc… work in
pure ruby too.

You can try un.rb in the standard library:

ruby -run -e help

I’ve known about it for years but never used it directly.

Now here is my fairly odd question - when I run something like
“./configure” and it is checking for something like “cp”, and if it can
not find it, it will state “cp: command not found”, or something, is
there a way to somehow use ruby instead of that?

ruby -run -e cp – [OPTION] SOURCE DEST

Additionally, would there be a way to completely control all aspects of

system()

from within a script, so that I could associate something like “cp” with
a specific action using ruby code instead?

You could setup a new bin directory (e.g. /opt/ruby-un/bin/)
full of files executable files like:

---------- /opt/ruby-un/bin/cp ----------------
#!/path/to/ruby -run

ARGV will be handled by cp directly,

no need to care for it in this script

cp
-------------------------- 8< -----------------

…And have /opt/ruby-un/bin in your $PATH

What I however am not sure of doing is, how to replace those calls with ruby
calls instead …
Probably it would be possible to do if there where Bash/Sh translator
written in Ruby. It would take bash script as input and do process it
intelligently with the ability to alter and intercept bash commands.

P.S.
If You need handy ruby wrapper over file system take a look at this
GitHub - al6x/vfs: Virtual File System - simple and unified API over different storages (Local, S3, SFTP, ...) .