How to invoke a cmd automatically when entering a certain directory?

Not EOS specific and I didn’t know where to ask.

How to invoke a cmd automatically when entering a certain directory?

I am aware that I can build something myself via functions in my shell but I was wondering if there is a tool which offers such a functionality. Like direnv does for loading and unloading environment vars definitions.

After more searching I found shadowenv (https://github.com/Shopify/shadowenv) which looks good. v2.0.7-2 is in AUR. The newest version in the github is 2.1.1

Looks interesting.

From reading the information, I was wondering if renaming a directory which you “trust” would inhibit the “script” from being executed; I suppose you would then have to “re-trust” the directory.

I’d be interested to know what your experiences are with shadowenv.

Just tested. Yes, when I move a such a directory then it is untrusted again and I have to re-trust it.

Hm, it seems that also shadowenv doesn’t allow to issue commands when entering a directory.

Thanks for testing.
Something to be aware of, then.

As in: you can’t enter a command on the command line?

:slight_smile:
It seems the easiest way for me, I am in fish shell, is to have a function like this

  function __svn_hook --on-variable PWD
    if test "$PWD" = "/my_important_dir"
      issue_my_cmd
    end
  end

I use direnv for this in some projects. Here is the snippet I use to conditionally load environment settings in my direnv .envrc. Since this is all shell commands, I would assume you can issue any command you wish via a a direnv .envrc

# The following is taken from
# https://github.com/direnv/direnv/issues/320#issuecomment-388341274
# This will watch the file name `.context`. If that file changes, it
# will read the content and will look for additional envrc files to
# source. For example `echo staging > .context`, will load the files
# .envrc.staging and .envrc.staging.secrets if they exist.
# Delete the .context fileert to the original settings
context=
[[ -f .context ]] && context=$(< .context)
watch_file ".context"

if [[ -n "$context" ]]; then
  echo "Loading context variables and secrets"
  # load the reguler variables
  context_file=".envrc.$context"
  if [[ -f "$context_file" ]]; then
   echo "  Loading context $context"
   source_env "$context_file"
  else
    echo "  $context_file missing"
  fi
fi

Hm, in my situation it is not about env vars. It is about issuing commands.

Let’s say I go into a directory and want to automatically run svn update. Can this be accomplished with direnv?

There might be several ways to do this, but these come to my mind now:

  • redefine the cd command as a function
  • on bash you can set the PROMPT_COMMAND variable to a bash function that does what you want

If this is already what you have considered, please ignore. :wink:

Edit: maybe you could add a folder specific file (for example, .my_cd_commands) into certain folders. The file could execute some command(s) when you cd to them. And cd should be a function that checks if the special file exists in the target, and executes it.

Edit2: re-read your posts and seems that you have already thought of these.

100%. Here is an example using git rather than svn but it should be the same.
I ran:

mkdir envtest
cd envtest
git init .
echo "git status" > .envrc
direnv allow

now when I cd to it, it runs the git status command.

output:

% cd envtest
direnv: loading ~/Code/envtest/.envrc
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.envrc

nothing added to commit but untracked files present (use "git add" to track)

% cat .envrc
git status

Using direnv in this way may be a little unorthodox, but if it serves your need.

1 Like

Thanks a lot @opennomad .

It is amazing how thinking gets blocked (at least in my mind it got blocked) when reading env in direnv and .envrc.

Of course, you are right.

The point is that direnv just does this

  • when entering the directory where an .envrc file exists
  • it saves the environment variables status
  • it just invokes .envrc and doesn’t care what it contains
  • it compares the new environment variables status so that in case of differences it knows exactly what to remove when I leave the directory.
1 Like

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