Operating System - HP-UX
1833589 Members
4129 Online
110061 Solutions
New Discussion

Re: Please help me acheive Streamlined Counting

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Please help me acheive Streamlined Counting

OOk, so this is a small part of a script I wrote to build disk groups using VXVM. The only problem is that I am limited to a count of 8 maximum. If I want more, I will have to add more lines of "if" statements. How can I accomplish the same thing, in a few lines, but not be limited in the max count?:

if [ $NUMBER -eq 1 ]
then
continue
# exit
fi
if [ $NUMBER -eq 2 ]
then
alphabet="02"
count=0
fi
if [ $NUMBER -eq 3 ]
then
alphabet="02 03"
count=0
fi
if [ $NUMBER -eq 4 ]
then
alphabet="02 03 04"
count=0
fi
if [ $NUMBER -eq 5 ]
then
alphabet="02 03 04 05"
count=0
fi
if [ $NUMBER -eq 6 ]
then
alphabet="02 03 04 05 06"
count=0
fi
if [ $NUMBER -eq 7 ]
then
alphabet="02 03 04 05 06 07"
count=0
fi
if [ $NUMBER -eq 8 ]
then
alphabet="02 03 04 05 06 07 08"
count=0
fi
for letter in $alphabet

do
count=`expr $count + 1`
echo "$letter"
done > count.txt

Here is the output of count.txt:

02
03
04
05
06
07
08

22 REPLIES 22
Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

I forgot this part at the beginning:

echo "There are $TOTAL2 $DISKTYPE disks of this size available."
echo
echo "How many do you want to use(8 disks max!)"
print -n "to create your diskgroup? :"
read NUMBER
James R. Ferguson
Acclaimed Contributor

Re: Please help me acheive Streamlined Counting

Hi Patrick:

You could generate your output along these lines:

# cat ./makecount
#!/usr/bin/sh
MIN=2
MAX=20
echo "Enter base value \c"
read i
while (( $i <= MAX ))
do
printf ">>> %02d\n" $i
let i=i+1
done

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

Ok, I shortened it to the following:

MIN=$NUMBER
MAX=$NUMBER
while (( $NUMBER <= MAX ))
do
printf ">>> %02d\n" $NUMBER
let NUMBER=NUMBER+1
done > count.txt

The output of count.txt is now:

>>> 08

Something is wrong....

If the variable $NUMBER=8, then the output should be:

02
03
04
05
06
07
08
James R. Ferguson
Acclaimed Contributor

Re: Please help me acheive Streamlined Counting

Hi (again) Patrick:

#!/usr/bin/sh
echo "Enter minimum value \c"
read MIN
echo "Enter miximum value \c"
read MAX
while (( $MIN <= MAX ))
do
printf ">>> %02d\n" $MIN
let MIN=MIN+1
done

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

Thanks.

I supposed I should tell the purpose of the output from count.txt. It getting used later in the script so that that this happens:

vxdg -g fakedg adddisk fakedg02=c10t0d7

vxdg -g fakedg adddisk fakedg03=c10t1d0

vxdg -g fakedg adddisk fakedg04=c10t1d1

vxdg -g fakedg adddisk fakedg05=c10t1d2

vxdg -g fakedg adddisk fakedg06=c10t1d3

vxdg -g fakedg adddisk fakedg07=c10t1d4

vxdg -g fakedg adddisk fakedg08=c10t1d5


Does this change anything?


Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

I tell the script how many disks I want to use to create my disk group, and it automatically adds them. This is how the script is working. I am just trying to find a more efficient way to do the count than my current method.
Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

Here is another method I received:

case $NUMBER
in
1) continue ;;
2) alphabet="02"
count=0 ;;
3) alphabet="02 03"
count=0 ;;
4) alphabet="02 03 04"
count=0 ;;
5) alphabet="02 03 04 05"
count=0 ;;
6) alphabet="02 03 04 05 06"
count=0 ;;
7) alphabet="02 03 04 05 06 07"
count=0 ;;
8) alphabet="02 03 04 05 06 07 08"
count=0 ;;
esac
john korterman
Honored Contributor

Re: Please help me acheive Streamlined Counting

Hi,

perhaps this example may help:

#!/usr/bin/sh
typeset -Z2 i=01
typeset -i NUMBER=0
NUMBER=9
OUT=./count.txt

case $NUMBER in
1) continue;;
*) while [ $i -lt $NUMBER ]
do
i=$(($i + 1))
echo $i
done >>$OUT
;;
esac



regards,
John K.
it would be nice if you always got a second chance
Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

john korterman,

I see that you are using /usr/bin/sh for the script. Is this supposed to work in ksh?

