在 rake 中调用 bash 别名
我的 .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”命令:
desc "convert all images to pdftex (or png)"
task :pdf do
sh "mfigpdf"
system "mfigpdf"
end
但这些任务都不起作用。我可以复制 rakefile 中的命令并将其插入到 shellscript 文件中,但我有重复的代码。
感谢您的帮助!
马蒂亚斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里存在三个问题:
source ~/.profile
或存储别名的任何位置。shopt -s Expand_aliases
以在非交互式 shell 中启用别名。所以:
应该有效。
但是,我建议使用 bash 函数而不是别名。因此,您的 bash 将是:
而您的 ruby:
该函数的行为方式与交互式 shell 中的别名基本相同,并且在非交互式 shell 中更容易调用。
There are three problems here:
source ~/.profile
, or wherever your aliases are stored, in the subshell.shopt -s expand_aliases
to enable aliases in a non-interactive shell.So:
Should work.
However, I would recommend using a bash function rather than an alias. So your bash would be:
And your ruby:
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.
sh mfigpdf
将尝试运行具有该名称的 shell 脚本,您必须使用sh -c mfigpdf
代替。您还必须使用
-i
标志强制 bash 进入“交互式 shell”模式,以便启用别名扩展并加载~/.bashrc
。您可以用 bash 函数替换别名。功能也在非交互模式下扩展,因此您可以直接使用
~/.bashrc
来代替:sh mfigpdf
will try to run a shell script with that name, you have to usesh -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
.You can replace your alias with a bash function. Functions are also expanded in non-interactive mode, so you could just source
~/.bashrc
instead:您必须获取 .bashrc 来加载该别名,但我认为 ruby 在 sh 上运行,它不使用 source 命令,而是使用“.”。命令。我相信这应该有效:
`。 /path/to/.bashrc
`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
`