Noob stuck - where do I put my loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Noob stuck - where do I put my loop
# 1  
Old 06-02-2010
Noob stuck - where do I put my loop

I'm a noob working on a script to take 3 user inputs. One of them is simply a variable: "poolname". The other 2 are cases: "groupa/groupb" and "enable/disable". "groupa" and "groupb" reference 2 files - groupa.txt or groupb.txt. These files simply consist of a list of server IP addresses and port numbers.

Based on the inputs, the script executes a command but, it needs to repeat the command once for each line in "groupa.txt" or "groupb.txt".

I've got the basics of it working, but the problem I'm having is with the loop to execute the command for each line in the list. Should I be doing this with a function or array or something else? Am I going about this all wrong? Smilie For now, I have the loop at the "group" case, but the script is not working correctly with the loop there.
Code:
!/bin/sh -x
usage() {
  echo "Usage: $1 <poolname> <group_a|group_b> <enable|disable>"
}
action() {
b pool $1 member $2 session $3
}
if [ $# -ne 3 ]; then
  usage $0
  exit 1
fi
POOLNAME=$1
GROUP=$2
VERB=$3
case "$GROUP" in
  "group_a")
     GROUP=`for i in $(cat groupa.txt); do echo $i; done`
    ;;
  "group_b")
     GROUP=`for i in $(cat groupb.txt); do echo $i; done`
    ;;
  *)
    echo "Unknown action $GROUP"
esac
case "$VERB" in
  "enable")
    action $POOLNAME $GROUP $VERB
    ;;
  "disable")
    action $POOLNAME $GROUP $VERB
    ;;
  *)
    echo "Unknown action $VERB"
esac

# 2  
Old 06-02-2010
Hi,
I'm not sure where to start since I don't know what "b" does and I ignore syntactic sugarlessness. But it feels like You should get along well with something like (skipping syntax check):

Code:
#!/bin/sh
while read line; do 
b pool $1 member $line session $3
done < $2.txt

This will read every line from the file called either groupa.txt or groupb.txt and run the command b for each line.

Could that help?

Best regards,
Lakris
# 3  
Old 06-02-2010
Think whole script can be written
Code:
#!/bin/bash
[ $# -ne 3 ] && { echo "Usage: $0 <poolname> <group_a|group_b> <enable|disable>"; exit 1; }
[ $2 != group_a ] && [ $2 != group_b ] && { echo "Unknown action $2"; exit 1; }
[ $3 != enable ] && [ $3 != disable ] && { echo "Unknown action $3"; exit 1; }
while read L
do
    b pool $1 member $L session $3
done < ${2/_/}.txt # to remove the '_' from parameter $2

I added exit 1 when parameters didn't match script requests.
Think parameter 2 could be "groupa" or "groupb" to transform the
Code:
done < ${2/_/}.txt
# in
done < $2.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop -- noob question

Hello, I am new to shell scripting and i am trying to figure why is this not working with else statement. I am searching for every directory in that DIR i am in, however the "else" seems to be triggered whenever the run the script.. Much thanks in advance! #!/bin/shell for item in... (3 Replies)
Discussion started by: Reb0rn
3 Replies

2. Shell Programming and Scripting

How to put html frames in for loop in perl?

Hi, I have to insert html frames in for loop. Here is the code. for($k=0;$k<3;$k++) { print<<HTML; <html> <head> <title> HTML Horizontal Frames </title> </head> <frameset cols="25%,75%"> <frame src="a.html"> <frame src="b.html"> </frameset> (0 Replies)
Discussion started by: vanitham
0 Replies

3. Shell Programming and Scripting

perl loop keeps getting stuck

I am using a Perl script to open a series of files in a loop, separate the paragraph into lines, and output the lines into a new file. The code works perfectly fine, except when the source file is over a certain size the loop gets stuck and won’t move on to the next file. It still does what it is... (0 Replies)
Discussion started by: renthead720
0 Replies

4. Shell Programming and Scripting

Script Stuck In Loop

Hi all! Im trying to get this script to check for folders in a year/month/day folder structure and if the day doesnt exist then it makes the day. It will also make sure all of the days before todays date exist as well. This script assumes that the month and year folder already exist. It works... (3 Replies)
Discussion started by: Grizzly
3 Replies

5. Shell Programming and Scripting

How to put for loop in nawk

Hello All, How i can put loop in nawk. what i want is that i define a variable which contain vlaues like var='1 2 3 4 5' and then define for loop which gives vlaue to nawk one by one to varilable inside nawk and then print it. cat /omp-data/logs/5etr/081121.APX | nawk -v CDNLIST='700 701' ' ... (1 Reply)
Discussion started by: wakhan
1 Replies

6. Shell Programming and Scripting

how can i put the condition in for loop for the below.

i have the equation like below 07:35:07 ( AB : 2319f.ab * 22) + ( AB : 2320f.ab * 22.03 ) + ( AB :2321f.ab * 22.07 ) ...... N i want put ":" as a delimiter and break the equation like below 2319f.ab * 22 2320f.ab *22.03 2321f.ab * 22.07 . . N i know the number of... (1 Reply)
Discussion started by: mail2sant
1 Replies

7. UNIX for Dummies Questions & Answers

What condition to be put in the while loop?

i have got a file where the env command is appended 5 times. i have to now look for the username and display it in the form of 1) PWD=/home/lee.ballancore 2) USER=lee.ballancore 3) MAIL=/var/spool/mail/lee.ballancore 4) LOGNAME=lee.ballancore 5) HOME=/home/lee.ballancore 6)... (1 Reply)
Discussion started by: nehaquick
1 Replies

8. Shell Programming and Scripting

Shell Noob

Hi all, I am trying to write a shell script that will move files from one directory to another, the only thing is I want to to check loads of different source directory and move the files to loads of different directories. I am totally new to shell scripts but not to UNIX (although I would... (16 Replies)
Discussion started by: Sax
16 Replies

9. UNIX for Dummies Questions & Answers

Menu function stuck in a loop?

I having problem when I call this cleanupmenu function within a script. It continuously loops and goes to selection * That wasn't a valid selection. I have to kill it everytime to stop it. What am I doing wrong. I use this function menu in several other scripts and I don't have this problem at... (2 Replies)
Discussion started by: darthur
2 Replies
Login or Register to Ask a Question