I incorperated this into my script, and it broke a couple of things. I think the problem is that the $NUMBER variable was set earlier in my script, and this modifies it, things get thrown off. What exactly is the typeset stuff for?



Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

I forgot to mention, that it does the count beautifully, when I use it like so:

typeset -Z2 i=01
typeset -i NUMBER=0
NUMBER=`cat number.txt`
OUT=count.txt

case $NUMBER
in
1) continue ;;
*) while [ $i -lt $NUMBER ]
do
i=$(($i + 1))
echo $i
done > $OUT
;;
esac

Here is the output of count.txt when $NUMBER is 5:
02
03
04
05


The problems I encounter is later in the script, where I have a function setup to do the following:

## Create a function that adds the sizes of all your disks together, and subtracts 5104.
NUMBER=`cat number.txt`
function adder_func
{
TYPE=$1
if [ -s sizes.$TYPE ]; then
let sum=0
for i in `cat sizes.$TYPE`
do
((i=$i - 5104))
((new_sum=$i + $sum))
sum=$new_sum
done
((last_sum=$new_sum * $NUMBER))
echo ${last_sum}
else
break
fi
}

## Call the adder_func function and output to value.txt to get the max size for your lvol.

adder_func size > value.txt


Here is the output of sizes.size:
524288000

Here is the output of value.txt
20

Based on the function, the output of value.txt should be:
2621414480

Just to make sure I ran the numbers:

root@server:/root_home> bc -l
524288000-5104
524282896
524282896*5
2621414480


Under the old way I was doing the count, it was working just fine. Now the number is off, and I don't know why. Please help!!


Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

john korterman
Honored Contributor

Re: Please help me acheive Streamlined Counting

Hi again Patrick,

#!/usr/bin/sh is the posix shell
The posix-shell enables use of typeset for variable definition, which may also work in ksh. However, the basic idea in
typeset -Z2 i=01
is to define a two-field integer, where a one digit number is preceded by zero

The other definition
typeset -i NUMBER=0
simply marks NUMBER as an integer, which I thought looked quite natural under the circumstances, but if it breaks something later in your script it is probably because of attempts of assigning other than pure integers to it.

Could you please post an example of the content of sizes.$TYPE
And what goes wrong in the adder_func?


regards,
John K.
it would be nice if you always got a second chance
Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

I posted an example of sizes.$TYPE above already:

$TYPE=size when I run the function:

adder_func size > value.txt

So sizes.$TYPE becomes sizes.size.

Here is the output of sizes.size:
524288000
Dennis Handly
Acclaimed Contributor

Re: Please help me achieve Streamlined Counting

>john: typeset -Z2 i=01
>is to define a two-field integer, where a one digit number is preceded by zero

Be very careful about using this trick. This caused errors for set_parms for the years 08 and 09:
http://h30499.www3.hp.com/t5/System-Administration/Change-the-System-Date-and-Time-in-11-31/m-p/4150450

Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

Here is what I ended up using to get my script working correctly:
-----------------------------------------------
## Increment the disk counter based on $NUMBER, and output to disks3.txt
> disks3.txt
CURRENT=0
while [ $CURRENT -le $NUMBER ]
do
if [ $CURRENT -gt 1 ]
then
lv_val=$(printf "%.2d" $CURRENT)
##Capture disk names, all except the first disk in the disk group
echo ${DISKGROUP}$lv_val >> disks3.txt
fi
CURRENT=$(($CURRENT + 1))
done
---------------------------------------------
Dennis Handly
Acclaimed Contributor

Re: Please help me achieve Streamlined Counting

CURRENT=$(($CURRENT + 1))

You can simplify this to:
(( CURRENT += 1 ))

And if you want performance add at the top:
typeset -i CURRENT

Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

Sorry to bump an old thread, but how would I get the output in hexidecimal? I'm trying to make a script for disaster recovery exercises, and I want to be able to run mknod to create my group files for volume groups in LVM in a single swoop. Any fast help would be appreciated!!
James R. Ferguson
Acclaimed Contributor
Solution

Re: Please help me acheive Streamlined Counting

Hi Patrick:

You can use 'printf' to convert to hexadecimal output. For example:

# printf "%0x\n" 15
f
# pritnf "%0X\n" 15
F
printf "%02x\n" 10
0a

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

Would I plug that in at the end?
Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

Wait, I understand..
Patrick Ware_1
Super Advisor

Re: Please help me acheive Streamlined Counting

And this to directly address the issue:

#printf "%02x0000\n" 10
0a0000
Dennis Handly
Acclaimed Contributor

Re: Please help me achieve Streamlined Counting

>but how would I get the output in hexadecimal?

A real shell will allow you to print in hex with:
$ typeset -i16 CURRENT=20; echo $CURRENT
16#14