user3648577 user3648577 - 1 month ago 13
Ruby Question

Ruby code has syntax error, unexpected keyword_end, expecting end-of-input

When I run the ruby sftp file sync code bellow, I get:

box_lin.ru:46: syntax error, unexpected keyword_end, expecting end-of-input

I've added end's in various places, and cannot find the syntax problem with quotes or parentheses. Help is greatly appreciated!

require 'net/ssh'
require 'net/sftp'
require 'dir'

local_path = '/Users/awesome/Development/box'
remote_path = '/home/awesome/box'
file_perm = 0644
dir_perm = 0755

puts 'Connecting to box...'
Net::SSH.start('server', 'username', 'password') do |ssh|
ssh.sftp.connect do |sftp|
puts 'Checking for files which need updating'
Find.find(local_path) do |file|
next if File.stat(file).directory?
local_file = "#{dir}/#{file}"
remote_file = remote_path + local_file.sub(local_path, '')

begin
remote_dir = File.dirname(remote_file)
sftp.stat(remote_dir)
rescue Net::SFTP::Operations::StatusException => e
raise unless e.code == 2

sftp.mkdir(remote_dir, :permissions => dir_perm)
end

begin
rstat = sftp.stat(remote_file)
rescue Net::SFTP::Operations::StatusException => e
raise unless e.code == 2
sftp.put_file(local_file, remote_file)
sftp.setstat(remote_file, :permissions => file_perm)
next
end

if File.stat(local_file).mtime > Time.at(rstat.mtime)
puts "Copying #{local_file} to #{remote_file}"
sftp.put_file(local_file, remote_file)
end
end
end

puts 'Disconnecting from box...'
end
end

puts ' synch complete! '

Answer

In this line:

puts ‘Disconnecting from box...'

You have a tick instead of quote.

You should also put this into a code review to make it cleaner.

 

Comments