Help me streamline this counting part of my script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help me streamline this counting part of my script.
# 1  
Old 02-25-2008
Help me streamline this counting part of my script.

Ok, 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?

In this case, $NUMBER = 8

Code:
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


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
# 2  
Old 02-25-2008
Tools you could use a case statement

Code:
case $number
   in
      1) ;;
      2) alphabet="02";;
      3) alphabet="02 03";;
      4) alphabet="02 03 04";;
      5) alphabet="02 03 04 05";;
      6) alphabet="02 03 04 05 06";;
      7) alphabet="02 03 04 05 06 07";;
      8) alphabet="02 03 04 05 06 07 08";;
esac

# 3  
Old 02-25-2008
Quote:
Originally Posted by joeyg
Code:
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

Fixed it...

Thanks for the reply. It definitely takes up less space, but I am still limited to a count of 8. I could add more lines, but I don't want to be limited by the number of lines I have in the file.

Last edited by LinuxRacr; 02-26-2008 at 03:09 AM..
# 4  
Old 02-26-2008
Question Why set Count with each case?

Count will 'default' to zero, you do not set it for the first condition. But, you feel you must set within each case possibility? Why?
Couldn't you set it just once outside of the case statement?

Also, is your pupose here to simply create the count.txt file? There are easier ways to accomplish that.
# 5  
Old 02-26-2008
I suppose you are correct. I put the counts back because of a syntax error I was getting. Let me try the script by taking out all counts, and putting it outside the "tree".
# 6  
Old 02-26-2008
Question continue statement

A 'continue' is normally only used (and needed) while inside some kind of loop - causing the execution to skip the rest of the processing and continue with the next iteration. Thus, your 'continue' may be not needed.

What is you final desired result? That count.txt file?
# 7  
Old 02-26-2008
I put the counts back because of a syntax error I was getting.

What error?? don't forget you need the keep the ;; in each of the selections for a case statement.

If you really want to be unlimited, why use if or case?

You could set a variable, say MAX_CNT to the input supplied to your script,
set alphabet=2, and create a "while loop <less than or equal to MAX_CNT" add one more to alphabet. This way there's no limit at all to your script.
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Streamline script to search for numbers in a certain range

Hello all, I need help making a script run faster since I have a huge file to sift through. The file I am running my script on looks like this: 1 -1.9E+001 -1.8E-001 1.5E+001 3.32E+001 2 -1.7E+001 -1.0E-002 1.2E+001 6.37E+001 3 -1.5E+001 -3.8E-006 6.7E+001 4.81E+001 The... (12 Replies)
Discussion started by: butson
12 Replies

2. Shell Programming and Scripting

counting in shell script

hi i am writing a korn shell script to compile all programs there are arount 2000+ files now i have a script that can compile programs but i want to show status of compilation because to compile all programs it takes around 10-15 mins so how can i display status like it should print no of files... (3 Replies)
Discussion started by: zedex
3 Replies
Login or Register to Ask a Question