The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
For Loop with Strings as parameters kadishmj Shell Programming and Scripting 6 10-06-2008 04:57 AM
While Loop Logic sambakamba Shell Programming and Scripting 4 09-02-2008 05:15 PM
Request to modify script to list multiple parameters for V_fieldid variable Sammy Shell Programming and Scripting 0 03-30-2008 09:08 PM
Parameters in loop mgirinath Shell Programming and Scripting 3 03-18-2008 10:20 AM
Grep on multiple parameters kingofprussia UNIX for Dummies Questions & Answers 5 05-21-2007 09:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-30-2008
ankimo ankimo is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 23
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 (permalink)  
Old 10-09-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886

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?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0