Here is my script
#!/usr/bin/env ruby
if __FILE__ == $0
`cd ..`
puts `ls`
end
which runs fine, but when it exits, I’m back where I started. How can I “export” the changes I’ve made to the environment?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That is because the backtick operator is not intended for complicated
scripting. It is useful for running a single command (and capturing
its output). There is no shell behind it to store environment changes
between its calls and after your Ruby script is terminated.
On Linux systems each process has its own current directory path (it
could be found in
/proc/‹pid›/cwd). Changing directory in a processdoes not affect parent processes (the shell you run program from). If
the
cdbuilt-in were a binary, it could change only its own currentdirectory, not the one of the parent process, and that is why the
cdcommand could be built-in only.
An alternative implementation
If a Ruby script must be executed from shell and must affect the
shell’s current directory path, the following trick may be used.
Instead of running commands from within Ruby, print those commands to
the standard output, and then
sourceit to the shell you are runningthe Ruby script from. The commands will not be executed by a separate
process of a shell, so all
cds will take effect in the currentshell instance.
So, instead of
write in your shell-script something like that:
The
ShellclassBut there is a convenient tool for command line scripting in Ruby: the
Shellclass! It has predefined shortenings for frequently usedcommands (such as
cd,pwd,cat,echo, etc.) and allows todefine your own (it supports commands and aliases). It also
transparently supports redirection of the standard input and output
streams using
|,>,<,>>,<<Ruby operators.Working with
Shellis self-explanatory most of the time. Take a lookat the following straightforward examples.
Creating a
Shellobject and changing the current directoryWorking within the current directory
Note that parentheses are necessary sometimes because of the
precedence of the redirection operators. Further, the output
of a command is not printed to by default, so you need to specify
that use
> STDOUT, or> STDERRif needed.Testing file properties
Works similar to
testfunction in a usual shell.Defining custom commands and aliases
The name of a defined command can be used to run it later (in exactly
the same way as it works with predefined
echoorcat, for example).Using the directory stack
Here the custom
listcommand defined above is used.By the way, there is a convenient
chdircommand to run severalcommands in a directory and return to previous working directory
after that.
Skip the shell object for a group of commands
Additional features
In addition, the
Shellclass has:foreachmethod to iterate through lines in a file, or throughlist of files in a directory (depending on whether given path points
to a file or a directory);
jobsandkillcommands to control processes;basename,chmod,chown,delete,dirname,rename,stat, andsymlink;Filemethods’ synonyms:directory?,executable?,exists?,readable?, etc.;FileTestclass methods:syscopy,copy,move,compare,safe_unlink,makedirs, andinstall.