Selective running

I have

[root@server ~] MegaCli64 -PDList -aALL | grep -E 'Enclosure Device ID|Slot Number'
Enclosure Device ID: 252
Slot Number: 0
Enclosure Device ID: 252
Slot Number: 1
Enclosure Device ID: 252
Slot Number: 2
Enclosure Device ID: 252
Slot Number: 3

I need to run the following command on all slots using a shell script.

MegaCli64 -pdInfo -physdrv\[252:0\] -aALL | grep "Firmware state"
MegaCli64 -pdInfo -physdrv\[252:1\] -aALL | grep "Firmware state"
MegaCli64 -pdInfo -physdrv\[252:2\] -aALL | grep "Firmware state"
MegaCli64 -pdInfo -physdrv\[252:3\] -aALL | grep "Firmware state"

Assuming that the slot number is the one following the ":"

#!/bin/sh

i=0
while [ $i -le 10 ]
do
        MegaCli64 -pdInfo -physdrv\[252:${i}\] -aALL | grep "Firmware state"
        i=$(( $i + 1 ))
done

Here replace "10" by the number of slots you want to process.

Thanks.

Here "Enclosure Device ID" and "Slot number" are variables. We need to grep out both variables and apply in script. And we cannot set a definite range for slots, 1 to 10. There can be more than 10 too.

Try this

#!/bin/sh

newDeviceID=0
deviceID=0
slotNum=0

MegaCli64 -PDList -aALL | grep -E 'Enclosure Device ID|Slot Number' > infile
while read line
do
        newDeviceID=''
        newDeviceID=$(echo $line | awk -F": " '/Enclosure Device ID/ { print $2 }')
        [ ! -z "$newDeviceID" ] && deviceID=${newDeviceID} && continue
        slotNum=$(echo $line | awk -F": " '/Slot Number/ { print $2 }')
        MegaCli64 -pdInfo -physdrv\[${deviceID}:${slotNum}\] -aALL | grep "Firmware state"
done < infile

rm infile
1 Like

Thanks a lot chacko123. It works fine.