Skip to content

Construct context aware function on top of L_finally.

do_stuff_in_temporary_directory() {
  L_with_cd_tmpdir
  echo 123 > tmpfile
  # cd to previous directory and remove tmpdir automatically both on exit and on function return
}

temporary_cd_to_tmp() {
  L_with_cd /tmp/
  echo now in tmp > tmpfile
}

Generated documentation from source:

with

What we can do with finally? We can do destructors.

L_with_cd

Change to given directory.

Register RETURN trap for parent function that will restore current working directory.

Arguments:

  • $1 Directory to cd into.
  • $2 Optional stack offset to add to RETURN trap.

L_with_tmpfile_to

Create a temporary directory.

Register RETURN trap for parent fuction that will remove the directory.

Arguments:

  • $1 Variable to assign the temporary file to.
  • $2 Optional stack offset to add to RETURN trap.

L_with_tmpdir_to

Create a temporary directory.

Register RETURN trap for parent fuction that will remove the directory.

Arguments:

  • $1 Variable to assign the temporary directory location to.
  • $2 Optional stack offset to add to RETURN trap.

L_with_cd_tmpdir

Create a temporary directory and cd into it.

Register RETURN trap that will remove the temporary directory and restore working directory on return from parent function.

Argument: $2 Optional stack offset to add to RETURN trap.

L_with_redirect_stdout_to

Temporary redirect stdout to string.

Non-forking command substition for the poor.

Arguments:

  • $1 Variable to capture stdout to.
  • $2 Optional stack offset to add to RETURN trap.