44 lines
1.1 KiB
Bash
Executable File
44 lines
1.1 KiB
Bash
Executable File
#!/usr/bin/env zsh
|
|
|
|
print_help_and_exit() {
|
|
echo "Usage: list-envvars.zsh [-h] [-z] <SCRIPT>"
|
|
echo " List the environment variables exported by SCRIPT, which is run in the"
|
|
echo " process. SCRIPT must be a zsh script as it will be sourced by this script. It"
|
|
echo " also mustn't use the DEBUG trap, as this script uses it."
|
|
echo "Options:"
|
|
echo " -h Print this message, then exit"
|
|
echo " -z Use a NULL byte instead of a newline to delimit output"
|
|
exit 0
|
|
}
|
|
|
|
(( ${#} == 0 )) && print_help_and_exit
|
|
|
|
local line_delim='\n'
|
|
while getopts 'hz' OPTOPT; do
|
|
case "${OPTOPT}" in
|
|
h)
|
|
print_help_and_exit
|
|
;;
|
|
z)
|
|
line_delim='\0'
|
|
;;
|
|
esac
|
|
done
|
|
|
|
local target_script="${@[${OPTIND}]}"
|
|
|
|
local found_vars=()
|
|
handle_debug_trap() {
|
|
local cmdline=(${(Q)${(Z:C:)1}})
|
|
[[ "${cmdline[1]}" = 'export' ]] || return
|
|
found_vars+=(${${cmdline:1}/=*})
|
|
}
|
|
|
|
() {
|
|
emulate -L zsh
|
|
trap 'handle_debug_trap "${ZSH_DEBUG_CMD}"' DEBUG
|
|
source "${target_script}" >/dev/null
|
|
}
|
|
|
|
printf "%s${line_delim}" ${found_vars}
|