0

I have a shell script that creates a function (in a bashscript file which is sourced by .bashrc) for each Markdown files to open it in the editor.

Example:

ls /home/nikhil/Notes/Studies
Physics.md
Chemistry.md
Studies.md
index.md

ls /home/nikhil/Notes/Sports
Football.md
Cricket.md
index.md

As you can see, some folder (such as Studies) contains a file with the same name (Studies.md) and index.md, while others (like Sports) contain index.md but not Sports.md

Now, my bashscript creates following functions

oPhysics(){ ${Editor:-vim} /home/nikhil/Notes/Studies/Physics.md }
oChemistry(){ ${Editor:-vim} /home/nikhil/Notes/Studies/Chemistry.md }
oStudies(){ ${Editor:-vim} /home/nikhil/Notes/Studies/Studies.md }
oiStudies(){ ${Editor:-vim} /home/nikhil/Notes/Studies/index.md }

oFootball(){ ${Editor:-vim} /home/nikhil/Notes/Sports/Football.md}
oCricket(){ ${Editor:-vim} /home/nikhil/Notes/Sports/Cricket.md}
oiSports(){ ${Editor:-vim} /home/nikhil/Notes/Sports/index.md}

Notice that: oStudies and oiStudies exist, while only oiSports exists.

Application

Now, as a user, I would like to type oStudies or oSports (this would fail) and not oiSports, and it should automatically run oiSports when oSports function does not exists. This is because, most of the time the user is interested in opening the normal markdown files, and only when they don't exist the user would like to open index.md. The user does not remembers for which folder only index.md exists (for example Sports folder).

How can I do this? The pattern I am looking for is to run a function oiBlahBlah if oBlahBlah does not exists, given that oBlahBlah is invoked by the user.


Replies to suggestions

Making alias won't work as I don't want oStudies to execute oiStudies

Porcupine
  • 1,680
  • 2
  • 19
  • 45
  • Make an alias? `alias oSports=oiSports` – Stephen Harris Jan 24 '20 at 19:16
  • @StephenHarris: Making alias won't work as I dont want `oStudies` to execute `oiStudies`. – Porcupine Jan 24 '20 at 19:42
  • 2
    Just to comment: `EDITOR` is a fairly standard environment variable that you could use instead of `Editor`. Many programs use it unless `VISUAL` is set to some program. Just using `"$EDITOR"` in your functions and then set+export that variable in your shell's startup files would make the functions look cleaner. – Kusalananda Jan 24 '20 at 20:05

2 Answers2

1

An answer to a different problem. If the user wants to edit the main markdown file and only wants to edit the index file if it doesn't exist, why not write the function to do that? Then just have the oSports function. Refactoring to have the common code in a function.

# Internal edit markdown function
_emd(){
   if [ -e "/home/nikhil/Notes/$1/$2.md" ]
   then
       ${Editor:-vim} "/home/nikhil/Notes/$1/$2.md"
   else
       ${Editor:-vim} "/home/nikhil/Notes/$1/index.md"
   fi
}
oPhysics(){ _emd Studies Physics ; }
oChemistry(){ _emd Studies Chemistry ; }
oStudies(){ _emd Studies Studies ; }
oiStudies(){ _emd Studies index ; }
oFootball(){ _emd Sports Football ; }
oCricket(){ _emd Sports Cricket ; }
oSports(){ _emd Sports Sports ; }
oiSports(){ _emd Sports index ; }
icarus
  • 17,420
  • 1
  • 37
  • 54
1

The circumstances not obvious, but a simple solution could be:

First define every functions like this:

oStudies() { oiStudies; }

etc. then let your script override these functions where needed.

icarus
  • 17,420
  • 1
  • 37
  • 54
gabor.zed
  • 157
  • 6