7

I just switched from Ubuntu to macOS (due to work reasons). And I wanted to transfer my bash aliases to zsh, but they are not working completely.

alias HPC1='ssh -l [username] [server1]'
alias HPC2='ssh -l [username] [server2]'
HPC[0]="HPC1"
HPC[1]="HPC2"
alias HPC='eval "${HPC[$RANDOM % 2]}"'

HPC1 connects to server1 and HPC2 connects to server2. HPC should connect to either server1 or server2 randomly. It works within bash. In macOS (zsh) only the aliases HPC1 and HPC2 work, but not HPC. Is the syntax different there?

Thanks for helping me out :).

gernophil
  • 349
  • zsh array indizes start with 1, see https://stackoverflow.com/questions/50427449/behavior-of-arrays-in-bash-scripting-and-zsh-shell-start-index-0-or-1 – nohillside Jul 17 '22 at 18:05
  • Related https://apple.stackexchange.com/questions/361870/what-are-the-practical-differences-between-bash-and-zsh?rq=1 – nohillside Jul 17 '22 at 18:38
  • If you have a complex Bash setup it might be worth installing and using an upto date bash using macports or homebrew. The package managers would also help if you needed any other unix programs that you used on Linux – mmmmmm Jul 18 '22 at 08:31
  • 1
    In zsh, HPC[0]="HPC1" should result in the error message assignment to invalid subscript range. If you don't get this error message, it means that the code either is not executed by zsh, or maybe by a zsh which is configured to run in Korn-shell compatible mode. – user1934428 Jul 18 '22 at 09:57
  • @user1934428 The question doesn't say what In macOS (zsh) only the aliases HPC1 and HPC2 work exactly means. With a invalid subscript range error, it should be clear what the error is. I wonder what exactly doesn't work, in that script, for the OP. – apaderno Jul 18 '22 at 11:18
  • in macOS (zsh) only the aliases HPC1 and HPC2 work means that if I enter HPC1 it connects to server1 and HP2 to server2. And yes, I get this error. Must have just missed it before. – gernophil Jul 19 '22 at 08:50
  • 1
    In general, don't try to use identical code for bash and zsh; there are too many differences, and not only in the array handling. A better approach would be to take the bash code as inspiration and write the zsh code from scratch. – user1934428 Jul 20 '22 at 06:23
  • @user1934428 which is why I stayed with bash and upgraded to v5 via Homebrew :-) – nohillside Jul 20 '22 at 06:51

1 Answers1

9

bash starts array indexes at 0, zsh at 1.

One slightly strange-looking way to overcome this is

server_list=(HPC1 HPC2)
alias HPC='ssh -l username ${server_list[(($RANDOM % 2 - 2))]}'

This puts the server names into a shell array, and then randomly picks the first or the second from the end of the list ($RANDOM % 2 - 2 is either -1 or -2).

nohillside
  • 100,768
  • 1
    You could also setopt KSH_ARRAYS, but that changes other semantics in addition to the zero-vs-one array indexing, so you should read the documentation before enabling it. – Kevin Jul 18 '22 at 06:02
  • 1
    Brilliant! In that way, it would select an array item starting from the bottom. – apaderno Jul 18 '22 at 10:52
  • @apaderno That is, if you envision arrays as growing from top to bottom. I envision them growing from left to right, so the negative indices operate on the items counted from the right. – jrw32982 Jul 19 '22 at 17:34