【问题标题】:bash command to either open one command or another commandbash 命令打开一个命令或另一个命令
【发布时间】:2015-11-09 18:01:29
【问题描述】:

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

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

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

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

我想要一个对它们都有效且不会显示任何错误的命令(或多个命令)。 我需要这个命令的地方是在 make 文件中。

我有一个这样的生成文件:

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    

【问题讨论】:

    标签: linux bash terminal makefile


    【解决方案1】:

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

    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,它可以在任一操作系统上运行。

    【讨论】:

      【解决方案2】:

      如果您不介意调用 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))            
      

      【讨论】:

      • 这对我有用。感谢@rubiks。如果我有超过 15 分,我会为你投赞成票。
      • @BhishanPoudel 一切都很好——我很高兴你有一些适合你的东西! :-)
      【解决方案3】:

      您可以尝试使用which 返回第一个可用的可执行文件,例如

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

      然后运行它:

      $open file.pdf
      

      【讨论】:

        猜你喜欢
        • 2017-07-19
        • 1970-01-01
        • 1970-01-01
        • 2012-08-20
        • 2014-06-10
        • 2022-01-24
        • 2020-10-09
        • 2014-04-02
        • 1970-01-01
        相关资源
        最近更新 更多