Score:0

How to run a script when accessing a specific non existing path?

us flag

I have some path I have to access, which is the result of mounting.

I would like the mounting to be automatic, via a script, and I want that script to run just before an error is thrown from not being able to access the path.

For example, assume the script is

echo scripting!
mkdir -p /non_existing_path

and I want it to run when trying to access (in any way) to the path /non_existing_path.

So when I do for example

cd /non_existing_path

or

touch /non_existing_path/my_file.txt

It would always succeed, with the output scripting!. In reality, the script would be more elaborated than that.


Is this possible at all?

Liso avatar
sd flag
You want the script to run on drive mount ?
bac0n avatar
cn flag
related: [How Do I use autofs to map a USB drive by its ID?](https://unix.stackexchange.com/questions/42260/how-do-i-use-autofs-to-map-a-usb-drive-by-its-id/276873)
us flag
@Liso no, I want the script to run just before getting the error `-bash: cd: /non_existing_path: No such file or directory.` It can be any script, and is not related to mounting
Liso avatar
sd flag
And why is that? You can prevent that with `mkdir -p` just like in your snippets. If you **really** want to make sure directory is exist, can't you just create small snippets to see *if dir not exist, then create it and run following, otherwise run following*. If that's the case, its easy to do with bash script.
us flag
@Liso as I said, the script is more elaborated. I want to mount a different directory depending on conditions, I want to log differently depending on conditions, I want to fail differently. i want the operating system to call a specific script upon accessing a specific path. Is it possible?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.