encapsulate sourced script in zsh - unix

I'm trying to control what variables get defined when sourcing a script in zsh. I'm imagining something that corresponds to this code:
(
source variable_definitions
somehow_export variable1=$variable_defined_in_script1
)
echo $variable1
as a result I want variable1 to be defined in the external scope and not variable_defined_in_script or any other variables in the sourced script.
(somehow_export is some magical placeholder in this example that allows exporting variable definitions to a parent shell. I believe that's not possible, so I'm looking for other solutions)

Something like this?
(
var_in_script1='Will this work?'
print variable1=$var_in_script1
) | while read line
do
[[ $line == *=* ]] && typeset "$line"
done
print $variable1
#=> Will this work?
print $var_in_script1
#=>
# empty; variable is only defined in the child shell
This uses stdout to send information to the parent shell. Depending on your requirements, you can add text to the print statement to filter for just the variables you want (this just looks for an '=').
If you need to handle more complex variables such as arrays, typeset -p
is a great option in zsh that can help. It's also useful for simply printing
the contents and types of variables.
(
var_local='this is only in the child process'
var_str='this is a string'
integer var_int=4
readonly var_ro='cannot be changed'
typeset -a var_ary
var_ary[1]='idx1'
var_ary[2]='idx2'
var_ary[5]='idx5'
typeset -A var_asc
var_asc[lblA]='label A'
var_asc[lblB]='label B'
# generate 'typeset' commands for the variables
# that will be sent to the parent shell:
typeset -p var_str var_int var_ro var_ary var_asc
) | while read line
do
[[ $line == typeset\ * ]] && eval "$line"
done
print 'In parent:'
typeset -p var_str var_int var_ro var_ary var_asc
print
print 'Not in parent:'
typeset -p var_local
Output:
In parent:
typeset var_str='this is a string'
typeset -i var_int=4
typeset -r var_ro='cannot be changed'
typeset -a var_ary=( idx1 idx2 '' '' idx5 )
typeset -A var_asc=( [lblA]='label A' [lblB]='label B' )
Not in parent:
./tst05:typeset:33: no such variable: var_local

Related

In zsh, is the tag "dynamic-dirs" special?

