Change the current working directory of the calling shell from ruby ​​script

I am writing a simple ruby ​​sandbox command-line utility to copy and extract directories from a remote file system to a local directory from scratch, to unzip them and allow users to edit files. I use Dir.mktmpdir as the default zero directory, which gives a really ugly path (for example: / var / folders / zz / zzzivhrRnAmviuee +++ 1vE +++ yo / -Tmp- / d20100311-70034-abz5zj)

I would like the last action of the copy-and-unzip script to call the shell of the call to the new directory from scratch so that people can easily access it, but I cannot figure out how to change the PWD of the calling shell. One possibility is for the utility to print a new path to stdout and then run the script as part of a subshell (i.e. cd $(sandbox my_dir) ), but I want to print information about the copy and unpack progress, as it may take up to 10 minutes, so it won’t work. Should I just go to a predefined, easy to find directory from scratch? Does anyone have a better suggestion? Thanks in advance for your help. -Erik

+4
source share
4 answers

You can die in a new shell that sits on your tree from scratch, but it's hard for me to understand the essence of all this. If your users will edit these files, I believe that they either want to save them, or plan to work with the modified files. In the first case, put the files in a reasonable location and you don’t have to worry about your users finding them (current working directory). In the second case, create a subshell for your users, which is located in the tree from scratch, and ask them to edit the files. When the child shell comes out, you can continue to process your files from one ruby ​​program.

+3
source

A process cannot change the environment of its parent process, so you have to do some action in the parent.

Maybe your users have a shell script, and the script calls the ruby ​​program and cd somewhere. When you use the shell source command ( . Or source ), this affects the current process.

+1
source

As Glenn Jackman notes, the child process cannot change the parent current working directory (or environment variables, etc.). If you do not think that you are violating the parent process using the debugger ...

A process running as part of command substitution can be written to stderr to provide output to the user, while stdout output is captured by command substitution:

 cd "$(sandbox whatever)" 

In cases where the sandbox command sends most of its output to stderr, but only the final 'directory in chdir to stdout. If the sandbox was a shell script, you could do it like this:

 #!/bin/sh newdir="$(mktemp -dt sandobox)" { # do whatever, in a sub-shell # use a group command (or change the braces to parens to use a sub-shell) to # capture all stdout and send it to stderr } 1>&2 echo "$newdir" 

I'm sure Ruby can do something equivalent.

+1
source

First create your temporary directory from the shell script shell, and then pass it as a command line argument to your Ruby script.

0
source

Source: https://habr.com/ru/post/1303863/


All Articles