Bash scripting in Obsidian: Script to rename file and move it depending on frontmatter variable

I’m using the Shell Commands plugin to do a number of things, e.g. run latexmk to convert a markdown file to PDF via LaTeX in order to render citations.

Now I’m trying to create a command that moves a file in a vault to another location in the same vault depending on frontmatter variables. E.g. if it has Medium: Book it moves it too Book Log, and if it has Medium: Movie it moves it somewhere else.

My problem is a) that I’m not sure my syntax is correct, and b) that between the fact that I need to use plugin specific variables (e.g. {{file_name}}) and that the plugin can only deal with one-liners, I’m not sure where the problem is.

As a one-liner when I put it into the plugin it looks like this:

case {{file_name}} in 2*) : ;; *) mv {{file_path:absolute}} {{folder_path:absolute}}/{{date:YYYY-MM-DD}}\ {{file_name}} ;; esac; case {{yaml_value:Medium}} in Book | Audiobook | Podcast | eBook | GraphicNovel) mv {{file_path:absolute}}  {{vault_path}}/Book\ Log/{{date:YYYY}}/{{date:MM}}/{{file_name}} ;; Movie | TV | Game) mv {{file_path:absolute}}  {{vault_path}}/Movies\ and\ Games/{{date:YYYY}}/{{date:MM}}/{{file_name}};; esac

with line-breaks it looks like this:

case {{file_name}} in 
    2*) : ;; 
    *) mv {{file_path:absolute}} {{folder_path:absolute}}/{{date:YYYY-MM-DD}}\ {{file_name}} ;;  
esac; 

case {{yaml_value:Medium}} in 
Book | Audiobook | Podcast | eBook | GraphicNovel) mv {{file_path:absolute}} {{vault_path}}/Book\ Log/{{date:YYYY}}/{{date:MM}}/{{file_name}} ;; 
Movie | TV | Game) mv {{file_path:absolute}}  {{vault_path}}/Movies\ and\ Games/{{date:YYYY}}/{{date:MM}}/{{file_name}};; 

esac; exit; 

exec bash

This script is meant to do two things: rename the file with the current date IFF it doesn’t already have a date prefixed to it, and move it to its correct location.
In the current above version it does either but not both upon a single execution. I.e. if the file starts with anything but a 2* it prefixes the date. If and only if it already has a date (either from manually putting it there or a first execution of this command) it then moves that file to its correct location. I’d like this to happen in a single step.

My suspicion is that somehow the mv step after the first esac is being parsed as part of the second case but I can’t figure out how to prevent that.

Any advice appreciated!
Thanks :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.