Executing shell command from ruby console returning Permission Denied Error?

Getting permission denied error while executing shell command from ruby console. And the same shell command is working from shell.

From Shell..

[email protected]:~$ "`grep '^datadir=' /etc/mysql/my.cnf | cut -f 2 -d '='`/db_backups" bash: /db_backups: is a directory [email protected]:~$

From ruby console..

>> %x["`grep '^datadir=' /etc/mysql/my.cnf | cut -f 2 -d '='`/db_backups"] sh: /db_backups: Permission denied => ""

Any Idea !

-------------Problems Reply------------

You're trying to execute a directory and the shells are saying no; bash says no by saying "/db_backups: is a directory" whereas sh says "/db_backups: Permission denied". If you just execute the backedticked part:

grep '^datadir=' /etc/mysql/my.cnf | cut -f 2 -d '='

You'll almost certainly see no output at all and the reason is probably that your regular expression is too tight, something like this:

grep '^[ ]*datadir[ ]*=' /etc/mysql/my.cnf | cut -f2 -d'='

Would serve you better; the character classes contain a space and a tab.

Now that you're looking for the right things we can move on to why it still won't work. The %x[] quoter tries to execute its argument using the shell. When you feed the backticked grep stuff:

`grep '^[ ]*datadir[ ]*=' /etc/mysql/my.cnf | cut -f2 -d'='`/db_backups

to the shell, you should get a directory name that ends with /db_backups but you can't execute a directory. I think you want this to produce the directory name:

d = %x[echo `grep '^[ ]*datadir[ ]*=' /etc/mysql/my.cnf | cut -f2 -d'='`/db_backups].strip

Note the leading echo and the .strip call on the returned string. The .strip is necessary to remove the newline from what echo produces.

I think you're going through a lot of trouble for something that could easily be done with just a couple lines of Ruby:

dir = nil
File.open('/etc/mysql/my.cnf').each do |line|
if(m = line.match(/^\s*datadir\s*=\s*(\S+)/))
dir = m[1] + '/db_backups'
break
end
end

You could probably tighten that up a bit if you wanted but I think that that's at least less confusing than putting shell backticks inside Ruby backticks.

It looks like you just want to get field 2 from the file. Then just do it in Ruby using split

File.open("file").each do |line|
if line[/^datadir/]
print line.split("=",2)[0]
end
end

There is no need to specifically shell out to call grep. This is inefficient and non-portable

Category:ruby on rails Views:0 Time:2011-03-28

