What I’m trying to do
I’m timidly trying the CLI. Currently I just want to add some text to the end of some files in my vault.
The script I’m using is:
#!/bin/zsh
# This script appends to all notes (except Clippings) a transcluded site license.
# Uses the Obsidian CLI.
obsidian files folder="CPB" ext="md" | while read -r f; do
if [[ "$f" =~ ^CPB\/ALL\ PAGES\/Clippings ]]; then
continue
fi
obsidian append file="$f" content="\n![[Site License]]\n"
done
If I replace the call to the CLI with something like echo “$f” then the script will produce a list of every file I want to alter. So far so good.
But if I put in the call to obsidian then only a single file (the first one it finds) is processed, then the script exits.
Things I have tried
I have looked everywhere I can think of for a solution or explanation and can’t find one.
I’m running Obsidian 1.12.7, fully updated, on MacOS Tahoe 26.5.
Can someone help?
The cause isn’t the CLI itself, but rather how UNIX pipes and processes interact.
obsidian files folder="test" ext="md" returns a stream of bytes to the stdout
read -r f reads this stream by chunks (lines)
- but
obsidian append … shares the same stdin as read and drains it to the last byte
- when execution flows to the next loop iteration -
stdin is empty and loop exits
The solution is to give obsidian append … its own empty stdin (/dev/null):
#!/bin/zsh
obsidian files folder="test" ext="md" | while read -r f; do
obsidian append file="$f" content="\n![[Site License]]\n" < /dev/null
done
Well, I’ll be… thanks for explaining that.
In hindsight, it makes sense; should have seen it myself.
Now the script stops at an arbitrary file and never goes further, but I’ll assume that’s another problem and try to figure it out myself.
Thanks again.
Quick followup:
The script is running, but it stops after a couple of dozen files - no where near the total number of files it should find.
There’s nothing particular about the next file that the script should process compared to those it does before just stopping.
If anyone has any idea even how to try to diagnose this, I’d appreciate it.
Let’s try to catch this error. I suspect that CLI returns an error on a particular file. But it doesn’t explain why the whole process stops - it should just continue with the next iteration loop. Thus we have to make two changes.
First - add debug logs. Maybe CLI returns something useful. I’m not sure if it writes to stdout or stderr so let’s check both and print a message if append failed:
obsidian append file="$f" content=$'\n![[Site License]]\n' </dev/null >>append.out 2>>append.err || print -u2 "rc=$? $f"
Then, let’s check why script exits because it’s not a default behavior:
setopt | grep -i errexit
trap
functions TRAPZERR
if any of this returns something - that’s could be an explanation.
I’m gonna need some time to get to this; other stuff is piling up.