我有一个红宝石脚本,它需要运行bash builtin command - shopt来删除除少数文件和文件夹之外的所有文件和文件夹。下面是我面临问题的代码段。
class Test1
def initialize(hostname, user, password)
begin
@hostname = hostname
@username = user
@password = password
@ssh = Net::SSH.start(@hostname, @username, :password => @password)
@rm_cmd = "shopt -s extglob; rm -rf !(file1.zip|dir1|dir2|dir3)"
cmd = @ssh.exec!(@rm_cmd)
puts "#{cmd}"
rescue => e
puts e
end
end
end
#initailizing the object
Test1.new("ABC", "user1", pass1")它能够建立到服务器的连接,但是看起来它无法执行@rm_cmd,而rescue块并没有捕捉到它。puts "#{cmd}"打印以下错误消息:
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `shopt -s extglob; rm -rf !((file1.zip|dir1|dir2|dir3)'我试图在括号前给出转义字符,即shopt -s extglob; rm -rf !\(file1.zip|dir1|dir2|dir3\),但它也有效。有人能帮我做更多的调试和工作吗?谢谢!
发布于 2015-03-09 20:21:27
尝试转义命令,使用中的shellwords扩展,并将extglob指定为bash选项(更好的原因是远程用户可以拥有其他shell):
require 'net/ssh'
require 'shellwords'
ssh = Net::SSH.start('remote-server', 'user', password: 'password')
command = Shellwords.escape('ls !(Projects|Downloads)')
p ssh.exec!(%Q{/bin/bash -O extglob -c #{command}})https://stackoverflow.com/questions/28947627
复制相似问题