I am currently working on a small script designed for tape backups, and I need the script to search for any tape drive linked to the system. I managed to get this part working, and we can see bellow the path of the two tape drives :
lsscsi --transport | grep fc: | grep tape | awk -F" " ' { print $4 } '
/dev/st0
/dev/st1
Edit - Here's the raw output of the lsscsi commands :
[1:0:0:0] tape fc:0x50014380032a90b8,0x000001 /dev/st0
[2:0:0:0] tape fc:0x50014380032a90bb,0x000001 /dev/st1
My wish is simply to find a way to redirect each line of the previous output in a new variable ($drive0=/dev/st0
, $drive1=/dev/st1
, etc).
My first attempt looked like this, and isn't working because every lines are dumped into the same variable called $path_tape
:
#!/bin/bash
/usr/bin/lsscsi --transport | grep fc: | grep tape | while read line ; do
path_tape=$(echo $line | awk -F" " ' { print $4 } ')
done
Now the thing is, there is this thread that mention a way to increment variables names, so I tried to adapt my code to this solution, resulting in the following lines :
#!/bin/bash
i=0
/usr/bin/lsscsi --transport | grep fc: | grep tape | while read line ; do
var="path_tape$i"
${!var}=$(echo $line |awk -F" " ' { print $4 } ')
((i++))
done
But when executed, I now get a syntax error :
./test.sh: line 7: =/dev/st0: No such file or directory
./test.sh: line 7: =/dev/st1: No such file or directory
What am I missing here ?