簡體   English   中英

bash命令可以打開一個命令或另一個命令

[英]bash command to either open one command or another command

我正在使用兩種不同的操作系統。

  • 對於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 ..

在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    

我認為最好更智能地執行此操作並確定您所使用的操作系統:

if [ `uname` == "Darwin" ]; then
  open file.pdf
else
  xdg-open file.pdf 
fi

如果您嘗試配置終端以便始終可以使用相同的命令,我建議將其添加到.bashrc或.bash_profile:

if [ `uname` == "Linux" ]; then
   alias open=xdg-open
fi

這樣,您始終可以使用open命令,它可以在任一操作系統上運行。

如果您不介意調用shell,可以在Makefile查詢系統並相應地設置PDFVIEWER

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))            

您可以嘗試使用which返回第一個可用的可執行文件,例如

open  := $(shell which open xdg-open | head -n1)
xargs := $(shell which gxargs xargs  | head -n1) # Another example.

然后運行它:

$open file.pdf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM