Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux bash命令打开一个命令或另一个命令_Linux_Bash_Terminal_Makefile - Fatal编程技术网

Linux bash命令打开一个命令或另一个命令

Linux bash命令打开一个命令或另一个命令,linux,bash,terminal,makefile,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

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

  • 对于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 ..
# 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    
在这里,在makefile中,我如何确保make命令在没有 Mac和Linux中都有错误

感谢@rubiks,现在代码运行良好。 代码如下所示:

all:
open file.pdf
xdg-open file.pdf
other things ..
# 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_配置文件中:

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

这对我有用。感谢@rubiks。如果我的分数超过15分,我就会放弃投票给你。@BhishanPoudel这一切都很好——我只是很高兴你得到了一些有用的东西!:-)