mks_env

create and manage named shell environments 

KornShell Built-in


SYNOPSIS

mks_env load [-d] [-kenv_piece] name

mks_env save [-kenv_piece] [-r shellcmd] [name]

mks_env delete name

mks_env list [-r|-t|-v] [name...]

mks_env script


DESCRIPTION

The mks_env utility is an MKS KornShell built-in that lets you save and restore shell environments using the registry. It has five sub-commands: load, save, delete, list, and script.

The save sub-command saves the environment with the specified name. This name identifies the registry key in which the environment information is stored. With the save sub-command, name is optional. When it is not specified, mks_env generates a unique name and saves the environment with that name. The save sub-command also saves a TimeStamp key that contains the equivalent of the output from date +%Y%m%d%H%M%S.

Note:

The following variables are explicitly not saved by the save sub-command:

ARGS
HISTCMD
LINENO
LINENO
LOGONSERVER
MKS_ENV_LOAD_NAME
NUMBER_OF_PROCESSORS
PPID
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
PWD
SECONDS
SHELL_VERSION
SystemDrive
SystemRoot
USERDOMAIN
USERNAME
USERPROFILE
windir

The load sub-command loads a named environment. When the load sub-command specifies a name argument, the environment variable MKS_ENV_LOAD_NAME is set to name. This lets you use that same name to re-save the environment with that same name. For example, the following script autosaves the environment every 10 commands:

mks_env load ENVNAME 		# loads environment ENVNAME
trap 'if [ $((HISTCMD%10)) == 0 ] ; then mks_env save "$MKS_ENV_LOAD_NAME" ; fi' DEBUG
mks_env save ENVNAME 		# saves environment ENVNAME with autosave DEBUG
				# trap added

You can also use the sh -R option to load a saved environment.

The delete sub-command deletes the saved environment with the specified name.

The list sub-command displays one or more of the names, time stamps, and registry paths of saved environments. When no options are specified with list, just the names of the saved environments specified by the name arguments (or all saved environments if no name arguments are given).

The script sub-command generates a script that, when sourced (with the . or source utility), recreates the current environment. This generated script is written to standard output.

With a combination of various sub-commands, the trap utility and the sh -R option, you can configure your shell to save the environment when you exit and then load that environment when you enter the shell again. You can also use these features to create specific shell environments that can be loaded on demand. This can be handy for development and quality assurance purposes.

Options

-d 

deletes the specified key after loading it.

-kenv_piece 

specifies which piece of the environment is to be saved or loaded. env_piece is a string of one or more of the following characters:

P    Position. This is all stty info,
including position
     and buffer size, but not including font information
S    Symbol Table. This includes aliases and functions. However, functions
     are only remembered if set -o nolog was not on when
     the function was defined.
T    Traps. This includes all user-defined traps.
V    Variables. This includes all environment variables, all set options,
     and the current directory.
-r 

when used with the list sub-command, displays the registry paths for the specified saved environments.

-r shellcmd 

when used with the save sub-command, specifies a shell command to be written to the HKEY_CURRENT_USER/Software\Mortice Kern Systems\Toolkit\Shell\Environments\RunOnce key. mks_env prepends wstart -D desktop -c to specified command, replacing desktop with the name of desktop in which the shell is running.

This option is particularly handy for LOGOFF traps where only shell built-ins can be run. For example:

trap 'mks_env save -r "$SHELL -R $$,SVPT,d" $$' logoff
-t 

when used with the list sub-command, displays the names and time stamps for the specified saved environments.

-v 

when used with the list sub-command, displays the names, time stamps, and registry paths for the specified saved environments.


EXAMPLES

To restore every shell and environment that you had at logout, place the following command in your profile.ksh file:

trap 'mks_env save -r "$SHELL -R $$,SVPT,d" $$" logoff

To automatically create a file that can be used to restore an environment in the case of an accidental or error-caused exit from the shell, place the following two commands in your profile.ksh file.

trap "if [ \$(( HISTCMD%10 )) == 0 ] ;then mks_env save sh_env_$$ ; fi" DEBUG
trap "mks_env delete sh_env_$$ " CLOSE EXIT LOGOFF

The first command saves your environment every 10 commands. The second command deletes the automatically saved environment upon a normal exit from the shell.

To have a certain startup at every login, get everything setup the way you like and run the following commands:

SHELL_ENV_NAME="some_unique_name"
mks_env save "$SHELL_ENV_NAME"
registry -k 'HKEY_CURRENT_USER/SoftWare/Microsoft/Windows/CurrentVersion/Run" \
    -n "$SHELL_ENV_NAME" -v'wstart -D $(desktop) -c $SHELL -R "$SHELL_ENV_NAME"'

Once you have save a few different environments, you can make a desktop shortcut pointing at:

sh -R name

For example, you might create on shortcut for the local development environment and another shortcut for a remote development environment.

To dump the contents of the saved environment named saved_env:

registry -p -k "$(mks_env list -r name)"

ENVIRONMENT VARIABLES

MKS_ENV_LOAD_NAME 

Contains the name of the latest environment loaded with the load sub-command.


DIAGNOSTICS

Possible exit status values are:

0 

Successful completion.

>0 

An error occurred.


PORTABILITY

Windows 8.1. Windows Server 2012 R2. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022.

mks_env is an MKS extension.


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
PTC Windchill Requirements and Validation


SEE ALSO

Commands:
set, sh, trap


PTC MKS Toolkit 10.4 Documentation Build 39.