Operating System - HP-UX
1830207 Members
1178 Online
109999 Solutions
New Discussion

Need to run a loop as per the value in the parameter

 
SOLVED
Go to solution
Vivek Bhatia
Trusted Contributor

Need to run a loop as per the value in the parameter

Hi,

I have created a script for checking the emulex card and the number of ports

##Checking Emulex Card
VALUE=`/sbin/lspci |grep -i -e Emulex | wc -l`

if [ $VALUE -eq 0 ]
then
echo "Emulex card is not present on $x host"
else
echo "Emulex card is present on $x host with $VALUE port"


So i want to run a loop as per the value of the VALUE parameter

Thanks
Vivek
7 REPLIES 7
Suraj K Sankari
Honored Contributor

Re: Need to run a loop as per the value in the parameter

Hi,

Not able to understand your question very clear.
if you want this loop is run till you have press ctrl+c then here it is

while true
do
##Checking Emulex Card
VALUE=`/sbin/lspci |grep -i -e Emulex | wc -l`
if [ $VALUE -eq 0 ]
then
echo "Emulex card is not present on $x host"
else
echo "Emulex card is present on $x host with $VALUE port"
sleep 3
done

Suraj
James R. Ferguson
Acclaimed Contributor

Re: Need to run a loop as per the value in the parameter

Hi Vivek:

You want something like this:

# cat ./showme
#!/usr/bin/sh
VALUES=$(/sbin/lspci|grep -c -i -e Emulex)
for VALUE in $VALUES
do
if [ $VALUE -eq 0 ]; then
echo "Emulex card is not present on $x host"
else
echo "Emulex card is present on $x host with $VALUE port"
fi
done

...

Note that 'grep' can return a count so you can eliminate the extra pipe to 'wc' too.

Regards!

...JRF...
Vivek Bhatia
Trusted Contributor

Re: Need to run a loop as per the value in the parameter

Hi,

Sorry for the incomplete loop above.

##Checking Emulex Card
VALUE=`/sbin/lspci |grep -i -e Emulex |wc -l`

if [ $VALUE -eq 0 ]
then
echo "Emulex card is not present on $x host"
else
echo "Emulex card is present on $x host with $VALUE port"
fi
done

Okay after the execution of this loop i will get a value in VALUE parameter.

For example VALUE =4, then i want to put a for loop to execute a command 4 times on four ports


cat /sys/class/scsi_host/host0/state | grep -i ready
cat /sys/class/scsi_host/host1/state | grep -i ready
cat /sys/class/scsi_host/host2/state | grep -i ready
cat /sys/class/scsi_host/host3/state | grep -i ready

Thanks
Vivek
James R. Ferguson
Acclaimed Contributor

Re: Need to run a loop as per the value in the parameter

Hi (again) Vivek:

> Sorry for the incomplete loop above.

Please post your complete script! It is unclear what you have now changed and what your objective is.

Regards!

...JRF...
Vivek Bhatia
Trusted Contributor

Re: Need to run a loop as per the value in the parameter

Hi James,

Will this work

for (( c=1; c<=$VALUE; c++ ))
do
cat /sys/class/scsi_host/host$c/state | grep -i ready
if [ $? -eq 0]
then
Y=$Y+1
done
fi

Thanks
Vivek
James R. Ferguson
Acclaimed Contributor
Solution

Re: Need to run a loop as per the value in the parameter

Hi (again) Vivek:

You have several syntax errors in your last post. The C-like 'for' loop isn't valid. Try something like this:

#!/usr/bin/sh
c=1
VALUE=4

while (( c<= ${VALUE} ))
do
/sys/class/scsi_host/host${c}/state | grep -i ready
if [ $? -eq 0 ]; then
Y=$(( $Y+1 ))
fi
c=$(( $c+1 ))
done

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Need to run a loop as per the value in the parameter

>JRF: Y=$(( $Y+1 ))

You can "improve" these by removing the "$" by putting everything in (( )):
(( Y += 1 ))