LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Simple While Loop (https://www.linuxquestions.org/questions/linux-newbie-8/simple-while-loop-847586/)

tdlynch88 11-30-2010 10:41 PM

Simple While Loop
 
Ok I am wanting to figure out a simple script that will implement a while loop to make a simple text file with multiple entries eg.

userlist (file name)
user,1,user1@domain.com
user,2,user2@domain.com
user,3,user3@domain.com
etc.

now I know this is childs play but people got to start somewhere. Thanks in advance!

grail 11-30-2010 10:54 PM

Assuming you are scripting in bash, you can have a look here:

http://tldp.org/LDP/abs/html/

theNbomr 12-01-2010 09:36 AM

You seem to putting the cart before the horse. Before you conclude that you need a while loop, perhaps you should describe what is the nature of the input data, where it comes from, and how you will parse it, if necessary. You are describing a programming conditional statement, without mentioning any of the conditions. A more strictly bounded loop may be more appropriate, or perhaps no looping at all.
This does sound a bit like homework. Please describe your objective and support it with some sample data.

--- rod.

tdlynch88 12-01-2010 11:52 PM

This is what I have come up with


#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo "User,$x,user$x@abcd.com"
x=$(( $x + 1 ))
done


when appended to a file it would create

User,1,user1@abcd.com
User,2,user2@abcd.com
User,3,user3@abcd.com
User,4,user4@abcd.com
User,5,user5@abcd.com

I am using this file as variables to add ldap entries. Not exactly homework but it is class work, and yes im a noob lol. Thanks for the help LQ seems like a great place to be!

catkin 12-02-2010 12:01 AM

It works and congratulations on that :)

It could be more elegantly done using an arithmetic for loop (not tested):
Code:

#!/bin/bash
for (( x=1; x<6; x++ ))
do
    echo "User,$x,user$x@abcd.com"
done

BTW it's easier to read code you put it in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).


All times are GMT -5. The time now is 02:47 AM.