BhishanPoudel
BhishanPoudel

Reputation: 17164

bash command to either open one command or another command

I am using two different OSes.

If I interchange the command it doesnot work. Is there any single line set of commands like:

I want a command (or commands) that works for both of them without showing any error. The place where I need this command is in make file.

I have a makefile like this:

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

Here, inside the makefile how can I be sure that make commands run without error both in Mac and in Linux?

Thanks to @rubiks, now the code works fine. The code looks like this:

# 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    

Upvotes: 3

Views: 386

Answers (3)

kenorb
kenorb

Reputation: 166765

You may try to use which to return first available executable, e.g.

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

then run it as:

$open file.pdf

Upvotes: 0

therealneil
therealneil

Reputation: 5320

If you don't mind invoking the shell, you can query the system from within the Makefile and set PDFVIEWER accordingly:

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

Upvotes: 1

PressingOnAlways
PressingOnAlways

Reputation: 12356

I think it is better to do this a little more intelligently and determine the OS you are on:

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

If you are trying to configure your terminal so that you can always use the same command, I would recommend adding this to your .bashrc or .bash_profile:

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

This way, you can always use the command open and it will work on either OS.

Upvotes: 7

Related Questions