1
#Get client IP base on current logged in user
if [ $USER == 'root' ]
then
    ip="$(last | awk 'NR==1 {print $3}')"
else
    ip="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
fi

/root/.bashrc:157: = not found

Line 157

enter image description here


Note

I appended this line source ~/.bashrc to my .zshrc to

vi .zshrc
source ~/.bashrc  # import all my quick aliases and fns
code-8
  • 442
  • 2
    Why are you parsing (or attempting to parse) the .bashrc if you're using zsh? – Chris Davies May 24 '21 at 16:37
  • I am so sorry, I updated my post. The issue now on line 157. I showed what it is. – code-8 May 24 '21 at 16:40
  • I am trying to load make my .bashrc file also compatible with .zshrc. – code-8 May 24 '21 at 16:41
  • Please don't post images of text. Instead, copy/paste the text into your question and use the formatting tools to format it as code. That way, we can actually copy the code to test it and we don't need to manually type it out. – terdon May 24 '21 at 17:04
  • 1
    Only mean to show line number as Proof. Code is there also. – code-8 May 24 '21 at 17:14
  • BTW, that ip="$(last | awk ... line to get the IP does not give you the current user's IP address. It gives you the IP (or hostname or tmux pid & window, or login day name, etc) of the last user to cause wtmp to be updated, which isn't necessarily the current user. Try who -m --ips (GNU who), but note that reliably getting the current login's IP is more complicated than it initially seems, and if you're doing it from a profile script like .zshrc or .bashrc, you have to handle non-network logins (e.g. console/xterm/serial login, tmux or screen window, etc), and non-login shells too. – cas May 25 '21 at 03:07
  • $SSH_CONNECTION is fairly reliable if you only allow network logins via ssh. Use that first, and only try who -m --ips or last "$USER" or even tty if it's empty. – cas May 25 '21 at 03:13

2 Answers2

4

In zsh, =cmd is a filename expansion operator that expands to the path of the cmd command. =cmd is similar to $commands[cmd].

So here, with == in one of the arguments of the [ command, that expands it to the path of the = command. As there's no command called = in your $PATH, that causes an error.

Compare:

$ echo =ls
/bin/ls
$ echo =junk
zsh: junk not found

The equality operator in the [ command is =. The [ command only does tests, it doesn't do any assignments, so there's no need to differentiate between an assignment operator and an equality comparison operator like there is in some other languages (with = vs == like in C or := vs = in some others, etc).

So it should just be:

[ "$USER" = root ]

Still the [ of zsh, like that of ksh also supports == as an alternative to =, but unless you disable the equals option (like it is in ksh emulation), you'd need to quote at least the first = to avoid that =cmd operator:

[ "$USER" '==' root ]

Note that while $USERNAME is automatically set by zsh, $USER is not (though it is set as an environ variable by some things like login).

To test whether you have superuser privileges, it's more robust to check that your effective user id is 0, which can be done in zsh or bash with [ "$EUID" -eq 0 ] or (( EUID == 0 )).

See also:

3

This line is not really syntactically correct (but bash handles it):

if [ $USER == 'root' ]

You should use == only with [[...]], and = otherwise. I'd also recommend you double-quote $USER if you're staying with [...] syntax

if [[ "$USER" == 'root' ]]

or

if [ "$USER" = 'root' ]
Chris Davies
  • 116,213
  • 16
  • 160
  • 287