1

I create this code into ruby for make one directory and change position in this directory. But it not work and i understand why bash return error

sh: :1 cd /path/ not found:

this is my code in ruby

#!/usr/bin/ruby

def pr_cartella
  @cartella=ARGV[0] #prendo l'argomento
  Dir.mkdir @cartella #creo la cartella dall'argomento ottenuto
  if Dir.exist? @cartella
    puts "bene la cartella è stata creata"
    Dir.chdir @cartella
    entra
  end
end

def entra
  comando = "cd " + Dir.pwd + @cartella
  %x[ "#{comando}" ]
end

pr_cartella
dax
  • 10,779
  • 8
  • 51
  • 86
awar
  • 61
  • 8
  • how are you calling this? what is ARGV[0]? – dax Aug 10 '15 at 14:14
  • you call your function `pr_cartella` without argument, but then read the first argument using `ARGV[0]`... – bro Aug 10 '15 at 14:15
  • yes i write the argument when call the script, sample: ./namescript.rb namefolder. It work because make the folder but not go in the new folder just created – awar Aug 10 '15 at 14:17

3 Answers3

1

It looks like you are trying to change the directory twice, which is causing the error to occur.

def pr_cartella
  @cartella=ARGV[0] 
  Dir.mkdir @cartella # This makes the directory
  if Dir.exist? @cartella # Assuming the mkdir passed, the always succeeds
    puts "bene la cartella è stata creata"
    Dir.chdir @cartella # This changes from pwd to the newly made directory
    entra
  end
end

Then inside your entra method, you try to change it with a system level command, but you already changed it with Dir.chdir. The documentation states:

Changes the current working directory of the process to the given string.

So this method:

def entra
  comando = "cd " + Dir.pwd + @cartella # Dir.pwd is already /path/to/@cartella
  %x[ "#{comando}" ]
end

Is actually trying to do:

cd /path/to/@cartella/@cartella

Since you expanded in the comments to say you were actually trying to affect the terminal session, I wanted to point out that you cannot use ruby to do that as discussed in this duplicate question: How to (terminal) cd in folder from ruby script

Community
  • 1
  • 1
jkeuhlen
  • 4,401
  • 23
  • 36
  • but what the way for change directory in the shell??? This is my problem – awar Aug 10 '15 at 14:24
  • So i must write only this: comando = "cd " + Dir.pwd + – awar Aug 10 '15 at 14:29
  • i try. The path now is correct but it not work. The shell return same error. – awar Aug 10 '15 at 14:32
  • @away Why do you need to change the directory for the shell? – jkeuhlen Aug 10 '15 at 14:36
  • My point is that the code is changing the current directory for the duration of the script. If you need to run another shell command from inside your script and it needs to be executed from a certain directory, you can do that using a `Dir.chdir` block. If you tell us why you need to do something we can point you in a better direction – jkeuhlen Aug 10 '15 at 14:41
  • simple, i need, with ruby, create a folder and go inside. But i must inside not in the ruby script but in the shell. In the teminal i not want write this: mkdir namefolder && cd nameforlder. When i move the ruby script in the /usr/bin i want write in the terminal: script.rb namefolder and it must be create a folder and go inside. This is all – awar Aug 10 '15 at 15:40
  • Then you need to use something other than ruby I believe. When you execute a ruby script it will not modify a terminal session you already have open and move for you. You can do all of that inside the script, but as soon as it finishes running it will return to where you started. Try using bash. An alias would work to do what you want if you really don't want to write `mkdir namefolder && cd namefolder` – jkeuhlen Aug 10 '15 at 16:08
1

The solution is in this code.

def pr_cartella
 @cartella=ARGV[0] #prendo l'argomento
 Dir.mkdir @cartella #creo la cartella dall'argomento ottenuto
 if Dir.exist? @cartella
 puts "bene la cartella è stata creata"
 Dir.chdir @cartella
 exec 'bash'
 end
 end
 pr_cartella

thanks to all.

awar
  • 61
  • 8
0

It is pointless to use %x(cd SOMEWHERE). After the command is finished, you are back where you had been before.

If you use Dir.chdir and then shell out to somewhere, you are already in the directory you have chdir'ed before, UNLESS your shell is implicitly sourcing some file (say: .bashrc, ....), and this file would be doing a chdir.

In other words: If I do a

Dir.chdir('goofy')
x=%x(pwd)

and the directory goofy exists, the variable x will contain something like '/..../..../..../goofy'

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • ok, but if i want change directory after created what the way? – awar Aug 10 '15 at 14:35
  • I don't get the meaning of your question. What exactly means "change directory"? Rename it? Make it your working directory? In the latter case, this is exactly what Dir.chdir is doing. Where is the problem? – user1934428 Aug 10 '15 at 15:25
  • i need, with ruby, create a folder and go inside. But i must inside not in the ruby script but in the shell. In the teminal i not want write this: mkdir namefolder && cd nameforlder. When i move the ruby script in the /usr/bin i want write in the terminal: script.rb namefolder and it must be create a folder and go inside – awar Aug 10 '15 at 15:46
  • Why do you want to do the chdir in the script (and not within your Ruby program), and what do you want to do, once you are in the skript? Of course you can do a `system("mkdir x && chdir x && my_script.sh")`, which would then run `my_script.sh` inside directory x, but don't forget that after the execution of my_script.sh is done, you are back in the directory where you had been before doing the chdir. This may or may not be what you want. – user1934428 Aug 11 '15 at 04:54