bash命令可以打开一个命令或另一个命令

Pou*_*del 3 linux bash terminal makefile

我正在使用两种不同的操作系统.

  • 对于MacOS:open file.pdf(它在Mac中的默认pdf程序中打开pdf)
  • 对于Linux:xdg-open file.pdf(它在Linux中这样做)

如果我交换命令它不起作用.是否有任何单行命令集如下:

  • open file.pdf或xdg-open file.pdf

我想要一个适用于它们的命令(或命令)而不显示任何错误.我需要此命令的地方是make文件.

我有这样的makefile:

all:
open file.pdf
xdg-open file.pdf
other things ..
Run Code Online (Sandbox Code Playgroud)

在makefile中,如何确保make命令在Mac和Linux中都能正常运行?

感谢@rubiks,现在代码工作正常.代码如下所示:

# set pdfviewer for linux and unix machines
####################################################

UNAME_S := $(shell uname -s)

$(info $$UNAME_S == $(UNAME_S))

ifeq ($(UNAME_S),Linux)
PDFVIEWER := xdg-open
else ifeq ($(UNAME_S),Darwin)
PDFVIEWER := open
else

$(error unsupported system: $(UNAME_S))
  endif
$(info $$PDFVIEWER == $(PDFVIEWER))
####################################################
# open the pdf file
default: all
    $(PDFVIEWER) my_pdf_filename.pdf    
Run Code Online (Sandbox Code Playgroud)

Pre*_*ays 7

我认为最好更智能地执行此操作并确定您所使用的操作系统:

if [ `uname` == "Darwin" ]; then
  open file.pdf
else
  xdg-open file.pdf 
fi
Run Code Online (Sandbox Code Playgroud)

如果您尝试配置终端以便始终可以使用相同的命令,我建议将其添加到.bashrc或.bash_profile:

if [ `uname` == "Linux" ]; then
   alias open=xdg-open
fi
Run Code Online (Sandbox Code Playgroud)

这样,您始终可以使用该命令open,它将适用于任一操作系统.