Bash For Loop Examples

by Vivek Gite · 60 comments

How do I use bash for loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using for statement? How do I use three-parameter for loop control expression?

A 'for loop' is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script.

For example, you can run UNIX command or task 5 times or read and process list of files using a for loop. A for loop can be used at a shell prompt or within a shell script itself.

for loop syntax

Numeric ranges for syntax is as follows:

for VARIABLE in 1 2 3 4 5 .. N
do
	command1
	command2
	commandN
done

This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:

#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done

Sometimes you may need to set a step value (allowing one to count by two's or to count backwards for instance). It can be done easily with seq command. A representative example in bash as follows:

#!/bin/bash
for i in $(seq 1 2 20)
do
   echo "Welcome $i times"
done

Latest bash version 3.0+ has inbuilt support for setting up ranges:

#!/bin/bash
for i in {1..5}
do
   echo "Welcome $i times"
done

Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:

#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
  do
     echo "Welcome $i times"
 done

Sample outputs:

Bash version 4.0.33(0)-release...
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times

4.0.33(0)-release

Three-expression bash for loops syntax

This type of for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3).

for (( EXP1; EXP2; EXP3 ))
do
	command1
	command2
	command3
done

A representative three-expression example in bash as follows:

#!/bin/bash
for (( c=1; c<=5; c++ ))
do
	echo "Welcome $c times..."
done

Sample output:

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

How do I use for as infinite loops?

Infinite for loop can be created with empty expressions, such as:

#!/bin/bash
for (( ; ; ))
do
   echo "infinite loops [ hit CTRL+C to stop]"
done

Conditional exit with break

You can do early exit with break statement inside the for loop. You can exit from within a FOR, WHILE or UNTIL loop using break. General break statement inside the for loop:

for I in 1 2 3 4 5
done
  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.
  statements2
  if (disaster-condition)
  then
	break       	   #Abandon the loop.
  fi
  statements3          #While good and, no disaster-condition.
done

Following shell script will go though all files stored in /etc directory. The for loop will be abandon when /etc/resolv.conf file found.

