Change the directory in the fish function and return to the original directory after interruption

I am switching from bash to fish, but I have problems porting to a convenient function that I often use. The point of this function is to run make from the root directory of my source tree, regardless of the directory in which my shell is located.

In bash, it was simple:

function omake {(
  cd $SOURCE_ROOT;
  make $@;
)}

Since fish have no subshells, the best I could do was:

function omake
    pushd
    cd $SOURCE_ROOT
    make $argv
    popd
end

This works, but with the caveat that after interrupting the fish version using ^ C, the shell is still in $ SOURCE_ROOT, but interrupting the bash version brings me back to the source directory.

Is there a way to write a script that works identically to bash alone in fish?

+4
source share
3 answers

:

function omake
    echo "cd $SOURCE_ROOT; and make \$argv" | fish /dev/stdin $argv
end

: Ctrl-C cmd

echo (cd /tmp; and sleep 15)

pid :

function omake
    pushd dir1
    make $argv &
    popd
end

, make Ctrl-C, kill %make

+3

make , -C:

function omake
    make -C $SOURCE_ROOT $argv
end

-C. : , git

, , script , , , Python C, .

:

#!/usr/bin/env fish
cd $SOURCE_ROOT
and exec make $argv

, , -C env! Env ; , ! cdrun script, :

#!/usr/bin/env fish
cd $argv[1]
and exec env $argv[2..-1]

#!/bin/sh
set cd="$1"
shift
cd "$cd" &&
exec env "$@"

cdrun, omake , :

function omake
    cdrun $SOURCE_ROOT make $argv
end

omake() {
    cdrun "$SOURCE_ROOT" make "$@"
}
0

GNU coreutils, env ( user2394284).

env -C foo pwd

pwd foo. , ,

:

dir . cd- , , ; , .

0
source

Source: https://habr.com/ru/post/1532707/


All Articles