From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 3757 invoked from network); 25 May 2020 09:05:22 -0000 Received: from ns1.primenet.com.au (HELO primenet.com.au) (203.24.36.2) by inbox.vuxu.org with ESMTPUTF8; 25 May 2020 09:05:22 -0000 Received: (qmail 29761 invoked by alias); 25 May 2020 09:05:10 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 24871 Received: (qmail 24659 invoked by uid 1010); 25 May 2020 09:05:10 -0000 X-Qmail-Scanner-Diagnostics: from gate.utahime.jp by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.3/25821. spamassassin: 3.4.4. Clear:RC:0(183.180.29.210):SA:0(-2.0/5.0):. Processed in 2.340855 secs); 25 May 2020 09:05:10 -0000 X-Envelope-From: yasu@utahime.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at utahime.org designates 183.180.29.210 as permitted sender) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at eastasia.home.utahime.org Date: Mon, 25 May 2020 18:02:59 +0900 (JST) Message-Id: <20200525.180259.1237420001357827351.yasu@utahime.org> To: zsh-users@zsh.org Subject: Avoid duplication of code From: Yasuhiro KIMURA X-Mailer: Mew version 6.8 on Emacs 26.3 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam: Yes Hello All, It may not directly be related to zsh but please help me improve my code. There is following function definition in my .zshrc. ---------------------------------------------------------------------- 01: update_ssh_auth_sock () { 02: if [ -z "${TMUX}" ] 03: then 04: echo "This function must be called inside tmux session." 05: return -1 06: fi 07: newval=$(tmux show-environment SSH_AUTH_SOCK | sed -n 's/^SSH_AUTH_SOCK=\(.*\)/\1/p') 08: if [ -n "${SSH_AUTH_SOCK}" ] 09: then 10: if [ -z "${newval}" ] 11: then 12: unset SSH_AUTH_SOCK 13: echo "SSH_AUTH_SOCK is cleared." 14: elif [ "${SSH_AUTH_SOCK}" != "${newval}" ] 15: then 16: export SSH_AUTH_SOCK=${newval} 17: echo "SSH_AUTH_SOCK is updated." 18: else 19: echo "SSH_AUTH_SOCK is unchanged." 20: fi 21: elif [ -n "${newval}" ] 22: then 23: export SSH_AUTH_SOCK=${newval} 24: echo "SSH_AUTH_SOCK is set now." 25: else 26: echo "SSH_AUTH_SOCK stays unset." 27: fi 28: return 0 29: } ---------------------------------------------------------------------- I use this function to update environment variables related to ssh-agent when I attaches tmux session that already exists before login. Currently this function only update SSH_AUTH_SOCK but I would like to change it so it also updates SSH_AGENT_PID. The easiest way is to copy lines from 7 to 27, paste them after line 27 and replace SSH_AUTH_SOCK with SSH_AGENT_PID. But if possible I would like to avoid duplication of code. At first following 2 idea hit upon me. 1. Define function that updates single environment variable and pass variable name as argument of it. 2. Use 'for name in word; do list done' such as following. for valname in SSH_AUTH_SOCK SSH_AGENT_PID do (update value of environment variable named ${valname}) done But it seems that both require nested variable expansion and that it is impossible. Then is there any other way to avoid code duplication? Best Regards. --- Yasuhiro KIMURA