0

I have this command that works in ZSH but not in bash. The command is an AFNI command and is as follows:

3dTcat \
-prefix Temp.nii \
Zero_dataset.nii \
$directory_rois/{{$array1}.nii,{$array2}.nii,{$array3}.nii}

where each array is a unique and non-overlapped interval of numbers between 1-1000 (an example being: array1=173..233)

Basically this script is combining all the .nii files together that have file names equivalent to a number within the arrays.

I've tried to change it to a bash acceptable format in the following way:

3dTcat \
-prefix Temp.nii \
Zero_dataset.nii \
$directory_rois/{"$array1".nii,"$array2".nii,"$array3".nii}

but this just leads to "FATAL ERROR: Can't open dataset $directory_rois/1..81.nii

This makes sense to me since 1..81.nii is not a file, rather it is 1.nii; 2.nii; 3.nii, (...) 1000.nii that are real files.

Is there any advice that you can provide?

Austin
  • 1
  • You did not tell how are your arrays created and what they contain. But aren't you looking for ``${array1[@]}`` ? – Itération 122442 May 09 '23 at 07:24
  • Arrays were initially defined as follows: (array1=289..317 array2=812..842). But this was done in ZSH. I've since formatted it as such (array1=( $(seq 289 317) ) array2=( $(seq 812 842) )) and am following your advice to use the ${array1[@]} formatting. I'm currently running it to see if it works! Thanks for your input :) – Austin May 10 '23 at 20:48
  • First of all, it means that `array1` does not have the value you expect it has. Secondly, please make up your mind whether you want to use bash or zsh. Arrays are pretty different in these languages, and it does not really make sense to tag the question with both bash and zsh. – user1934428 May 11 '23 at 06:54

2 Answers2

0

Bash does not let you use variables in brace expansion. A reasonable refactoring would look something like

for ((x=${array1[0]}; x<=${array1[-1]}; ++x)); do
  for ((y=${array2[0]}; y<=${array2[-1]}; ++y)); do
    for ((z=${array3[0]; z<=${array3[-1]}; ++z)); do
      3dTcat \
        -prefix Temp.nii \
        Zero_dataset.nii \
        "$directory_rois"/{"$x","$y","$z"}.nii
    done
  done
done

(Pre-4 versions of Bash don't let you use [-1] to access the final element of an array; if you need compatibility back to Bash 3.x. you need to use something more complex.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • In addition, in zsh you would write $array1[1], becaue the lowest array index is 1. Array operations which work the same in bash and zsh are tricky. – user1934428 May 09 '23 at 10:27
0

I think you could use something like:

array1[0]=1
array1[1]=2
array2[0]=454
echo {"${array1[@]/%/.nii}","${array2[@]/%/.nii}"}
1.nii 2.nii 454.nii

However I can't remember how this is called and can't find it in documentation.

Tested on bash 4.4.23.

Itération 122442
  • 2,644
  • 2
  • 27
  • 73