Jump to content

Executing system commands from Ruby


Recommended Posts

Hello again.  :)

 

I wonder if someone could help out with a bit of Ruby scripting. I've written a workflow that deletes a list of files selected from Alfred. The problem is with the script; I can't get it to delete the files.

 


 

ext = "{query}"
 
fileNames = text.split("/t")
 
filesDeleted = ""
 
fileNames.each do  |fileName| 
 
    command = "rm -r " + fileName
 
    system(command)
 
    filesDeleted << fileName
 
end
 
print filesDeleted

 
Everything else works okay apart from the system call. 
 
Any ideas as to what I've forgotten? ...  :(
 
Link to comment

Hello again.  :)

 

I wonder if someone could help out with a bit of Ruby scripting. I've written a workflow that deletes a list of files selected from Alfred. The problem is with the script; I can't get it to delete the files.

 


 

ext = "{query}"
 
fileNames = text.split("/t")
 
filesDeleted = ""
 
fileNames.each do  |fileName| 
 
    command = "rm -r " + fileName
 
    system(command)
 
    filesDeleted << fileName
 
end
 
print filesDeleted

 
Everything else works okay apart from the system call. 
 
Any ideas as to what I've forgotten? ...  :(

 

Have you tried commenting out the system command and just using puts or print to get the exact output of command? Make sure its creating the string correctly? Also, in the command string where you are creating 'rm -r '+filename, you might ought to modify it so that the output of fileName will be in quotes when the command is run so it will still function correctly if the path has a space in it (unless you already have it escaped)

Link to comment

Hello again.  :)

 

I wonder if someone could help out with a bit of Ruby scripting. I've written a workflow that deletes a list of files selected from Alfred. The problem is with the script; I can't get it to delete the files.

 


 

ext = "{query}"
 
fileNames = text.split("/t")
 
filesDeleted = ""
 
fileNames.each do  |fileName| 
 
    command = "rm -r " + fileName
 
    system(command)
 
    filesDeleted << fileName
 
end
 
print filesDeleted

 
Everything else works okay apart from the system call. 
 
Any ideas as to what I've forgotten? ...  :(

 

You can use the back ticks to run system commands.

 

`rm -r "#{fileName}"`

 

Your problem might be spaces in filenames. Hence the double quotes.

Link to comment
  • 6 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...