在 rake 中调用 bash 别名 [英] Invoking bash aliases in rake
问题描述
我的 .bashrc 中有以下命令:
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'
我想在我的 Rakefile 中执行mfigpdf"命令:
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
但是这些任务都不起作用.我可以将命令复制到 rakefile 中,然后将其插入到 shellscript 文件中,但是我已经复制了代码.
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.
感谢您的帮助!
马蒂亚斯
推荐答案
这里有三个问题:
- 您需要在子 shell 中
source ~/.profile
或存储别名的任何位置. - 您需要调用
shopt -s expand_aliases
以在非交互式 shell 中启用别名. - 您需要在与实际调用别名的单独行上执行这两项操作.(出于某种原因,即使您使用分号,设置 expand_aliases 也不适用于同一行输入上的别名.请参阅 这个答案.)
- 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.)
所以:
system %{
source ~/.profile
shopt -s expand_aliases
mfigpdf
}
应该可以.
但是,我建议使用 bash 函数而不是别名.所以你的 bash 将是:
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
}
还有你的红宝石:
system 'source ~/.profile; mfigpdf'
该函数的行为方式与交互式 shell 中的别名基本相同,并且在非交互式 shell 中更容易调用.
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.
这篇关于在 rake 中调用 bash 别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!