Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash command to either open one command or another command

I am using two different OSes.

  • for MacOS : open file.pdf ( it opens pdf in default pdf program in Mac)
  • for Linux : xdg-open file.pdf ( it does so in Linux)

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

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

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    
like image 493
BhishanPoudel Avatar asked May 02 '26 13:05

BhishanPoudel


1 Answers

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.

like image 144
PressingOnAlways Avatar answered May 04 '26 03:05

PressingOnAlways