Related post

  • Executing shell command from ruby using the same shell process 2011-11-17

    In Ruby, is there a way to execute multiple shell commands forcing them to use the same shell process? For example, something that would make `` (or system or popen) to behave like: $ irb > `echo $$` => "999\n" > `echo $$` => "999\n" ----

  • executing shell command from ruby 2010-10-25

    This isn't working in ruby and I don't understand why? #!/usr/bin/env ruby exec "sort data.txt > data.sort" data.txt is a comma sepparated file. Anyway.. If I try to execute that line in the shell it works without a problem. When I try to run this

  • Execute shell command from ruby script 2011-11-28

    I'm trying to do something like this listen_for /turn on the living room lights/i do system('/usr/bin/tdtool -n 1') say "Your lights is now turned on" request_completed end but the command inside system does not run, instead I get the following messa

  • Cannot execute RVM shell commands in ruby 2011-08-02

    Long time lurker, first time poster! Goal My ultimate goal is to make a Rake setup script to setup my rvm environment stuff (I need to dynamically create gemsets, install gems to those gemsets, and run ruby scripts within those gemsets). Problem I ne

  • Ruby: execute shell command 'echo' with '-n' option 2011-02-20

    I'd like to run the following shell command from Ruby, which copies a string into the clipboard (on OS X), 'n' is suppressing the line break after the string caused by echo: echo -n foobar | pbcopy —> works, fine, now the clipboard contains "fooba

  • How to execute shell command in C? 2011-05-12

    I am having problems executing the shell commands in C. I want to execute a shell command in C then capture the output of shell command and process the output further. I used following code to perform the functionality. But, the issue is when the she

  • "bad interpreter: Permission denied " error while trying to execute rails commands 2011-09-14

    I am using ubunutu natty narwhal.I had installed ruby,rails,rvm etc.. sometime back and everything was running peacefully.Recently i tried to execute "rails -v" on the command line i get this frustrating error. [email protected]:~$ rails -v b

  • How to execute sql command and read its return value when the column has no name 2011-12-27

    I've been using SqlDataReader to read results from my querys to an SQL server db. SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine(rdr["ColumnName"].ToString()); } But now I want to create a Job in my C# program, and wh

  • php executing shell command gives random output in only in heavy use mode 2012-03-28

    Please help me with this issue that I am facing on production server. I launched an application that saves images in a directory only after checking that it does not already exist. To check if it already exists I used the following command find $file

  • Execute shell command from within MySQL client? 2009-05-30

    In oracle database, command "host" can be used to run bash command from database command window. Is there a equivalent command as "host" in mySql? --------------Solutions------------- You can use the system command. system command, \! command Execute

  • Executing shell commands using PHP, e.g. shell_exec() etc., on a remote host? 2010-09-19

    Is it possible to execute shell commands on a remote computer (not localhost)? For instance things like $output = shell_exec("unzip filename.zip"); You can assume that you have the login credentials of a user account on the remote machine, as well as

  • CTS_ERROR >>> Failed to execute shell command am instrument 2010-11-29

    Using the Android 2.2, API-8, SDK-r7 along with CTS-2.2_r4 suite. Updated the SDK_ROOT environment variable with SDK_r7 tools in “android-cts/tools/startcts” script and the “SDK_ROOT/tools” also included in PATH environment variable. Ran the “android

  • How to execute shell commands in android? 2011-07-31

    I am trying to execute shell command through my code for adding entry in Iptables. The following is my piece of code private void createShellCommand(String uidValue_param, String interface_param) { // TODO Auto-generated method stub StringBuilder cmd

  • Execute shell command in c++ 2011-08-29

    I have a question regarding executing shell commands in c++. I'm building an application in winforms, vs 2008. My application has a button, when clicked should decode a binary file to a .csv file. I can decode files by first going to the right direct

  • python: os.system does not execute shell command 2012-03-02

    I need to execute shell command in python program (I have ubuntu). More specifically I want to create graph using graphviz in python script. My code is os.system("dot -Tpng graph.dot -o graph.png") It does not work, but if I just type "dot -Tpng grap

  • Shell script in bash on Windows permission denied 2013-05-30

    Following the instructions from http://www.techsutram.com/2009/05/bash-environment-on-windows-this-is-not.html, I have successfully installed a bash shell on my Windows machine. I then put a shell script (which I know to work) in the C:\bash folder,

  • Permission denied error with RVM 2010-11-29

    I have searched for this problem and couldn't find relevant similar questions. Please bear with me if this is repetitive. I have followed guides in RVM website to install RVM and I have installed rubies: [email protected]:~$ rvm list rvm rubies ruby-1.8.7-p

  • Permission denied error while installing gem 2011-02-26

    I am trying to install rackamole on Windows XP: S:\development\DevKit>gem install rackamole Temporarily enhancing PATH to include DevKit... Building native extensions. This could take a while... ERROR: While executing gem ... (Errno::EACCES) Permi

  • git aliases causing "Permission denied" error 2011-11-03

    The following commands $ git co -b newbranch $ git co oldbranch result in "fatal: cannot exec 'git-co': Permission denied" error. In the same time, $ git checkout -b newbranch $ git checkout oldbranch and $ sudo git co -b newbranch $ sudo git co oldb

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.073 (s). 11 q(s)