I have not coded in bash or made shell scripts before. The following code is working for me now in a shell script. ashlar is a python library which does multiple image stitching and registration. My question is how can I write only one variable instead of $c1 $c3 $c5 $c7
in the last line.
I have tried using or cat but it didn't work.
for i in 1 3 5 7
do
export ROOT_DIR=a/b/c/abcd_"$i"_123/
export FILENAME_HEADER=a_b_c_40X_
export WIDTH=5
export HEIGHT=15
export PIXEL_SIZE=0.1507
export OVERLAP=0.1
export LAYOUT=snake
export DIRECTION=vertical
export MAX_SHIFT=30
export SIGMA=2
export ALIGN_CHANNEL=4
export OUTPUT_DIR=a/b/c/sample.ome.tiff
export c"$i"="fileseries|""$ROOT_DIR""|pattern=""$FILENAME_HEADER"""$i"_F{series}.ome.tiff|width=""$WIDTH""|height=""$HEIGHT""|pixel_size=""$PIXEL_SIZE""|overlap=""$OVERLAP""|layout=""$LAYOUT""|direction=""$DIRECTION"
done
ashlar $c1 $c3 $c5 $c7 --flip-y --pyramid --maximum-shift $MAX_SHIFT --filter-sigma $SIGMA --align-channel $ALIGN_CHANNEL -o $OUTPUT_DIR
CodePudding user response:
Don't use numbered variables, use an array:
c=()
for i in 1 3 5 7
do
export ROOT_DIR="a/b/c/abcd_${i}_123/"
...
c =( "fileseries|${ROOT_DIR}|pattern=${FILENAME_HEADER}${i}_F{series}.ome.tiff|width=${WIDTH}|height=${HEIGHT}|pixel_size=${PIXEL_SIZE}|overlap=${OVERLAP}|layout=${LAYOUT}|direction=${DIRECTION}" )
#^^^ .............................................................................................................................................................................................. ^
done
Then
ashlar "${c[@]}" --flip-y --pyramid --maximum-shift "$MAX_SHIFT" --filter-sigma "$SIGMA" --align-channel "$ALIGN_CHANNEL" -o "$OUTPUT_DIR"
# .....^^^^^^^^^
Some notes:
- I'm using braces to delineate variables instead of multiple quotes. It's a style preference.
- not all variables need to be exported: only the ones that the
ashlar
program uses. you can just sayvar=value
to use a shell variable. - quote your variables. See Security implications of forgetting to quote a variable in bash/POSIX shells