for loop logic with multiple parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop logic with multiple parameters
# 1  
Old 09-30-2008
for loop logic with multiple parameters

hi, unix wizards,

i have a question about the logic of my inner for loop below. first, what i am trying to do is to write a script called create_account that automatically creates mysql accounts. the user can provide a user_name or a group_id as an argument (and the script can take multiple arguments). user_name arguments should simply be appended to a user_file.

group ids should be compared to a file that contains a list of group_ids with multiple user_names per id. if the group id exists, then grab all the users and append to the user_file. if the group id does not exist, print "group id not valid".

of course, the first thing i have to do is validate the argument -- if its a number of a certain type, i assume its a group_id. else, it's a user_name. btw, "is_num" is a function to validate if the argument is a number (and hence, a group_id).


----------------------------------------------
get arg

  • if argument is a number
    • then validate group_id
    • if num is invalid group_id,
      • then print "group id not valid"
    • if num is valid group_id
      • then parse group_id_file and grab users from 4th field on
  • else
    • append to user_file
----------------------------------------------


my code works well when i have just one arguement and it doesn't matter if the arguement is valid group_id, an invalid group_id, or a user_name. but when i use multiple parameters, the problems with my code are:

  1. user_name arguments are getting passed through the inner for loop and shouldn't be because they don't pass the is_num function criteria
  2. commands with multiple parameters run all arguments multiple times
  • for example, if i say...
    Code:
    create_acct valid_num invalid_num

    ... i get a list with all the valid users times 2
i wonder if anyone has the time to point me in the right direction. thanks for your help.



Code:
for arg; do
  if is_num $arg; then

#----------------------------------------------

   for num; do
      a=$(grep "^c$num:" group_file)

      if [ -z "$a" ]; then
        print "Group ID " $num "is not in group file."
        continue
      fi

      f4=$(echo $a | cut -d: -f4)

      if [ -z "$f4" ]; then
        print "Group ID " $num "does not have any users."
      else
        print $f4 | tr ',' '\n' >> user_list
        continue
      fi
    done

#----------------------------------------------

  else
    echo $arg >> user_list
  fi
done

# 2  
Old 10-09-2008
Code:
for arg; do
  if is_num $arg; then

   for num; do
     ....
   done

This looks like sh/ksh/bash scripting. The question is, what are "arg" and "num"?? is this in a shell function or is this found in the "main" part of the shell script?
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell/Perl logic for loop

Hi, I have a requirement as follows. Have 3 files. Need to match up the data in each one of them and sum up the data by a field and display it. example given below. File 1 : Name, Emp id File 2 : Empid, Subject, File 3 : Subject, Score, Class Match Emp id in File 1 and File 2 and then... (7 Replies)
Discussion started by: preethgideon
7 Replies

2. Shell Programming and Scripting

while loop logic

Hi, Here I am trying to query database and check a value, if the value not matches then I wants to re-query the database.Once the value matches, I want to email the reqidstatus_log.txt file. Database query produces a file reqidstatus_log.txt which contains result. But the query not working as... (3 Replies)
Discussion started by: rajsp217
3 Replies

3. UNIX for Dummies Questions & Answers

if then else logic with while loop problem

Hi Friends, I have to do write a shell file based on one flag.If that flag value is 'N' then process look in $DATA are and the normal process continue.If vaule is 'P' then it check for the files in different location $CONV and move those file in $DATA area and rest of the process... (2 Replies)
Discussion started by: Param0073
2 Replies

4. Shell Programming and Scripting

loop logic inside of an inline redirect?

i need to log the feedback from the ftp server as i'm performing some deletes. the only way i know of to do this is with the inline redirect << EOF ... but from there to the closing EOF, it's like i'm at the ftp command prompt, so I don't know how to have ksh script logic in there I have an... (3 Replies)
Discussion started by: tlavoie
3 Replies

5. Shell Programming and Scripting

bash if loop for checking multiple parameters

Hello, I've got next problem: I want to examine at the beginning of a script in an if loop that: 1. Is there 4 parameters given 2. If first state is true then: is there switches -e and -d? 3. At the end, how can i indentify them as variebles regardlees to its order. I was thinking like... (2 Replies)
Discussion started by: szittyafergeteg
2 Replies

6. Shell Programming and Scripting

While Loop Logic

I would need to with making while loop logic working in shell program when I am new into the shell programing 1) I would need to try to get the file from the remote side ----need to try 15 mins apart for 4 times and terminate the program if file is not available.... I would need to know how I... (4 Replies)
Discussion started by: sambakamba
4 Replies

7. Shell Programming and Scripting

Parameters in loop

Hi, I am trying to write a script which will read inputs form user and process those files, I have issue reading the input parameters in a loop. Following is the script... I run the script as ./Script.sh 3 table1 table 2 table3 NumberOfTables=$1 let TableCount=1 while do ... (3 Replies)
Discussion started by: mgirinath
3 Replies
Login or Register to Ask a Question