how to use shellescape for a path with spaces in it - ruby

essentially, I have code like this (running on CentOS 6.5, ruby 2.3):
foo = "/opt/provisioning/workspace/jobs/This Has Spaces/files/thisfile.xml"
read_file_and_do_something_interesting(foo)
where we have:
def read_file_and_do_something_interesting(file_path)
data = File.read(file_path)
which leads to error:
/opt/provision/jobs/lib/aws_tools.rb:498:in `read': No such file or directory # rb_sysopen - /opt/provisioning/workspace/jobs/This Has Spaces/files/thisfile.xml (Errno::ENOENT)
So, I tried to use shellescape, like this:
read_file_and_do_something_interesting(foo.shellescape)
and still I get error:
/opt/provision/jobs/lib/aws_tools.rb:498:in `read': No such file or directory # rb_sysopen - /opt/provisioning/workspace/jobs/This\ Has\ Spaces/files/thisfile.xml (Errno::ENOENT)
So, simply, how do you use this thing?

I think this file /opt/provisioning/workspace/jobs/This Has Spaces/files/thisfile.xml really not exists.
Can you run ls "/opt/provisioning/workspace/jobs/This Has Spaces/files/thisfile.xml"?

Related

How to use `Dir` to reference directories outside the working directory

The closest issue I could find on Stack Overflow was "Path outside of project, Ruby Dir", but the user appears to have been in the wrong development environment.
My issue is that the Dir class only gives information relative to the working directory. For instance, if I'm in C:/Users/john/Developer and this directory only has two subdirectories A and B then
$ ruby -e "p Dir['*']"
will return ["A", "B"]. This seems intended, but what if there is a directory C:/Users/jane/Developer/C that I need to run a command in using a script in C:/Users/john/Developer?
Running
Dir.chdir('C:/Users/jane/Developer/C') { %x[lerna clean] }
returns the error:
-e:1:in `chdir': No such file or directory # dir_s_chdir - C:/Users/jane/Developer/C (Errno::ENOENT)
from -e:1:in `<main>'
Is there a way to access directories outside of the working directory using the Dir class? It seems absurd to have to require scripts to be written at a root level.
Note, using %x[] to run the command in C:/Users/jane/Developer/C also doesn't seem to work.

Errors with getting file permissions with File.Stat (Errno::ENOENT)

I'm new to programming and I am trying to figure out how to grab all the recursive directories of my array, and get the file permissions of all of them.
Googling was pointing me to file.stat, however the code is stating there is no such file or directory called 'stat'. For context, after getting the file permissions, my next objective is to be able to compare the permissions of the file owner to the group to determine any differences. I think part of my problem displayed with 'File::Stat:0x7f2407a9e908' is that it's not converting to an integer.
Any suggestions on reading material or revision would be greatly appreciated.
Here is my code so far:
%w(/etc /bin /usr/bin /usr/lbin /usr/usb /sbin /usr/sbin).each do |dir|
Dir.glob("#{dir}/**").each do |c|
s = File.stat("#{c}")
puts s
end
end
And here is my return:
#<File::Stat:0x7f2407a9e908>
test.rb:3:in `stat': No such file or directory - /usr/bin/chef-zero (Errno::ENOENT)
from test.rb:3
from test.rb:2:in `each'
from test.rb:2
from test.rb:1:in `each'
from test.rb:1

how to read file using path in ruby by function IO.readlines("path")[0]

i want to read first line of file by using following function in ruby
IO.readlines("path")[0]
But file is not in current directory, so i use path there
puts IO.readlines("Home/Documents/vikas/SHIF.doc")
but it is giving error as
a1.rb:1:in `readlines': No such file or directory # rb_sysopen - Home/Documents/vikas/SHIF.doc (Errno::ENOENT)
from a1.rb:1:in `<main>'
You can also open a file and read only the first line instead of the entire file
File.open("Home/Documents/vikas/SHIF.doc").readline
You can use File.expand_path:
puts IO.readlines(File.expand_path("Home/Documents/vikas/SHIF.doc", __FILE__))
Note however that it will create path relatively to a file directory, not to a root directory.
If you are using rails, you could use:
puts IO.readlines(Rails.root.join 'Home', 'Documents', 'vikas', 'SHIF.doc')

Unexpected behaviour with a file in Ruby on Rails

While trying to upload a file in Ruby on Rails, I ran into an issue.
Here is how I upload a file:
def upload_image(image)
File.new(Rails.root.join('assets','images','products',image.original_filename),'wb') do |f|
f.write(image.read)
end
end
Which throws an exception:
Errno::ENOENT in ProductsController#update
No such file or directory - /home/alex/RubymineProjects/psg/assets/images/products/my-image.png
Why is this happening? I'm just creating a new file, I'm not trying to open an existing one.
It does not create directories.
File.new("test", 'wb') #=> creates the file test
File.new("test/test", 'wb') #=> test.rb:1:in `initialize': No such file or directory - test/test (Errno::ENOENT)
If you add an /app you have the path you are looking for. Don't really think thats the way to use the asset pipeline though. See reasoning in this question.
File.open(Rails.root.join('app','assets','images','test.jpg'),'wb') do |f|
f.write("image")
end
=> 5
cat app/assets/images/test.jpg #=> image%

No such file or directory

This is the error.
Atrosity [ Eric-Raios-MacBook ][ ~/dev/rubyscripts ]$ ruby script.rb
script.rb:7:in `read': No such file or directory - sent (Errno::ENOENT)
from script.rb:7:in `lSent'
from script.rb:16:in `<main>'
My Method that is causing the error is:
def lSent
$sent = Set.new(File.read("sent").split(";"))
end
lSent
If I delete this, my script runs but does not output what I want to do.
sent should be a path to a file in your server, such as
$sent = Set.new(File.read("/root/path/file.txt").split(";"))
You're attempting to read a file called "sent", but it doesn't exist in the application's path. Try including the full path to the file.

Resources