4

I have a command my_cmd that is aliased to another command in my ~/.bashrc. From Perl and Python, I can run this command as bash -ic "my_cmd" (this also works from the terminal window, but here it is not necessary, and I can simply type my_cmd since bash already has loaded my ~/.bashrc). When I try this from Emacs:

(print (shell-command-to-string "bash -ic 'my_cmd'") (current-buffer)))

I get error:

bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell

I am running Emacs 24.3.1 on Ubuntu 14.04.

Håkon Hægland
  • 3,648
  • 1
  • 22
  • 51

3 Answers3

3

I tried it as well. I got the errors, but I got the output of the command, too. So, a partial solution would be to redirect the errors away:

(print (shell-command-to-string "bash -ic 'my_cmd' 2>/dev/null")
       (current-buffer))

Including the redirection inside single quotes doesn't help, because it's the outer bash that reports the problems, not the command.

choroba
  • 2,045
  • 11
  • 16
3

A solution seems to be to use a login shell instead of interactive shell, according to the following answer on stackoverflow: running-system-command-under-interactive-bash-shell.

First make sure ~/.bash_profile contains:

if [ -f ~/.bashrc ]; then
    shopt -s expand_aliases
    . ~/.bashrc
fi

You can now use bash -lc instead of bash -ic:

(print (shell-command-to-string "bash -lc 'my_cmd'") (current-buffer)))
Håkon Hægland
  • 3,648
  • 1
  • 22
  • 51
0

ok so i don't know how the whole thing works. i'll try to find out interactively

googling job control gives me http://www.gnu.org/software/libc/manual/html_node/Job-Control.html#Job-Control and https://stackoverflow.com/questions/11821378/what-does-bashno-job-control-in-this-shell-mean

so what exactly does "shell-command-to-string" do? C-h f shell-command-to-string RET Then I click on the link to the file to get me source code. (Ch-f = M-x describe-function)

(defun shell-command-to-string (command)
  "Execute shell command COMMAND and return its output as a string."
  (with-output-to-string
    (with-current-buffer
      standard-output
      (process-file shell-file-name nil t nil shell-command-switch command))))

what is "shell-file-name", you have to check by your place but for me C-h v shell-file-name RET returns "bash". (C-h v is M-x describe-variable)

and for me shell-command-switch is "-c". So no -i be default.

[WRONG]So by default you start an interactive bash in a non interactive bash. So yeah, doesn't work. I don't know much about the whole thing so can't tell you how to fix it. Maybe changing shell-command-switch would help, I'd try that out. Maybe it opens a whole another can of worms ^^![WRONG]

edit:

well actually I was quite wrong!

bash -c "bash -ic 'll'" (with ll aliased to ls -l in .bashrc) actually works!

So I guess I'd need to know what you tried to run to test it at home :)

freakhill
  • 181
  • 4
  • 2
    -1 for the following reason: the answer currently concludes that it doesn't work, you don't know how to fix it, and then suggests something you haven't tried. Small, speculative suggestions are welcome in comments, but answers should answer the question. The discussion of job control, shell-command-to-string, shell-file-name, and shell-command-switch, if cleaned up, would provide the possible basis for the background to an answer. I'll remove my downvote and convert it to an upvote if you can clean up the text and provide a solution to OP's problem. – Dan Dec 11 '14 at 16:21
  • 2
    well i don't have enough rep to write comments and i think my answer helped the dude to some extent, gave him some leads. Hope he can still see it! – freakhill Dec 11 '14 at 16:39
  • If you can clean it up and condense it, I can convert it to a comment for you. – Dan Dec 11 '14 at 16:57
  • Thanks, freakhill. I also have shell-file-name equal to bash.. but I tried setting (setq shell-command-switch "-ic") but it still does not work :( – Håkon Hægland Dec 11 '14 at 17:01
  • unfortunately i cannot help you without more data there.

    bash -ic 'll' works fine at home without any problem in my macos emacs. (ll is aliased to ls -l in my .bashrc).

    I'd need to know which command you are actually trying to run.

    – freakhill Dec 11 '14 at 17:30
  • @freakhill You can use ls -l, I get that behavior for that command too :) – Håkon Hægland Dec 11 '14 at 18:09
  • It works on my setup at home on macos, even when starting emacs with no conf (-Q). I'll try it at work tomorrow on windows and centos. – freakhill Dec 11 '14 at 20:14
  • Could you also execute the command on your setup with "emacs -Q", just to be sure. – freakhill Dec 11 '14 at 20:20
  • Yeah I get the same error with "emacs -Q"... – Håkon Hægland Dec 12 '14 at 09:33
  • Putting some notes advancing on the problem.

    I could get the problem with a CentOS VM I use at work.

    SO I used edebug to see what was executed in the end.

    http://www.gnu.org/software/emacs/manual/html_node/elisp/Edebug.html#Edebug

    (call-process "/bin/bash" nil t nil "-ic" "$-")

    I use $- to verify that the program was really started as interactive and indeed it is (i get hiBHc as answer, it contains i so the shell is interactive).

    A strace -f -p EMACS_PID shows that the command seems to be correctly executed (execve bash [bash, -ic, "ls -l", ....])

    So I am here for now!

    – freakhill Dec 13 '14 at 13:13
  • http://serverfault.com/questions/411307/cannot-set-terminal-process-group-during-su-to-another-user-as-login-shell

    Only processes with a controlling terminal can have session leaders which manipulate process groups (do job control) <<

    – freakhill Dec 13 '14 at 13:22
  • So I read that a looong time ago but I guess I need a refresher ^^ http://www.gnu.org/software/libc/manual/html_mono/libc.html#toc-Job-Control-1 – freakhill Dec 13 '14 at 13:24
  • Still reading, but I fear it has something to do with selinux.

    Thing to do next! -> Try while disabling SElinux!

    – freakhill Dec 13 '14 at 13:28
  • @freakhill See my answer for a workaround.. – Håkon Hægland Jan 02 '15 at 20:25