Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking bash aliases in rake

Tags:

rake

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

like image 944
Matthias Guenther Avatar asked Feb 12 '11 14:02

Matthias Guenther


1 Answers

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.

like image 175
Austin Taylor Avatar answered Sep 27 '22 23:09

Austin Taylor