I’m wanting to do basically what this function does. It comes from the zsh manual page and other zsh documentation. I’m trying to “vaguely understand” without diving into the fine details what this function is doing and how it works.
In this case, I understand everything more or less except the _wanted line and in particular, is the tag "dynamic-dirs" any arbitrary tag or does it need to match what the higher level driving functions are looking for? I read briefly about tags and it seems like they would need to match up but I can’t find dynamic-dirs anywhere in any code that I’ve grep’ed. Nor have a found any type of list of tags that are used and what they means except the example of “files” and “directories” mentioned in the first few paragraphs of zshcompsys(1).
zsh_directory_name() {
emulate -L zsh
setopt extendedglob
local -a match mbegin mend
if [[ $1 = d ]]; then
# turn the directory into a name
if [[ $2 = (#b)(/home/pws/perforce/)([^/]##)* ]]; then
typeset -ga reply
reply=(p:$match[2] $(( ${#match[1]} + ${#match[2]} )) )
else
return 1
fi
elif [[ $1 = n ]]; then
# turn the name into a directory
[[ $2 != (#b)p:(?*) ]] && return 1
typeset -ga reply
reply=(/home/pws/perforce/$match[1])
elif [[ $1 = c ]]; then
# complete names
local expl
local -a dirs
dirs=(/home/pws/perforce/*(/:t))
dirs=(p:${^dirs})
_wanted dynamic-dirs expl 'dynamic directory' compadd -S\] -a dirs
return
else
return 1
fi
return 0
}
The other question is about the ’n’ section. It is going to return a reply even if the directory doesn’t exist. Am I reading the code right?
I guess this would be nice if I was going to do mkdir ~p:foodog ?

using associative arrays with zparseopts

I'm trying to use an associate array to parse options with zparseopts.
I have some working code, shared below, using normal arrays...but it's so awkward and verbose. i want to just ask if -f is present using opts as an associative array, by passing -A as my option to zparseopts, but I can't seem to make it work.
local -a opts
zparseopts -D -a opts f
if [[ ${opts[(ie)-f]} -le ${#opts} ]]; then
echo "force was passed"
else
echo "be kind"
fi
thanks for any help!
When the (i) subscript flag is used with an associative array, the substitution will return the key if a match was found, or an empty value if it was not. So you just need to test for a non-empty string:
local -A opts
zparseopts -D -A opts f
if [[ -n ${opts[(ie)-f]} ]]; then
echo "force was passed"
else
echo "be kind"
fi
To consolidate option parsing, I've used this (slightly cryptic) substitution:
flag=-f; force=${${:-true false}[(w)((${#opts[(i)$flag]}>0?1:2))]}
flag=-q; quiet=${${:-true false}[(w)((${#opts[(i)$flag]}>0?1:2))]}
...
if $force; then
...
if ! $quiet; then
...

How to complete a variable number of arguments containing spaces

I've build a command line tool and I need to complete arguments with zsh. I never wrote a zsh completion function so I looked in the scripts provided with zsh but I missed something so that it could work properly.
So, mytool can take a variable number of values and two options.
Here are some call examples:
mytool ONE
mytool ONE TWO
mytool AAA BBB CCC DDD EEE --info
In order to complete the values, I hava another executable that outputs all possible lines to stdout, like this simplified script named getdata:
#!/usr/local/bin/zsh
echo ONE
echo TWO ONE
echo TWO TWO
# ... a lot of lines
echo OTHER ONE
echo ONE ANOTHER LINE
echo AAA BBB CCC DDD EEE
Each completion must match to a whole line, so in my getdata example, it will not be possible to just complete with the value TWO because this whole line does not exist, it must be TWO ONE or TWO TWO.
As this script is quite time consuming, I would like to use zsh caching feature. So, here is my zsh complete script:
compdef _complete_mytool mytool
__mytool_caching_policy() {
oldp=( "$1"(Nmh+1) ) # 1 hour
(( $#oldp ))
}
__mytool_deployments() {
local cache_policy
zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
if [[ -z "$cache_policy" ]]; then
zstyle ":completion:${curcontext}:" cache-policy __mytool_caching_policy
fi
if ( [[ ${+_mytool_values} -eq 0 ]] || _cache_invalid mytool_deployments ) \
&& ! _retrieve_cache mytool_deployments;
then
local -a lines
_mytool_values=(${(f)"$(_call_program values getdata)"})
_store_cache mytool_deployments _mytool_values
fi
_describe "mytool values" _mytool_values
}
_complete_mytool() {
integer ret=1
local -a context expl line state state_descr args
typeset -A opt_args
args+=(
'*:values:->values'
'--help[show this help message and exit]'
'(-i --info)'{-i,--info}'[display info about values and exit]'
'(-v --version)'{-v,--version}'[display version about values and exit]'
)
_call_function res __mytool_deployments
return ret
}
But when I try to complete, spaces are escaped with backslash, and I don't want this behaviour.
mytool OTHER\ ONE
The options seem not to be completed too... So, any help will be greatly appreciated.
Thanks to okdana on the freenode zsh channel who helped me a lot.
So, the solution is:
compdef _complete_mytool mytool
__mytool_caching_policy() {
oldp=( "$1"(Nmh+1) ) # 1 hour
(( $#oldp ))
}
__mytool_deployments() {
local cache_policy
zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
if [[ -z "$cache_policy" ]]; then
zstyle ":completion:${curcontext}:" cache-policy __mytool_caching_policy
fi
if ( [[ ${+_mytool_values} -eq 0 ]] || _cache_invalid mytool_deployments ) \
&& ! _retrieve_cache mytool_deployments;
then
local -a lines
_mytool_values=(${(f)"$(_call_program values getdata)"})
_store_cache mytool_deployments _mytool_values
fi
_describe "mytool values" _mytool_values -Q
}
_complete_mytool() {
_arguments : \
': :__mytool_deployments' \
'--help[show this help message and exit]' \
'(-i --info)'{-i,--info}'[display info about values and exit]' \
'(-v --version)'{-v,--version}'[display version about values and exit]'
}

Not able to change global variable in function used for zsh prompt

I'm trying to build a zsh function that returns an output based on a time interval. Initially the "You're thirsty" condition is true, but after changing the variable thirsty through the command line and setting it to false, the initial if statement goes through, but the variable thirsty in it doesn't change the global variable thirsty. Is there a way to modify the global variable thirsty?
thirsty=
last_time=
drink_water() {
echo -n "$thirsty"
if [[ $thirsty == false ]]; then
last_time="$[$(date +%s) + 10]"
thirsty=true
echo -n "${last_time} $(date +%s) ${thirsty}"
elif [[ $[last_time] -lt $(date +%s) ]]; then
echo -n "💧 You're thirsty"
fi
}
Since your code is actually called from:
PROMPT='$(drink_water)'
...everything it contains is run in a subprocess spawned as part of this command substitution operation ($() is a "command substitution": It creates a new subprocess, runs the code given in that subprocess, and reads the subprocess's output). When that subprocess exits, changes to variables -- even global variables -- made within the subprocess are lost.
If you put your update code directly inside a precmd function, then it would be run before each prompt is printed but without a command substitution intervening. That is:
precmd() {
local curr_time=$(date +%s) # this is slow, don't repeat it!
if [[ $thirsty = false ]]; then
last_time="$(( curr_time + 10 ))"
thirsty=true
PROMPT="$last_time $curr_time $thirsty"
elif (( last_time < curr_time )); then
PROMPT="💧 You're thirsty"
fi
}
Of course, you can set your PROMPT with a command substitution, but updates to variable state have to be done separately, outside that command substitution, if they are to persist.
You can use Zsh Hooks.
Hooks avoid the issues of command substitution here because they run in the same shell, rather than a subshell.
drink_water_prompt=
thirsty=
last_time=
drink_water_gen_prompt() {
drink_water_prompt="$thirsty"
if [[ $thirsty == false ]]; then
last_time="$[$(date +%s) + 10]"
thirsty=true
drink_water_prompt+="${last_time} $(date +%s) ${thirsty}"
elif [[ $[last_time] -lt $(date +%s) ]]; then
drink_water_prompt+="💧 You're thirsty"
fi
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd drink_water_gen_prompt
PROMPT='${drink_water_prompt}'
These also allow more than one precmd() function.

Loop over environment variables in POSIX sh

I need to loop over environment variables and get their names and values in POSIX sh (not bash). This is what I have so far.
#!/usr/bin/env sh
# Loop over each line from the env command
while read -r line; do
# Get the string before = (the var name)
name="${line%=*}"
eval value="\$$name"
echo "name: ${name}, value: ${value}"
done <<EOF
$(env)
EOF
It works most of the time, except when an environment variable contains a newline. I need it to work in that case.
I am aware of the -0 flag for env that separates variables with nul instead of newlines, but if I use that flag, how do I loop over each variable? Edit: #chepner pointed out that POSIX env doesn't support -0, so that's out.
Any solution that uses portable linux utilities is good as long as it works in POSIX sh.
There is no way to parse the output of env with complete confidence; consider this output:
bar=3
baz=9
I can produce that with two different environments:
$ env -i "bar=3" "baz=9"
bar=3
baz=9
$ env -i "bar=3
> baz=9"
bar=3
baz=9
Is that two environment variables, bar and baz, with simple numeric values, or is it one variable bar with the value $'3\nbaz=9' (to use bash's ANSI quoting style)?
You can safely access the environment with POSIX awk, however, using the ENVIRON array. For example:
awk 'END { for (name in ENVIRON) {
print "Name is "name;
print "Value is "ENVIRON[name];
}
}' < /dev/null
With this command, you can distinguish between the two environments mentioned above.
$ env -i "bar=3" "baz=9" awk 'END { for (name in ENVIRON) { print "Name is "name; print "Value is "ENVIRON[name]; }}' < /dev/null
Name is baz
Value is 9
Name is bar
Value is 3
$ env -i "bar=3
> baz=9" awk 'END { for (name in ENVIRON) { print "Name is "name; print "Value is "ENVIRON[name]; }}' < /dev/null
Name is bar
Value is 3
baz=9
Maybe this would work?
#!/usr/bin/env sh
env | while IFS= read -r line
do
name="${line%%=*}"
indirect_presence="$(eval echo "\${$name+x}")"
[ -z "$name" ] || [ -z "$indirect_presence" ] || echo "name:$name, value:$(eval echo "\$$name")"
done
It is not bullet-proof, as if the value of a variable with a newline happens to have a line beginning that looks like an assignment, it could be somewhat confused.
The expansion uses %% to remove the longest match, so if a line contains several = signs, they should all be removed to leave only the variable name from the beginning of the line.
Here an example based on the awk approach:
#!/bin/sh
for NAME in $(awk "END { for (name in ENVIRON) { print name; }}" < /dev/null)
do
VAL="$(awk "END { printf ENVIRON[\"$NAME\"]; }" < /dev/null)"
echo "$NAME=$VAL"
done

Resources