3

The fact that bash is 0-indexed and zsh is 1-indexed can cause problems. For example the following will work as intended with bash, but not zsh (PS I am aware that I don't NEED to use an array for this, it's just an example):

DISK1=Samsung_SSD_850_EVO_120GB_S21SNX0H915161E
DISK2=Samsung_SSD_850_EVO_120GB_S21SNX0H915160K

DISKS=( $DISK1 $DISK2 )

mdadm --create /dev/md0 --level=mirror --raid-devices=2 "${DISKS[0]}" "${DISKS[1]}"

I was thinking I could make this shell-agnostic by writing everything relative to the index of the current shell, if there were a way to get the index of the current shell as a variable, something like this:

INDEX=$(some_function_that_returns_index)
DISK1=Samsung_SSD_850_EVO_120GB_S21SNX0H915161E
DISK2=Samsung_SSD_850_EVO_120GB_S21SNX0H915160K

DISKS=( $DISK1 $DISK2 )

echo "${DISKS[$INDEX]}" "${DISKS[$(expr $INDEX + 1)]}"



mdadm --create /dev/md0 --level=mirror --raid-devices=2 "${DISKS[$INDEX]}" "${DISKS[$(expr $INDEX + 1)]}"

Is there a way to get the index of the current shell, or alternatively get the nth array element? Ideally using just single shell commands and not a custom little script.

8
  • 1
    I don't understand why you want to write a script that two separate languages can run. Commented Nov 25, 2023 at 3:29
  • 2
    perhaps this would help: stackoverflow.com/questions/60844165/… . on initialization, set a variable for indexbase based on the shell identified. Commented Nov 25, 2023 at 4:33
  • 7
    Some comments here seem unaware that all Unixes must have a Bourne-compatible shell (/bin/sh) in order to meet the requirements of POSIX and the Unix trademark, and there is a longstanding tradition that scripts intended for sharing/distribution are expected to only use Bourne shell syntax so they can run on any Unix or Unix-like OS without modification. ksh, bash, and zsh are all considered Bourne-compatible shells, especially when invoked in Bourne-compatibility mode. It seems like OP is trying to honor the tradition in spirit, but without giving up arrays, which sh didn't support.
    – Spiff
    Commented Nov 25, 2023 at 4:57
  • 1
    "So that it will still work if I change shells" – Write a shell script for a certain shell, use a proper shebang and run it from any shell. In case of a script (as opposed to a sourced file) the shebang is what matters, your current interactive shell does not matter. Changing shells is not a problem, missing the shell specified by the shebang would be. Therefore portable scripts should use sh. (cont'd) Commented Nov 25, 2023 at 19:56
  • 1
    (cont'd) The whole idea of the POSIX shell is to have a "minimal" specification anyone can rely on. sh may be provided by Bash, Dash or whatever, these are implementations. Any implementation should follow the specification. These shells can run portable code out of the box, but when invoked as sh they deliberately drop (some of) their extra features so the features not specified by POSIX don't interfere by chance. Any POSIX-compliant OS provides sh and you can count on it because this is the idea. Commented Nov 25, 2023 at 19:56

0

You must log in to answer this question.

Browse other questions tagged .