Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Avoid duplication of code



Hello Jun,

Thank you for reply.

From: "Jun. T" <takimoto-j@xxxxxxxxxxxxxxxxx>
Subject: Re: Avoid duplication of code
Date: Mon, 25 May 2020 23:22:39 +0900

> Suppose a parameter, say valname, contains a name of another parameter;
> 
> valname=SSH_AUTH_SOCK
> 
> Then you can GET the current value of SSH_AUTH_SOCK by
> 
> if [[ -n ${(P)valname} ]]; then ...
> 
> You can SET a new value to SSH_AUTH_SOCK in a couple of ways.
> In your case, probably
> 
> export $valname='new value'
> 
> would be the simplest. If you don't want to export the variable, then,
> 
> : ${(P)valname::=new value}

I changed function definition as following.

----------------------------------------------------------------------
update_tmux_ssh_agent_environments () {
    if [ -z "${TMUX}" ]
    then
        echo "This function must be called inside tmux session."
        return -1
    fi
    for varname in SSH_AGENT_PID SSH_AUTH_SOCK
    do
        newvalue=$(tmux show-environment "${varname}" | sed -n "s/^${varname}=\(.*\)/\1/p")
        if [ -n "${(P)varname}" ]
        then
            if [ -z "${newvalue}" ]
            then
                unset "${varname}"
                echo "${varname} is cleared."
            elif [ "${(P)varname}" != "${newvalue}" ]
            then
                export "${varname}"="${newvalue}"
                echo "${varname} is updated."
            else
                echo "${varname} is unchanged."
            fi
        elif [ -n "${newvalue}" ]
        then
            export "${varname}"="${newvalue}"
            echo "${varname} is set now."
        else
            echo "${varname} stays unset."
        fi
    done
    return 0
}
----------------------------------------------------------------------

And now I can successfully update both SSH_AGENT_PID and
SSH_AUTH_SOCK.

---
Yasuhiro KIMURA



Messages sorted by: Reverse Date, Date, Thread, Author