• Re: Getting a home directory in shell script (Was: Abnormal HOME variab

    From Eli the Bearded@21:1/5 to Kenny McCormack on Tue Oct 26 19:10:57 2021
    In comp.unix.shell, Kenny McCormack <gazelle@shell.xmission.com> wrote:
    Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
    [ HOME=$(dirname $(dirname $(realpath -e $0))) proxychains-ng-socks5 ]
    $ awk -F : -v user=$(whoami) '$1 == user {print $6;}' /etc/passwd
    /home/lpitcher
    Or just:

    $ echo ~

    I've just got to really wonder what's going on here. If I have set
    HOME differently than the one in /etc/passwd, I probably have a good
    reason for that and will not like having it reset.

    I frequently use /var/tmp/$(whoami) as an alternative $HOME for testing
    things, in particular testing how stuff works without all my dot files.
    Or for say, testing how dot file installation from a home-config git
    repo works.

    I use other directories, too, but rarely. It's a handy way to have
    multiple simultaneous news readers working with different .newsrc files,
    for example.

    Elijah
    ------
    HOME=/var/tmp/elijah PS1=': subshell; ' $SHELL

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to Kenny McCormack on Tue Oct 26 17:45:11 2021
    On Wednesday, October 27, 2021 at 2:37:57 AM UTC+8, Kenny McCormack wrote:
    In article <sl9ckq$nc9$1...@dont-email.me>,
    Lew Pitcher <lew.p...@digitalfreehold.ca> wrote:
    On Tue, 26 Oct 2021 08:14:02 -0700, hongy...@gmail.com wrote:

    On Tuesday, October 26, 2021 at 9:55:32 PM UTC+8, Lew Pitcher wrote:
    On Tue, 26 Oct 2021 05:47:48 -0700, hongy...@gmail.com wrote:

    I've written the following Emacs package debug script which can be
    run by `$ bash /home/werner/.emacs.d/debug/.emacs.d/init.el`:

    ```emacs-lisp
    #!/usr/bin/env bash
    :;# $ realpath init.el
    :;# /home/werner/.emacs.d/debug/.emacs.d/init.el
    :; HOME=$(dirname $(dirname $(realpath -e $0))) proxychains-ng-socks5 >>> > /usr/local/bin/emacs -- "$@"; exit

    ;;Bootstrap straight
    (defvar bootstrap-version)
    (let ((bootstrap-file
    (expand-file-name "straight/repos/straight.el/bootstrap.el"
    user-emacs-directory))
    (bootstrap-version 5))
    (unless (file-exists-p bootstrap-file)
    (with-current-buffer
    (url-retrieve-synchronously
    "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
    'silent 'inhibit-cookies)
    (goto-char (point-max))
    (eval-print-last-sexp)))
    (load bootstrap-file nil 'nomessage))
    (straight-use-package 'use-package)
    (setq straight-use-package-by-default t)

    (use-package company
    :demand t
    :hook
    (after-init . global-company-mode))
    ```
    But the `proxychains-ng-socks5` wrapper script relies on the normal HOME
    variable setting. How to solve this dilemma?
    Well, you /could/ derive your version of HOME differently. Yes, let's go >>> with that.

    Initially, HOME is derived from the 6th field in the user's
    /etc/passwd entry. You could
    - use whoami(1) to determine the username
    - use grep(1) and cut(1) (alternately, just awk(1) )to parse /etc/passwd >>> and extract the home directory of the selected username
    - use that extracted home directory value to set your HOME variable

    Do you mean this one?

    $ grep ^"$(whoami)" /etc/passwd |cut -d ':' -f6
    /home/werner

    Sure. Or
    $ awk -F : -v user=$(whoami) '$1 == user {print $6;}' /etc/passwd /home/lpitcher
    Or just:

    $ echo ~
    and
    $ echo ~lpitcher

    This doesn't work for the Emacs case. If the HOME has been changed from within the session where Emacs is started, it won't use the new HOME as ~.

    --
    People sleep peaceably in their beds at night only because rough
    men stand ready to do violence on their behalf.

    George Orwell

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Kenny McCormack on Wed Oct 27 14:31:45 2021
    On Tue, 26 Oct 2021 18:37:52 +0000, Kenny McCormack wrote:

    In article <sl9ckq$nc9$1@dont-email.me>,
    Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
    On Tue, 26 Oct 2021 08:14:02 -0700, hongy...@gmail.com wrote:

    On Tuesday, October 26, 2021 at 9:55:32 PM UTC+8, Lew Pitcher wrote:
    On Tue, 26 Oct 2021 05:47:48 -0700, hongy...@gmail.com wrote:

    I've written the following Emacs package debug script which can be
    run by `$ bash /home/werner/.emacs.d/debug/.emacs.d/init.el`:

    ```emacs-lisp
    #!/usr/bin/env bash
    :;# $ realpath init.el
    :;# /home/werner/.emacs.d/debug/.emacs.d/init.el
    :; HOME=$(dirname $(dirname $(realpath -e $0))) proxychains-ng-socks5 >>>> > /usr/local/bin/emacs -- "$@"; exit

    ;;Bootstrap straight
    (defvar bootstrap-version)
    (let ((bootstrap-file
    (expand-file-name "straight/repos/straight.el/bootstrap.el"
    user-emacs-directory))
    (bootstrap-version 5))
    (unless (file-exists-p bootstrap-file)
    (with-current-buffer
    (url-retrieve-synchronously
    "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
    'silent 'inhibit-cookies)
    (goto-char (point-max))
    (eval-print-last-sexp)))
    (load bootstrap-file nil 'nomessage))
    (straight-use-package 'use-package)
    (setq straight-use-package-by-default t)

    (use-package company
    :demand t
    :hook
    (after-init . global-company-mode))
    ```
    But the `proxychains-ng-socks5` wrapper script relies on the normal HOME >>>> > variable setting. How to solve this dilemma?
    Well, you /could/ derive your version of HOME differently. Yes, let's go >>>> with that.

    Initially, HOME is derived from the 6th field in the user's
    /etc/passwd entry. You could
    - use whoami(1) to determine the username
    - use grep(1) and cut(1) (alternately, just awk(1) )to parse /etc/passwd >>>> and extract the home directory of the selected username
    - use that extracted home directory value to set your HOME variable

    Do you mean this one?

    $ grep ^"$(whoami)" /etc/passwd |cut -d ':' -f6
    /home/werner

    Sure. Or
    $ awk -F : -v user=$(whoami) '$1 == user {print $6;}' /etc/passwd
    /home/lpitcher

    Or just:

    $ echo ~
    and
    $ echo ~lpitcher

    If $HOME is not reliably set, then ~ is unlikely to be either.
    Witness:

    10:27 $ echo $HOME
    /home/lpitcher
    10:27 $ echo ~
    /home/lpitcher

    Both $HOME and ~ point at my home directory

    But now, I change $HOME to point somewhere else
    10:27 $ HOME=/var/some/directory
    10:28 $ echo $HOME
    /var/some/directory

    And, lo and behold, ~ changes as well
    10:28 $ echo ~
    /var/some/directory

    So, if the OP can't depend on $HOME to point to the proper
    directory (hence, his asking how to derive the home directory)
    then he can't depend on ~ either.

    HTH
    --
    Lew Pitcher
    "In Skills, We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Lew Pitcher on Wed Oct 27 17:12:22 2021
    On Wed, 27 Oct 2021 14:31:45 +0000, Lew Pitcher wrote:

    On Tue, 26 Oct 2021 18:37:52 +0000, Kenny McCormack wrote:

    In article <sl9ckq$nc9$1@dont-email.me>,
    Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
    On Tue, 26 Oct 2021 08:14:02 -0700, hongy...@gmail.com wrote:

    On Tuesday, October 26, 2021 at 9:55:32 PM UTC+8, Lew Pitcher wrote:
    On Tue, 26 Oct 2021 05:47:48 -0700, hongy...@gmail.com wrote:

    I've written the following Emacs package debug script which can be >>>>> > run by `$ bash /home/werner/.emacs.d/debug/.emacs.d/init.el`:

    ```emacs-lisp
    #!/usr/bin/env bash
    :;# $ realpath init.el
    :;# /home/werner/.emacs.d/debug/.emacs.d/init.el
    :; HOME=$(dirname $(dirname $(realpath -e $0))) proxychains-ng-socks5 >>>>> > /usr/local/bin/emacs -- "$@"; exit

    ;;Bootstrap straight
    (defvar bootstrap-version)
    (let ((bootstrap-file
    (expand-file-name "straight/repos/straight.el/bootstrap.el"
    user-emacs-directory))
    (bootstrap-version 5))
    (unless (file-exists-p bootstrap-file)
    (with-current-buffer
    (url-retrieve-synchronously
    "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
    'silent 'inhibit-cookies)
    (goto-char (point-max))
    (eval-print-last-sexp)))
    (load bootstrap-file nil 'nomessage))
    (straight-use-package 'use-package)
    (setq straight-use-package-by-default t)

    (use-package company
    :demand t
    :hook
    (after-init . global-company-mode))
    ```
    But the `proxychains-ng-socks5` wrapper script relies on the normal HOME
    variable setting. How to solve this dilemma?
    Well, you /could/ derive your version of HOME differently. Yes, let's go >>>>> with that.

    Initially, HOME is derived from the 6th field in the user's
    /etc/passwd entry. You could
    - use whoami(1) to determine the username
    - use grep(1) and cut(1) (alternately, just awk(1) )to parse /etc/passwd >>>>> and extract the home directory of the selected username
    - use that extracted home directory value to set your HOME variable

    Do you mean this one?

    $ grep ^"$(whoami)" /etc/passwd |cut -d ':' -f6
    /home/werner

    Sure. Or
    $ awk -F : -v user=$(whoami) '$1 == user {print $6;}' /etc/passwd
    /home/lpitcher

    Or just:

    $ echo ~
    and
    $ echo ~lpitcher

    If $HOME is not reliably set, then ~ is unlikely to be either.
    Witness:

    10:27 $ echo $HOME
    /home/lpitcher
    10:27 $ echo ~
    /home/lpitcher

    Both $HOME and ~ point at my home directory

    But now, I change $HOME to point somewhere else
    10:27 $ HOME=/var/some/directory
    10:28 $ echo $HOME
    /var/some/directory

    And, lo and behold, ~ changes as well
    10:28 $ echo ~
    /var/some/directory

    So, if the OP can't depend on $HOME to point to the proper
    directory (hence, his asking how to derive the home directory)
    then he can't depend on ~ either.

    HTH

    FWIW, from bash(1):
    Shell Variables
    The following variables are set by the shell:
    ...
    HOME The home directory of the current user; the default argument for
    the cd builtin command. The value of this variable is also used
    when performing tilde expansion.

    --
    Lew Pitcher
    "In Skills, We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)