Explanation of bug
Some version/implementation of wc
made formatted output and put spaces before the value you coud try:
$ wc -l < <(find /sys/block/ -name 'sd*')
3
Please, note that output of wc
contain spaces. Now try:
$ findhdds= 3
bash: 3: command not found
to obtain the error reported in SO's question.
Two solutions (and more...):
.1 compute answer to ensure this become a number:
findhdds=$((0+$(find /sys/block/ -name 'sd*' | wc -l)))
Or (I like this):
findhdds=$[$(find /sys/block/ -name 'sd*' | wc -l)]
Both of them will store number only in variable:
echo $findhdds
3
.2 Store devices names in an array an count members:
You could use bash for doing what wc
does:
$ findhdds=($(find /sys/block/ -name 'sd*'))
$ echo $findhdds
/sys/block/sda
$ echo ${#findhdds[@]}
3
$ echo ${findhdds[2]}
/sys/block/sdc
At all, in this particular case, you could use bash for the whole job:
Simply:
findhdds=(/sys/block/sd*)
Count
echo ${#findhdds[@]}
3
Indices
echo ${!findhdds[@]}
0 1 2
All disks (with full paths)
echo ${findhdds[@]}
/sys/block/sda /sys/block/sdb /sys/block/sdc
One of them
echo ${findhdds[0]}
/sys/block/sda
Only the device name
echo ${findhdds[0]##*/}
sda
echo ${findhdds[@]##*/}
sda sdb sdc
... could be processed in a loop:
for devnam in ${findhdds[@]##*/} ; do
printf "Doing something with device: %s...\n" $devnam
done
Doing something with device: sda...
Doing something with device: sdb...
Doing something with device: sdc...