5

I have the following command in my .bashrc:

alias mfigpdf='for FIG in *.fig; do fig2dev -L pdftex "$FIG" "${FIG%.*}.pdftex"; done;
                 for FIG in *.fig; do fig2dev -L pstex_t -p "${FIG%.*}.pdftex" "$FIG" "${FIG%.*}.pdftex_t"; done'

And I want to execute the 'mfigpdf' command in my Rakefile:

desc "convert all images to pdftex (or png)"
task :pdf do
  sh "mfigpdf"
  system "mfigpdf"
end

But none of theses tasks is working. I could just copy the command in the rakefile of insert it in a shellscript file, but than I have duplicated code.

Thanks for your help!

Matthias

Matthias Guenther
  • 1,643
  • 2
  • 15
  • 29
  • 1
    Why not declare this alias as a function in rake file? You will have the full power of ruby to your disposal. Sorry, I do everything in ruby and I see you are using rake that's why couldn't help but ask you. – Art Shayderov Mar 14 '11 at 16:27

3 Answers3

5

There are three problems here:

  • You need to source ~/.profile, or wherever your aliases are stored, in the subshell.
  • You need to call shopt -s expand_aliases to enable aliases in a non-interactive shell.
  • You need to do both of these on a separate line from the actual call to the alias. (For some reason, setting expand_aliases doesn't work for aliases on the same line of input, even if you use semicolons. See this answer.)

So:

system %{
  source ~/.profile
  shopt -s expand_aliases
  mfigpdf
}

Should work.

However, I would recommend using a bash function rather than an alias. So your bash would be:

function mfigpdf() {
  for FIG in *.fig; do
    fig2dev -L pdftex "$FIG" "${FIG%.*}.pdftex"
  done
  for FIG in *.fig; do
    fig2dev -L pstex_t -p "${FIG%.*}.pdftex" "$FIG" "${FIG%.*}.pdftex_t"
  done
}

And your ruby:

system 'source ~/.profile; mfigpdf'

The function will behave basically the same way as the alias in an interactive shell, and will be easier to call in a non-interactive shell.

Community
  • 1
  • 1
Austin Taylor
  • 5,437
  • 1
  • 23
  • 29
  • This is a great answer, but what is still missing is the maintenance of the shell scope. Every shell cmd using an alias has to happen on one line, and without other ruby in-between. – New Alexandria Jan 06 '12 at 17:34
3

sh mfigpdf will try to run a shell script with that name, you have to use sh -c mfigpdf instead.

You also have to force bash into "interactive shell" mode with the -i flag in order to enable alias expansion and to load ~/.bashrc.

sh "bash -ci 'mfigpdf'"

You can replace your alias with a bash function. Functions are also expanded in non-interactive mode, so you could just source ~/.bashrc instead:

sh "bash -c '. ~/.bashrc ; mfigpdf'"
Dan Berindei
  • 7,054
  • 3
  • 41
  • 48
1

You have to source your .bashrc to load that aliases, but I think ruby runs on sh that doesnt use the source command but the '.' command.I believe this should work:

`. /path/to/.bashrc `

Fernando Diaz Garrido
  • 3,995
  • 19
  • 22