#!/bin/bash
for file in /etc/*
do
	if [ "${file}" == "/etc/resolv.conf" ]
	then
		countNameservers=$(grep -c nameserver /etc/resolv.conf)
		echo "Total  ${countNameservers} nameservers defined in ${file}"
		break
	fi
done

Early continuation with continue statement

To resume the next iteration of the enclosing FOR, WHILE or UNTIL loop use continue statement.

for I in 1 2 3 4 5
done
  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.
  statements2
  if (condition)
  then
	continue   #Go to next iteration of I in the loop and skip statements3
  fi
  statements3
done

This script make backup of all file names specified on command line. If .bak file exists, it will skip the cp command.

#!/bin/bash
FILES="$@"
for f in $FILES
do
        # if .bak backup file exists, read next file
	if [ -f ${f}.bak ]
	then
		echo "Skiping $f file..."
		continue  # read next file and skip cp command
	fi
        # we are hear means no backup file exists, just use cp command to copy file
	/bin/cp $f $f.bak
done

Recommended readings:

  • See all sample for loop shell script in our bash shell directory.
  • man bash
  • help for
  • help {
  • help break
  • help continue

Updated for accuracy!

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 60 comments… read them below or add one }

1 Jadu Saikia 11.02.08 at 3:37 pm

Nice one. All the examples are explained well, thanks Vivek.

seq 1 2 20
output can also be produced using jot

jot – 1 20 2

The infinite loops as everyone knows have the following alternatives.

while(true)
or
while :

//Jadu

2 Sean 11.04.08 at 2:20 am

The last example can also be produced without the ” in $FILES”:

#!/bin/sh

for f
do

# For-Loop body

done

If the ” in …” is excluded, the loop will run as if “in $@” was given.

3 Andreas 11.13.08 at 4:53 am

Nice explanation tutorial.

4 Manish 11.25.08 at 6:33 am

hey vivek i tried the following syntax for for loop suggested by u but both dint work…
1.
#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo “Welcome $c times…”
done

2.
#!/bin/bash
for i in {1..5}
do
echo “Welcome $i times”
done

got error for both the syntax
1. unexpected ‘(‘
2. it printed welcome {1..5} times instead repeating it…

help..?

5 lascost 12.06.08 at 6:15 pm

i tried the last example but i seen dint work

#!/bin/bash 

set -x

FILLES="$@"
CP=$(which cp)
for f in $FILES
do
        if [ -f ${f}.bak ]
        then
                echo "skiping $f file"
                continue # read netxt file and skip cp command
        fi
        $CP $f $f.bak
done

i would like know where is the error

6 Vivek Gite 12.13.08 at 4:56 pm

Replace

FILLES="$@"

With

FILES="$@"
7 lo2y 01.28.09 at 10:10 am

hi guys . can any one help me . i need a script to check the file /var/log/messages every 10 minutes .and if its has the following log :
ext3_orphan_cleanup: deleting unreferenced

to apply the following command
sendsms to wut ever .

thnx alot

8 swatkat 02.12.09 at 2:06 pm

i would like to breakk a csv file depending upon two criteria.

1. SIngle file should not be more than 100 lines
2. The third column if has same value on the 100th line as that of the 101th line, the complete line should be included in the 2nd file.
so., now, 1st file will have 99 lines and 2nd file will have 100 lines, ifthe above 2nd condition does not repeats.,

9 archana 03.16.09 at 11:53 pm

for file in $(ls 0902*0010202.TLG); do
day=$(echo $file | cut -c 1-6)
grep ^203 $file | cut -d, -f3 | sort | uniq -c | while read line; do
cnt=$(echo $line | cut -d” ” -f1)
acct=$(echo $line | cut -d” ” -f2)
echo “Date 20${day} Account ${acct} had ${cnt} 203’s” >> Feb_report.txt
done
done

when i run it it gives me a syntax error
ins@ARTMGA01> ./arc.sh
./arc.sh: syntax error at line 4: `$’ unexpected

could you help

10 Navneet 04.20.09 at 10:57 am

Good examples!! easily understood

11 firmit 06.07.09 at 5:06 pm

Hi

How do I read line by line in a file, and use these in a loop? I have a file I read in (cmd max_cpu):
firefox 15
conky 1
cmds=$(cat file)
But $cmds now consist of n items, all being “equal” – it does not split on each line to a new array. I expected that by looping over $cmds, I’d get a 2D array…. I did not.

Otherwise, excellent tutorial!

12 Vivek Gite 06.07.09 at 5:56 pm

Try:

FILE=/etc/passwd
while read line
do
	# store field 1
	F1=$(echo $line|cut -d$FS -f1)
	# store field 2
	F2=$(echo $line|cut -d$FS -f6)
	# store field
	F3=$(echo $line|cut -d$FS -f7)
	echo "User \"$F1\" home directory is $F2 and login shell is $F3"
done < $FILE
13 firmit 06.07.09 at 6:03 pm

Excellent! Thanks Vivek.

14 Peko 07.16.09 at 6:11 pm

Hi Vivek,
Thanks for this a useful topic.

IMNSHO, there may be something to modify here
=======================
Latest bash version 3.0+ has inbuilt support for setting up a step value:

#!/bin/bash
for i in {1..5}
=======================
1) The increment feature seems to belong to the version 4 of bash.
Reference: http://bash-hackers.org/wiki/doku.php/syntax/expansion/brace
Accordingly, my bash v3.2 does not include this feature.

BTW, where did you read that it was 3.0+ ?
(I ask because you may know some good website of interest on the subject).

2) The syntax is {from..to..step} where from, to, step are 3 integers.
You code is missing the increment.

Note that GNU Bash documentation may be bugged at this time,
because on GNU Bash manual, you will find the syntax {x..y[incr]}
which may be a typo. (missing the second “..” between y and increment).

see http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion

The Bash Hackers page
again, see http://bash-hackers.org/wiki/doku.php/syntax/expansion/brace
seeems to be more accurate,
but who knows ? Anyway, at least one of them may be right… ;-)

Keep on the good work of your own,
Thanks a million.

– Peko

15 Vivek Gite 07.16.09 at 6:31 pm

@ Peko,

Thanks for pointing out ranges vs step value. I’ve updated the FAQ.

16 Peko 07.16.09 at 8:04 pm

Yes.

But you mispelled the syntax with an extra dot “.” after “START’
not {START…END..INCREMENT}
but {START..END..INCREMENT}

;-)
– Peko

17 Michal Kaut 07.22.09 at 6:12 am

Hello,

is there a simple way to control the number formatting? I use several computers, some of which have non-US settings with comma as a decimal point. This means that
for x in $(seq 0 0.1 1) gives 0 0.1 0.2 … 1 one some machines and 0 0,1 0,2 … 1 on other.
Is there a way to force the first variant, regardless of the language settings? Can I, for example, set the keyboard to US inside the script? Or perhaps some alternative to $x that would convert commas to points?
(I am sending these as parameters to another code and it won’t accept numbers with commas…)

The best thing I could think of is adding x=`echo $x | sed s/,/./` as a first line inside the loop, but there should be a better solution? (Interestingly, the sed command does not seem to be upset by me rewriting its variable.)

Thanks,
Michal

18 Peko 07.22.09 at 7:27 am

To Michal Kaut:

Hi Michal,

Such output format is configured through LOCALE settings.

I tried :

export LC_CTYPE=”en_EN.UTF-8″; seq 0 0.1 1

and it works as desired.

You just have to find the exact value for LC_CTYPE that fits to your systems and your needs.

Peko

19 Peko 07.22.09 at 2:29 pm

To Michal Kaus [2]

Ooops – ;-)
Instead of LC_CTYPE,
LC_NUMERIC should be more appropriate
(Although LC_CTYPE is actually yielding to the same result – I tested both)

By the way, Vivek has already documented the matter : http://www.cyberciti.biz/tips/linux-find-supportable-character-sets.html

– Peko

20 VIKAS 07.22.09 at 3:58 pm

Excellent stuff… keep up the good work.

21 Brad Landis 10.26.09 at 3:26 pm

Comment 12 was really helpful. I was trying to split up a log file by date, such as
logfile.20091026 , without having to use grep a million times. I’m kind of disappointed I couldn’t find a one-liner to do so, but I will take what I can get :).

22 Vivek Gite 10.26.09 at 4:47 pm

@Brad,

Try this without grep or cut using bash parameter expansion :

file="logfile.20091026"
log="${file%%.*}"
date="${file##*.}"
echo $log
echo $date

HTH

23 Philippe Petrinko 10.26.09 at 4:52 pm

H i vivek,

Just wondering why you don’t amend the typo I pointed out:
<>

24 Brad Landis 10.26.09 at 4:53 pm

I think you misunderstood. I’m going line by line, and converting the dates at the beginning of the line, such as “Sep 12″, and copying that line from logfile to logfile.20090912.

My script is really slow though, with the conversion of the month name to a number. I’ve tried using the date command, and my own function, and both take 7 seconds to process 10,000 lines. It doesn’t seem like a long time, but I’ve got a lot of log files to process on multiple machines.

I don’t guess you’d know a faster trick, would you?

25 Vivek Gite 10.26.09 at 6:37 pm

@Brad, yes, I did misunderstood your post. If I were you I will try out awk.

@Philippe,
Thanks for the heads up. The faq has been updated.

26 TheBonsai 10.30.09 at 6:13 am

@Peko:

(I’m the operator of bash-hackers.org/wiki, that’s why I found this page):

Regarding Bash documentation for brace expansion (increment syntax), actually I’m right and the documentation is wrong (a rare situation!). I reported it to the list.

27 Philippe Petrinko 10.30.09 at 8:35 am

To Vivek:
Regarding your last example, that is : running a loop through arguments given to the script on the command line, there is a simplier way of doing this:
# instead of:
# FILES=”$@”
# for f in $FILES

# use the following syntax
for arg
do
# whatever you need here – try : echo “$arg”
done

Of course, you can use any variable name, not only “arg”.

To TheBonsai: Welcome Buddy!
Fine! I am happy to see 2 great FOSS web sites now related !

28 tdurden 11.10.09 at 9:32 pm

Command line while loop.. Very handy..

Say you wanted to rename all the files in a specific dir..
Create a file with the contents you want to rename
(ls -l | awk ‘{print $9}’ > asdf or something)

Contents of asdf:
file1
file2
file3
file4

cat asdf | while read a ; do mv $a $a.new ; done

ls -l
asdf file1.new file2.new file3.new file4.new

I have used this while command for many things from simply renaming files to formatting and labling new SAN luns..

29 TheBonsai 11.11.09 at 7:07 am

There are 2 problems and one optical flaw with your code:

(1) You should use read -r without any variable name given, to use the default $REPLY (due to a specific behaviour of read, see manpage)
(2) You should quote $a
(3) Useless use af cat :)

30 Philippe Petrinko 11.11.09 at 11:25 am

To tdurden:

Why would’nt you use

1) either a [for] loop
for old in * ; do mv ${old} ${old}.new; done

2) Either the [rename] command ?
excerpt form “man rename” :

RENAME(1) Perl Programmers Reference Guide RENAME(1)

NAME
rename – renames multiple files

SYNOPSIS
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

DESCRIPTION
“rename” renames the filenames supplied according to the rule specified
as the first argument. The perlexpr argument is a Perl expression
which is expected to modify the $_ string in Perl for at least some of
the filenames specified. If a given filename is not modified by the
expression, it will not be renamed. If no filenames are given on the
command line, filenames will be read via standard input.

For example, to rename all files matching “*.bak” to strip the
extension, you might say

rename ’s/\.bak$//’ *.bak

To translate uppercase names to lower, you’d use

rename ‘y/A-Z/a-z/’ *

– Philippe

31 TheBonsai 11.11.09 at 11:49 am

Note for rename(1): There exist two major variants on Linux system. One non-Perl originating in the RedHat area, and one Per, originating in the Debian area.

32 Sean 11.11.09 at 2:42 pm

To tdurden:

I would also replace “ls -l | awk ‘{print $9}’” with just “ls”. Otherwise you’ll run into issues with files that have spaces in it. As far as using:

for i in *;
vs
for i in $(ls);

I personally prefer “$(ls)” or “$(find . )”. This provides more control over what files I’m going to be looping through. For instance:

$(ls -F | grep -v “\/$”)
or
$(ls -A)

33 Philippe Petrinko 11.11.09 at 3:03 pm

To Sean:
CMIIAW :

try the following commands:
# touch “file with spaces in name”
# for f in *; do echo “”;done

which shows that there is no need to use [for f in $(ls)] instead of [ for f in *]
Doesn’t it ?

– Philippe

34 Philippe Petrinko 11.11.09 at 3:14 pm

Sorry Sean, my last post was truncated,
due to limitations of this form used to post comments. (impossible to use Greater_than and Less_than caracters)

I meant, use the following:
# touch “file with spaces in name”
# for f in *; do echo “:${f}:”;done

:file with spaces in name:

35 Sean 11.11.09 at 8:10 pm

Sorry for the confusion, I understand that “for i in *;” will not have any issues with spaces. I was referring to the ls -l | awk ‘{print $9}’ having issues with spaces. The reason I choose to use $(ls) instead of * is for filtering out unwanted files e.g. $(ls -F | grep -v “\/$”)

36 Philippe Petrinko 11.11.09 at 9:16 pm

To Sean:

But then, that’s wrong.
[ for f in $(ls -F|grep -v "V$") ]
won’t process appropriately spaces in filename.
Check :
# touch “file with spaces in name”
# for f in $(ls -F|grep -v “V$”); do echo “:${f}:”;done
:file:
:with:
:spaces:
:in:
:name:

37 Philippe Petrinko 11.11.09 at 9:18 pm

The best tool to filter files and process them
is [find] piped to [xargs] (with zero-ended filenames)

38 Philippe Petrinko 11.11.09 at 9:25 pm

To sean:
But if you want to exclude files from globbing,
[bash] has the [extglob] option.

Let’s say you want to process every file except files ending by a “V”, just type

# for f in !(*V); do echo “:${f}:”;done

39 Philippe Petrinko 11.11.09 at 9:27 pm

If you set the shell option extglob, Bash understands some more powerful patterns. Here, a is one or more pattern, separated by the pipe-symbol (|).

?() Matches zero or one occurrence of the given patterns
*() Matches zero or more occurrences of the given patterns
+() Matches one or more occurrences of the given patterns
@() Matches one of the given patterns
!() Matches anything except one of the given patterns

40 Philippe Petrinko 11.11.09 at 9:28 pm
41 Narender 11.12.09 at 6:47 am

I have two files here X.a and y.a Now what i need is i need to substitute CvfdsDisk_sdb/c/d/e in lines of Node CvfsDisk_XXX in the order CvfsDisk_sdb/c/f/g first word of each line of x.a exists. how can i do in shell scripting i can get the first word of each line of X.a using awk /cut but to replace these in y.a i am not getting it … any help here ?
[Raj]$ cat x.a
CvfsDisk_sdb /dev/sdb # host 0 lun 1 sectors 4840746976 sector_size 512 inquiry [AMCC 9550SX-12M DISK 3.08] serial AMCC ZAJBSXJFF92A9D003C6A
CvfsDisk_sdc /dev/sdc # host 0 lun 0 sectors 3906148319 sector_size 512 inquiry [AMCC 9550SX-12M DISK 3.08] serial AMCC ZAJ8MJKFF92A9D001FEC
CvfsDisk_sdf /dev/sdf # host 0 lun 1 sectors 4840746976 sector_size 512 inquiry [AMCC 9550SX-12M DISK 3.08] serial AMCC ZAJBSXJFF92A9D003C6A
CvfsDisk_sdg /dev/sdg # host 0 lun 0 sectors 3906148319 sector_size 512 inquiry [AMCC 9550SX-12M DISK 3.08] serial AMCC ZAJ8MJKFF92A9D001FEC
[naren@Beas dxall]$ cat y.a

[StripeGroup Metafiles]
Metadata Yes
Status UP
Read Enabled
Write Enabled
Journal Yes
StripeBreadth 1280K
Node CvfsDisk_sdb 0

[StripeGroup datafiles1]
Metadata Yes
Status UP
Read Enabled
Write Enabled
StripeBreadth 1024K
Node CvfsDisk_sdc 0

[StripeGroup datafiles2]
Metadata Yes
Status UP
Read Enabled
Write Enabled
StripeBreadth 1280K
Node CvfsDisk_sdd 0

[StripeGroup datafiles3]
Metadata Yes
Status UP
Read Enabled
Write Enabled
StripeBreadth 1024K
Node CvfsDisk_sde 0

42 Vivek Gite 11.12.09 at 1:47 pm

@Narender,

Your post is offtopic. I suggest you use our shell scripting forum for question.

43 Sean 11.12.09 at 3:26 pm

To Philippe:

You are right,

# for i in $(ls)

will break up files with spaces if IFS isn’t set to just the newline character. I don’t believe this is consistent across distributions. So the for loop should have

# export IFS=$’\n’

before it. I also use find in for loops when I want to look through the directory contents, but this isn’t always desired. Thanks for the info about extglob, I haven’t done much with extended globbing in bash.

44 Philippe Petrinko 11.12.09 at 3:44 pm

To Sean:
Right, the more sharp a knife is, the easier it can cut your fingers…

I mean: There are side-effects to the use of file globbing (like in [ for f in * ] ) , when the globbing expression matches nothing: the globbing expression is not susbtitued.

Then you might want to consider using [ nullglob ] shell extension,
to prevent this.
see: http://www.bash-hackers.org/wiki/doku.php/syntax/expansion/globs#customization

Devil hides in detail ;-)

45 The_Catalanish 12.04.09 at 11:22 am

Response to the tip number 12
At thos script, It’s missing the followng line
FS=’:’
in the variables declaration
(you forgit the delimiter field, for the cut command)
:-P
The_Catalanish

46 Rilif 12.10.09 at 9:27 pm

#! /usr/bin/ksh
for i in `cat /input`
do
bdf | grep file_system | grep -vE ‘^A|B|C’ | awk ‘{ print $4}’ | while read output;
do
file_system=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1 )
partition=$(echo $output | awk ‘{ print $2 }’ )
if [ $file_system -ge 60 ]; then
echo “don’t run the sync $partition ($file_system%) ”
else
rsync $i
fi
done
done
The problem with the logic I’m having is I do not want the script to exit(as it does now) the loop once the file_system area reaches 60%. I want
it to continue to retest bdf and continue the loop once disk usage drops below 60%.

Any Ideas?

47 Philippe Petrinko 12.11.09 at 12:17 pm

To Rilif:

1) I assume you use [ bdf ] on UNIX system – because Linux equivalent is [ df ] – and I cannot be of help because I cannot test your script on my Linux boxes.

2) This seems to be a specific programming debugging problem and out of this topic scope – There may be a better place to post that kind of topic – A programmer forum for instance.

Best regards.

48 dee 12.13.09 at 10:27 am

do any one know how to write this script in steps?
as so /// ./ test 10 /// The first argument [1] will ex. to create a multiple users, groups, cn, dn, etc for ldap in one or two scripts but from command line. you would just enter file then the number of atrributes to build.
this is a headache for me since i’m new at this.

49 Philippe Petrinko 12.13.09 at 10:54 am

To Dee:

0) The first part of your first sentence is incomprehensible – this may be because text that you entered is altered, it may contain HTML-like syntax that is interpreted by this comment form. (By the way, Vivek Gite would be welcomed to tell us how to prevent this. TIA :-) )

1) LDAP syntax is off-topic.

2) You’ll find appropriate resources in LDAP forums/sites – just find out.

3) We may be in position to help you to build a [for] loop, assuming you do your part of the job by providing the basic LDPA instructions to create user, for instance. Try to create at least a LDAP
object by yourself on the command-line, then provide us the code, and as much as possible further explanation please.

50 Vivek Gite 12.13.09 at 10:58 am

There was no html tag or anything else in comment.

@dee, if you need to attach code use <pre> tags.

51 Philippe Petrinko 12.13.09 at 11:44 am

Thanks Vivek – But I am afraid I do not get it right – what does “pre” mean ? (I understand you wrote the “less than” tag, and “greater than” tag – but why “pre” ?

And are you sure these are the only ones two use ?

52 Vivek Gite 12.13.09 at 1:26 pm

@Philippe,

All allowed html tags are displayed below the form itself. It is wordpress that converts those symbol and syntax is

<pre>#!/bin/bash
echo "Hello world!"
</pre>

HTH

53 dee 12.13.09 at 1:41 pm

first file make: create_user_idif.sh.txt

#!/bin/bash
echo "dn: $2 $3,o=dmacc,dc=bad1dee,dc=com" > $1
echo "changetype: add" >> $1
echo "objectclass: top" >> $1
echo "objectclass: person" >> $1
echo "objectclass: organizationalPerson" >> $1
echo "objectclass: inetOrgPerson" >> $1
echo "cn: $2 $3" >> $1
echo "givenName: $2" >> $1
echo "sn: $3" >> $1
echo "uid: ${2:0:1}$3" >> $1
echo "mail: $4" >> $1
___________________________________
mass_user.txt
Scott,brown,sbrown@bad4dee.com\n
karla,smith,ssmith@bad4dee.com\n
sam,goodie,sgoodie@bad4dee.com\n
marge,jones,mjones@bad4dee.com
____________________________________
mass_add.sh.txt
#!/bin/bash

u1=`cat $1 | tr -d '\n'`
#u2=`echo $u1 | tr \| '\n'`
u2=`echo $u1 | sed s/\|/\\n/`

echo "DBG"
echo -e $u1
echo "DBG"

echo -e $u1 | ( IFS=, ; while read fname lname mail;
do
echo -e "Creating $fname $lname\n\n"

./create_user_ldif.sh.txt ./user.ldif $fname $lname $mail

cat ./user.ldif
echo -e "\n\n"
done )

_____________________________________
./mass_add.sh.txt mass_user.txt
This ex: will pull from a list but same out come i do not know how to write another script to pull the attributes i need from the command line like ./test 100 and that command will pull only a 100 users id’s from idif.txt out of 1000 generated.

This next samples of code will file in the attributes for you.
first file make: create_user_idif.sh.txt

#!/bin/bash
echo "dn: $2 $3,o=dmacc,dc=bad1dee,dc=com" > $1
echo "changetype: add" >> $1
echo "objectclass: top" >> $1
echo "objectclass: person" >> $1
echo "objectclass: organizationalPerson" >> $1
echo "objectclass: inetOrgPerson" >> $1
echo "cn: $2 $3" >> $1
echo "givenName: $2" >> $1
echo "sn: $3" >> $1
echo "uid: ${2:0:1}$3" >> $1
echo "mail: $4" >> $1

____________________________________________________________
Then:

mass_add.sh.txt

#!/bin/bash

u1=`cat $1 | tr -d '\n'`
#u2=`echo $u1 | tr \| '\n'`
u2=`echo $u1 | sed s/\|/\\n/`

echo "DBG"
echo -e $u1
echo "DBG"

echo -e $u1 | ( IFS=, ; while read fname lname mail;
do
echo -e "Creating $fname $lname\n\n"
./create_user_ldif.sh.txt ./user.ldif $fname $lname $mail
cat ./user.ldif
echo -e "\n\n"
done )

_____________________________________________________________________
last:
adduserfile1.txt

user0,boo,us1@bad1dee.com\n
user1,boo,us1@bad1dee.com\n
user2,boo,us1@bad1dee.com\n
user3,boo,us1@bad1dee.com\n
user4,boo,us1@bad1dee.com\n
user5,boo,us1@bad1dee.com\n
user6,boo,us1@bad1dee.com\n
user7,boo,us1@bad1dee.com\n
user8,boo,us1@bad1dee.com\n
user9,boo,us1@bad1dee.com\n
user10boo,us1@bad1dee.com\n
user11,boo,us1@bad1dee.com\n
user12,boo,us1@bad1dee.com\n
user13,boo,us1@bad1dee.com\n
user14,boo,us1@bad1dee.com\n
user15,boo,us1@bad1dee.com\n
user16,boo,us1@bad1dee.com\n
user17,boo,us1@bad1dee.com\n
user18,boo,us1@bad1dee.com\n
user19,boo,us1@bad1dee.com\n
user20,boo,us1@bad1dee.com\n
user21,boo,us1@bad1dee.com\n
user22,boo,us1@bad1dee.com\n
user23,boo,us1@bad1dee.com\n
user24,boo,us1@bad1dee.com\n
user25,boo,us1@bad1dee.com\n
user26,boo,us1@bad1dee.com\n
user27,boo,us1@bad1dee.com\n
user28,boo,us1@bad1dee.com\n
user29,boo,us1@bad1dee.com\n
user30,boo,us1@bad1dee.com\n
user31,boo,us1@bad1dee.com\n
user32,boo,us1@bad1dee.com\n
user33,boo,us1@bad1dee.com\n
user34,boo,us1@bad1dee.com\n
user35,boo,us1@bad1dee.com\n
user36,boo,us1@bad1dee.com\n
user37,boo,us1@bad1dee.com\n
user38,boo,us1@bad1dee.com\n
user39,boo,us1@bad1dee.com\n
user40,boo,us1@bad1dee.com\n
user41,boo,us1@bad1dee.com\n
user42,boo,us1@bad1dee.com\n
user43,boo,us1@bad1dee.com\n
user44,boo,us1@bad1dee.com\n
user45,boo,us1@bad1dee.com\n
user46,boo,us1@bad1dee.com\n
user47,boo,us1@bad1dee.com\n
user48,boo,us1@bad1dee.com\n
user49,boo,us1@bad1dee.com\n
user50,boo,us1@bad1dee.com\n
user51,boo,us1@bad1dee.com\n
user52,boo,us1@bad1dee.com\n
user53,boo,us1@bad1dee.com\n
user54,boo,us1@bad1dee.com\n
user55,boo,us1@bad1dee.com\n
user56,boo,us1@bad1dee.com\n
user57,boo,us1@bad1dee.com\n
user58,boo,us1@bad1dee.com\n
user59,boo,us1@bad1dee.com\n
user60,boo,us1@bad1dee.com\n
user61,boo,us1@bad1dee.com\n
user62,boo,us1@bad1dee.com\n
user63,boo,us1@bad1dee.com\n
user64,boo,us1@bad1dee.com\n
user65,boo,us1@bad1dee.com\n
user66,boo,us1@bad1dee.com\n
user67,boo,us1@bad1dee.com\n
user68,boo,us1@bad1dee.com\n
user69,boo,us1@bad1dee.com\n
user70,boo,us1@bad1dee.com\n
user71,boo,us1@bad1dee.com\n
user72,boo,us1@bad1dee.com\n
user73,boo,us1@bad1dee.com\n
user74,boo,us1@bad1dee.com\n
user75,boo,us1@bad1dee.com\n
user76,boo,us1@bad1dee.com\n
user77,boo,us1@bad1dee.com\n
user78,boo,us1@bad1dee.com\n
user79,boo,us1@bad1dee.com\n
user80,boo,us1@bad1dee.com\n
user81,boo,us1@bad1dee.com\n
user82,boo,us1@bad1dee.com\n
user83,boo,us1@bad1dee.com\n
user84,boo,us1@bad1dee.com\n
user85,boo,us1@bad1dee.com\n
user86,boo,us1@bad1dee.com\n
user87,boo,us1@bad1dee.com\n
user88,boo,us1@bad1dee.com\n
user89,boo,us1@bad1dee.com\n
user90,boo,us1@bad1dee.com\n
user91,boo,us1@bad1dee.com\n
user92,boo,us1@bad1dee.com\n
user93,boo,us1@bad1dee.com\n
user94,boo,us1@bad1dee.com\n
user95,boo,us1@bad1dee.com\n
user96,boo,us1@bad1dee.com\n
user97,boo,us1@bad1dee.com\n
user98,boo,us1@bad1dee.com\n
user99,boo,us1@bad1dee.com
./mass_add.sh.txt adduserfile1.txt

This is what i'm working on now ? i still do not know how to tie in C++ or bash script this code to work with command line, so i can control the out come of created users.

#!/bin/bash
Set objRootDSE = GetObject("LDAP://rootDSE")

Set objContainer = GetObject("LDAP://cn=Users," & _
    objRootDSE.Get("defaultNamingContext"))

For i = 1 To 1000
    Set objLeaf = objContainer.Create("User", "cn=UserNo" & i)
    objLeaf.Put "sAMAccountName", "UserNo" & i
    objLeaf.SetInfo
Next

WScript.Echo "1000 Users created."
example: [root]#./test 10
usid:UserNo1 gid:manager cn:Bob dn:bHarison
UserNo2
UserNo3
^^^^^^^
UserNo10      

This script makes a 1000 users. However i can not control the out come. For example: from the command line I would like it to stop at 100 users by typing in ./test 100. Using agrv [1]. so when I type a number after the file name it will create a list and print that record to the screen. I do not know bash that well as C++ and it is not helping because the char.. are diff...

54 dee 12.13.09 at 1:43 pm

this is what it suppose to print out: samle
objectclass: inetOrgPerson
cn: user51 boo
givenName: user51
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user52 boo

dn: user52 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user52 boo
givenName: user52
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user53 boo

dn: user53 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user53 boo
givenName: user53
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user54 boo

dn: user54 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user54 boo
givenName: user54
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user55 boo

dn: user55 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user55 boo
givenName: user55
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user56 boo

dn: user56 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user56 boo
givenName: user56
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user57 boo

dn: user57 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user57 boo
givenName: user57
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user58 boo

dn: user58 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user58 boo
givenName: user58
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user59 boo

dn: user59 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user59 boo
givenName: user59
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user60 boo

dn: user60 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user60 boo
givenName: user60
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user61 boo

dn: user61 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user61 boo
givenName: user61
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user62 boo

dn: user62 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user62 boo
givenName: user62
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user63 boo

dn: user63 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user63 boo
givenName: user63
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user64 boo

dn: user64 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user64 boo
givenName: user64
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user65 boo

dn: user65 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user65 boo
givenName: user65
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user66 boo

dn: user66 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user66 boo
givenName: user66
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user67 boo

dn: user67 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user67 boo
givenName: user67
sn: boo
uid: boo
mail: us1@bad1dee.com

55 dee 12.13.09 at 1:46 pm

here is a sample of print out :) thank u if you can help?

Creating user52 boo

dn: user52 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user52 boo
givenName: user52
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user53 boo

dn: user53 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user53 boo
givenName: user53
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user54 boo

dn: user54 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user54 boo
givenName: user54
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user55 boo

dn: user55 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user55 boo
givenName: user55
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user56 boo

dn: user56 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user56 boo
givenName: user56
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user57 boo

dn: user57 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user57 boo
givenName: user57
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user58 boo

dn: user58 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user58 boo
givenName: user58
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user59 boo

dn: user59 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user59 boo
givenName: user59
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user60 boo

dn: user60 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user60 boo
givenName: user60
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user61 boo

dn: user61 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user61 boo
givenName: user61
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user62 boo

dn: user62 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user62 boo
givenName: user62
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user63 boo

dn: user63 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user63 boo
givenName: user63
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user64 boo

dn: user64 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user64 boo
givenName: user64
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user65 boo

dn: user65 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user65 boo
givenName: user65
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user66 boo

dn: user66 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user66 boo
givenName: user66
sn: boo
uid: boo
mail: us1@bad1dee.com

Creating user67 boo

dn: user67 boo,o=dmacc,dc=bad1dee,dc=com
changetype: add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: user67 boo
givenName: user67
sn: boo
uid: boo
mail: us1@bad1dee.com

56 dee 12.13.09 at 2:44 pm

first file make: create_user_idif.sh.txt
#!/bin/bash
echo “dn: $2 $3,o=dmacc,dc=bad1dee,dc=com” > $1
echo “changetype: add” >> $1
echo “objectclass: top” >> $1
echo “objectclass: person” >> $1
echo “objectclass: organizationalPerson” >> $1
echo “objectclass: inetOrgPerson” >> $1
echo “cn: $2 $3″ >> $1
echo “givenName: $2″ >> $1
echo “sn: $3″ >> $1
echo “uid: ${2:0:1}$3″ >> $1
echo “mail: $4″ >> $1
___________________________________
mass_user.txt
Scott,brown,sbrown@bad4dee.com\n
karla,smith,ssmith@bad4dee.com\n
sam,goodie,sgoodie@bad4dee.com\n
marge,jones,mjones@bad4dee.com
____________________________________
mass_add.sh.txt
#!/bin/bash

u1=`cat $1 | tr -d ‘\n’`
#u2=`echo $u1 | tr \| ‘\n’`
u2=`echo $u1 | sed s/\|/\\n/`

echo “DBG”
echo -e $u1
echo “DBG”

echo -e $u1 | ( IFS=, ; while read fname lname mail;
do
echo -e “Creating $fname $lname\n\n”
il
cat ./user.ldif
echo -e “\n\n”
done )
_____________________________________
./mass_add.sh.txt mass_user.txt
This ex: will pull from a list but same out come i do not know how to write another script to pull the attributes i need from the command line like ./test 100 and that command will pull only a 100 users id’s from idif.txt out of 1000 generated.

This next samples of code will file in the attributes for you.
first file make: create_user_idif.sh.txt
#!/bin/bash
echo “dn: $2 $3,o=dmacc,dc=bad1dee,dc=com” > $1
echo “changetype: add” >> $1
echo “objectclass: top” >> $1
echo “objectclass: person” >> $1
echo “objectclass: organizationalPerson” >> $1
echo “objectclass: inetOrgPerson” >> $1
echo “cn: $2 $3″ >> $1
echo “givenName: $2″ >> $1
echo “sn: $3″ >> $1
echo “uid: ${2:0:1}$3″ >> $1
echo “mail: $4″ >> $1
____________________________________________________________
Then:

mass_add.sh.txt
#!/bin/bash

u1=`cat $1 | tr -d ‘\n’`
#u2=`echo $u1 | tr \| ‘\n’`
u2=`echo $u1 | sed s/\|/\\n/`

echo “DBG”
echo -e $u1
echo “DBG”

echo -e $u1 | ( IFS=, ; while read fname lname mail;
do
echo -e “Creating $fname $lname\n\n”
./create_user_ldif.sh.txt ./user.ldif $fname $lname $mail
cat ./user.ldif
echo -e “\n\n”
done )
_____________________________________________________________________
last:
adduserfile1.txt
user0,boo,us1@bad1dee.com\n
user1,boo,us1@bad1dee.com\n
user2,boo,us1@bad1dee.com\n
user3,boo,us1@bad1dee.com\n
user4,boo,us1@bad1dee.com\n
user5,boo,us1@bad1dee.com\n
user6,boo,us1@bad1dee.com\n
user7,boo,us1@bad1dee.com\n
user8,boo,us1@bad1dee.com\n
user9,boo,us1@bad1dee.com\n
user10,boo,us1@bad1dee.com\n
user11,boo,us1@bad1dee.com\n
user12,boo,us1@bad1dee.com\n
user13,boo,us1@bad1dee.com\n
user14,boo,us1@bad1dee.com\n
user15,boo,us1@bad1dee.com\n
user16,boo,us1@bad1dee.com\n
user17,boo,us1@bad1dee.com\n
user18,boo,us1@bad1dee.com\n
user19,boo,us1@bad1dee.com\n
user20,boo,us1@bad1dee.com\n
user21,boo,us1@bad1dee.com\n
user22,boo,us1@bad1dee.com\n
user23,boo,us1@bad1dee.com\n
user24,boo,us1@bad1dee.com\n
user25,boo,us1@bad1dee.com\n
user26,boo,us1@bad1dee.com\n
user27,boo,us1@bad1dee.com\n
user28,boo,us1@bad1dee.com\n
user29,boo,us1@bad1dee.com\n
user30,boo,us1@bad1dee.com\n
user31,boo,us1@bad1dee.com\n
user32,boo,us1@bad1dee.com\n
user33,boo,us1@bad1dee.com\n
user34,boo,us1@bad1dee.com\n
user35,boo,us1@bad1dee.com\n
user36,boo,us1@bad1dee.com\n
user37,boo,us1@bad1dee.com\n
user38,boo,us1@bad1dee.com\n
user39,boo,us1@bad1dee.com\n
user40,boo,us1@bad1dee.com\n
user41,boo,us1@bad1dee.com\n
user42,boo,us1@bad1dee.com\n
user43,boo,us1@bad1dee.com\n
user44,boo,us1@bad1dee.com\n
user45,boo,us1@bad1dee.com\n
user46,boo,us1@bad1dee.com\n
user47,boo,us1@bad1dee.com\n
user48,boo,us1@bad1dee.com\n
user49,boo,us1@bad1dee.com\n
user50,boo,us1@bad1dee.com\n
user51,boo,us1@bad1dee.com\n
user52,boo,us1@bad1dee.com\n
user53,boo,us1@bad1dee.com\n
user54,boo,us1@bad1dee.com\n
user55,boo,us1@bad1dee.com\n
user56,boo,us1@bad1dee.com\n
user57,boo,us1@bad1dee.com\n
user58,boo,us1@bad1dee.com\n
user59,boo,us1@bad1dee.com\n
user60,boo,us1@bad1dee.com\n
user61,boo,us1@bad1dee.com\n
user62,boo,us1@bad1dee.com\n
user63,boo,us1@bad1dee.com\n
user64,boo,us1@bad1dee.com\n
user65,boo,us1@bad1dee.com\n
user66,boo,us1@bad1dee.com\n
user67,boo,us1@bad1dee.com\n
user68,boo,us1@bad1dee.com\n
user69,boo,us1@bad1dee.com\n
user70,boo,us1@bad1dee.com\n
user71,boo,us1@bad1dee.com\n
user72,boo,us1@bad1dee.com\n
user73,boo,us1@bad1dee.com\n
user74,boo,us1@bad1dee.com\n
user75,boo,us1@bad1dee.com\n
user76,boo,us1@bad1dee.com\n
user77,boo,us1@bad1dee.com\n
user78,boo,us1@bad1dee.com\n
user79,boo,us1@bad1dee.com\n
user80,boo,us1@bad1dee.com\n
user81,boo,us1@bad1dee.com\n
user82,boo,us1@bad1dee.com\n
user83,boo,us1@bad1dee.com\n
user84,boo,us1@bad1dee.com\n
user85,boo,us1@bad1dee.com\n
user86,boo,us1@bad1dee.com\n
user87,boo,us1@bad1dee.com\n
user88,boo,us1@bad1dee.com\n
user89,boo,us1@bad1dee.com\n
user90,boo,us1@bad1dee.com\n
user91,boo,us1@bad1dee.com\n
user92,boo,us1@bad1dee.com\n
user93,boo,us1@bad1dee.com\n
user94,boo,us1@bad1dee.com\n
user95,boo,us1@bad1dee.com\n
user96,boo,us1@bad1dee.com\n
user97,boo,us1@bad1dee.com\n
user98,boo,us1@bad1dee.com\n
user99,boo,us1@bad1dee.com
./mass_add.sh.txt adduserfile1.txt
This is what i’m working on now ? i still do not know how to tie in C++ or bash script this code to work with command line, so i can control the out come of created users.
#!/bin/bash
Set objRootDSE = GetObject(“LDAP://rootDSE”)

Set objContainer = GetObject(“LDAP://cn=Users,” & _
objRootDSE.Get(“defaultNamingContext”))

For i = 1 To 1000
Set objLeaf = objContainer.Create(“User”, “cn=UserNo” & i)
objLeaf.Put “sAMAccountName”, “UserNo” & i
objLeaf.SetInfo
Next

WScript.Echo “1000 Users created.”
example: [root]#./test 10
usid:UserNo1 gid:manager cn:Bob dn:bHarison
UserNo2
UserNo3
^^^^^^^
UserNo10
This script makes a 1000 users. However i can not control the out come. For example: from the command line I would like it to stop at 100 users by typing in ./test 100. Using agrv [1]. so when I type a number after the file name it will create a list and print that record to the screen. I do not know bash that well as C++ and it is not helping because the char.. are diff…

57 Philippe Petrinko 12.13.09 at 2:51 pm

To Dee:
1) Man, with a 3-users-sample instead of hundreds, we would have figured out, don’t you think so?

2) Well that’s a start. May be Vivek would like to wipe this post out, and create a new topic: “Of mice, LDAP and loops” ;-) ???

58 dee 12.13.09 at 6:14 pm

first file make: create_user_idif.sh.txt
#!/bin/bash
echo “dn: $2 $3,o=dmacc,dc=bad1dee,dc=com” > $1
echo “changetype: add” >> $1
echo “objectclass: top” >> $1
echo “objectclass: person” >> $1
echo “objectclass: organizationalPerson” >> $1
echo “objectclass: inetOrgPerson” >> $1
echo “cn: $2 $3″ >> $1
echo “givenName: $2″ >> $1
echo “sn: $3″ >> $1
echo “uid: ${2:0:1}$3″ >> $1
echo “mail: $4″ >> $1
___________________________________
mass_user.txt
Scott,brown,sbrown@bad4dee.com\n
karla,smith,ssmith@bad4dee.com\n
sam,goodie,sgoodie@bad4dee.com\n
marge,jones,mjones@bad4dee.com
____________________________________
mass_add.sh.txt
#!/bin/bash

u1=`cat $1 | tr -d ‘\n’`
#u2=`echo $u1 | tr \| ‘\n’`
u2=`echo $u1 | sed s/\|/\\n/`

echo “DBG”
echo -e $u1
echo “DBG”

echo -e $u1 | ( IFS=, ; while read fname lname mail;
do
echo -e “Creating $fname $lname\n\n”
il
cat ./user.ldif
echo -e “\n\n”
done )
_____________________________________
./mass_add.sh.txt mass_user.txt
This ex: will pull from a list but same out come i do not know how to write another script to pull the attributes i need from the command line like ./test 100 and that command will pull only a 100 users id’s from idif.txt out of 1000 generated.
ok the code up top is short, i know there is a for loop or bash script that can run with ./test 3. i just need a script that can create data or users and print first those users to screen with first argument on command line./ test 3 or 4 etc and that number will print those names any code would work. I mean it would give me a running chance ; )
example:
[root]# ./getdata 3
Scott,brown,sbrown@bad4dee.com\n
karla,smith,ssmith@bad4dee.com\n
sam,goodie,sgoodie@bad4dee.com\n
oh, i’m using fedora 10
is this possible.

59 Dominic 01.14.10 at 10:04 am

There is an interesting difference between the exit value for two different for looping structures (hope this comes out right):
for (( c=1; c<=2; c++ )) do echo -n "inside (( )) loop c is $c, "; done; echo "done (( )) loop c is $c"
for c in {1..2}; do echo -n "inside { } loop c is $c, "; done; echo "done { } loop c is $c"

You see that the first structure does a final increment of c, the second does not. The first is more useful IMO because if you have a conditional break in the for loop, then you can subsequently test the value of $c to see if the for loop was broken or not; with the second structure you can’t know whether the loop was broken on the last iteration or continued to completion.

60 Dominic 01.14.10 at 10:09 am

sorry, my previous post would have been clearer if I had shown the output of my code snippet, which is:
inside (( )) loop c is 1, inside (( )) loop c is 2, done (( )) loop c is 3
inside { } loop c is 1, inside { } loop c is 2, done { } loop c is 2

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All