1

Suppose I have two scripts, script1.sh and script2.sh. I am wondering if there is a way to make a filesystem interface such that, for example, I can go vim file and then have my system run script1.sh and have the output from the script inside my editor. Then, when I write the file, the system would send the modified text as piped input into script2.sh.

Is this possible? I've looked into using 'inotify', which could run my desired script when the file was changed. But I haven't figured out how to do the first part yet, where opening the file itself just gets the standard output from some script.

2 Answers 2

0

The tool for this job is FUSE (FUSE stands for Filesystem in USErspace). FUSE allows a filesystem to be implemented by a user program. If you run vim file and file is on a FUSE filesystem, then the program providing the filesystem will generate the file's content when Vim reads from it, and store (or not store!) the content when Vim saves the file.

FUSE is more elaborate than what you're asking for. The program providing the filesystem not only manages the file's content, but also directory structure, permissions, etc. It also needs to manage reads and writes of a chunk of each file, unless you make the files non-seekable (which will restrict the programs that can work on them).

I'm not aware of any project that builds on FUSE to provide just an interface to manipulate file contents while providing a classical directory structure. There are example filesystems that you can copy code from.

0

This shell function will do what you ask:

vim() { bash script1.sh <"$1" >"$1.hide~"; /usr/bin/vim "$1.hide~"; bash script2.sh <"$1.hide~"; }

Use this function as follows:

vim file

Breaking it down into pieces:

  • bash script1.sh <"$1" >"$1.hide~"

    $1 is the first argument that you supply to the script which should be the name of a file. This runs 'script1.sh with that file on its stdin and writes the output to a temporary file.

  • /usr/bin/vim "$1.hide~"

    This runs the real vim on the output of script1.sh.

    This assumes that the vim editor on your system is located in /usr/bin. If it is located somewhere else, adjust the function definition appropriately.

  • bash script2.sh <"$1.hide~"

    This supplies the edited file as stdin for script2.sh.

To make this function definition permanent, place it as a separate line in your ~/.bashrc file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.