You are not logged in.

#1 2022-06-18 22:59:12

kev717
Member
Registered: 2011-06-08
Posts: 67
Website

How to set different code path based on current user shell?

Here's some pseudo-code:

if [[ shell == bash ]]; then
    do something
elif [[ shell == csh ]]; then
    do something else
elif [[ shell == zsh ]]; then
    do something different
else
    return
fi

My question is: how do I detect what shell is running in the general case to execute the different code paths for each shell?


If I code something, it's probably at https://github.com/kellcodes

Do **NOT** expect a response from me.

Offline

#2 2022-06-18 23:03:25

Adriik
Member
From: Argentina
Registered: 2022-03-10
Posts: 134
Website

Re: How to set different code path based on current user shell?

Maybe with the SHELL variable?

> echo $SHELL
/bin/zsh

More info: https://wiki.archlinux.org/title/Environment_variables

Last edited by Adriik (2022-06-18 23:04:05)


I'm just someone. Please use [code] [/code] tags.

Offline

#3 2022-06-18 23:06:16

kev717
Member
Registered: 2011-06-08
Posts: 67
Website

Re: How to set different code path based on current user shell?

I tried that.  The problem on my system is that some shells are returning /usr/bin/<shell> instead of /bin/<shell> and I want this to work well in the future.  I guess I could just use `rev | cut -d'/' -f 1 | rev`, it's just not as pretty.


If I code something, it's probably at https://github.com/kellcodes

Do **NOT** expect a response from me.

Offline

#4 2022-06-18 23:38:26

Adriik
Member
From: Argentina
Registered: 2022-03-10
Posts: 134
Website

Re: How to set different code path based on current user shell?

returning /usr/bin/<shell> instead of /bin/<shell>

/bin on Arch is a symlink to /usr/bin:

> ls -l /bin
lrwxrwxrwx 1 root root 7 dic  6  2021 /bin -> usr/bin

I'm just someone. Please use [code] [/code] tags.

Offline

#5 2022-06-18 23:43:40

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: How to set different code path based on current user shell?

kev717 wrote:

I guess I could just use `rev | cut -d'/' -f 1 | rev`

Or just...

basename $SHELL

No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#6 2022-06-18 23:49:00

kev717
Member
Registered: 2011-06-08
Posts: 67
Website

Re: How to set different code path based on current user shell?

Adriik wrote:

/bin on Arch is a symlink to /usr/bin

Yes.  Yes it is.

Either way, it doesn't look like my helper script will work, apparently writing a script that runs `eval "$(/home/user/anaconda3/bin/conda shell.bash hook)"` doesn't have the intended effect of setting up my user-installed anaconda environment.

basename $SHELL

I forget the specific case, but one time I spent the better part of 2 hours debugging a script only to find that the 'basename' command was returning trash and my silly little `rev | cut -d'/' -f 1 | rev` fix was the only way it would work.  That's why I don't use basename.

Last edited by kev717 (2022-06-18 23:52:38)


If I code something, it's probably at https://github.com/kellcodes

Do **NOT** expect a response from me.

Offline

#7 2022-06-19 01:11:41

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,480
Website

Re: How to set different code path based on current user shell?

I'm not sure if using $SHELL is the best way to go (it doesn't work on my system), but if you do, don't use that three level pipeline with a rev | cut | rev, just use sed or awk:

echo $SHELL | sed 's|.*/||'
# or
echo $SHELL | awk  -F/ '{print $NF;}'

I suspect parsing /proc/$$/comm will be more reliable and may not need any processing.

EDIT: scratch that, /proc/$$/comm will fail too.  But `readlink /proc/$$/exe` should be good, but then requires basename or trimming again.

Last edited by Trilby (2022-06-19 01:22:18)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2022-06-19 06:59:07

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,569

Re: How to set different code path based on current user shell?

I forget the specific case, but one time … the 'basename' command was returning trash

Unescaped and unquoted blank in the variable … GI/GO problem.

Offline

Board footer

Powered by FluxBB