Bash command to open one team or another team

I use two different OS.

  • for MacOS: open the .pdf file (it opens pdf in the default PDF program on Mac)
  • for Linux: xdg-open file.pdf (this is done on Linux)

If I change the command, this does not work. Is there any one set of commands, for example:

  • open .pdf file or xdg-open file.pdf

I want the team (or teams) to work for both of them without any error. The place where I need this command is in the make file.

I have a make file like this:

all:
open file.pdf
xdg-open file.pdf
other things ..

Here, inside the makefile, how can I be sure that command execution is executed without error on both Mac and Linux?

Thanks to @rubiks, now the code works fine. The code is as follows:

# 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    
+4
3

, 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))            
+2

, , :

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, .

+8

You can try using whichto return the first available executable, for example

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

then run it like:

$open file.pdf
+1
source

Source: https://habr.com/ru/post/1615096/


All Articles