在rake中调用bash别名

Mat*_*her 5 rake

我的.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'
Run Code Online (Sandbox Code Playgroud)

我想在我的Rakefile中执行'mfigpdf'命令:

desc "convert all images to pdftex (or png)"
task :pdf do
  sh "mfigpdf"
  system "mfigpdf"
end
Run Code Online (Sandbox Code Playgroud)

但这些任务都没有奏效.我可以在rakefile中复制命令,将它插入一个shellscript文件中,但是我有重复的代码.

谢谢你的帮助!

马蒂亚斯

Aus*_*lor 5

这里有三个问题:

  • 您需要source ~/.profile或在子shell中存储别名的任何位置.
  • 您需要调用shopt -s expand_aliases以在非交互式shell中启用别名.
  • 您需要在与实际调用别名的单独行中执行这两项操作.(由于某种原因,即使使用分号,设置expand_aliases也不适用于同一行输入的别名.请参阅此答案.)

所以:

system %{
  source ~/.profile
  shopt -s expand_aliases
  mfigpdf
}
Run Code Online (Sandbox Code Playgroud)

应该管用.

但是,我建议使用bash函数而不是别名.所以你的bash将是:

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
}
Run Code Online (Sandbox Code Playgroud)

还有你的红宝石:

system 'source ~/.profile; mfigpdf'
Run Code Online (Sandbox Code Playgroud)

该函数的行为与交互式shell中的别名基本相同,并且在非交互式shell中更容易调用.


Fer*_*ido 1

您必须获取 .bashrc 来加载该别名,但我认为 ruby​​ 在 sh 上运行,它不使用 source 命令,而是使用“.”。命令。我相信这应该有效:

`. /path/to/.bashrc`