zsh-users
 help / color / mirror / Atom feed
From: Yasuhiro KIMURA <yasu@utahime.org>
To: zsh-users@zsh.org
Subject: Re: Avoid duplication of code
Date: Thu, 28 May 2020 17:05:55 +0900 (JST)
Message-ID: <20200528.170555.2059382693553026707.yasu@utahime.org> (raw)
In-Reply-To: <D7E7657E-0281-4537-825A-2D3021461A67@kba.biglobe.ne.jp>

Hello Jun,

Thank you for reply.

From: "Jun. T" <takimoto-j@kba.biglobe.ne.jp>
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

  reply	other threads:[~2020-05-28  8:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-25  9:02 Yasuhiro KIMURA
2020-05-25 14:22 ` Jun. T
2020-05-28  8:05   ` Yasuhiro KIMURA [this message]
2020-05-28  8:53     ` Daniel Shahaf
2020-05-28 10:24       ` Yasuhiro KIMURA

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200528.170555.2059382693553026707.yasu@utahime.org \
    --to=yasu@utahime.org \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git