LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-20-2011, 09:21 AM   #1
++nick++
Member
 
Registered: Dec 2008
Location: Bellevue,WA
Distribution: RHEL 5 , Fedora ,Sabayon,Solaris,Vmware,AWS
Posts: 107

Rep: Reputation: 18
Unhappy variable incrementation in for loop


All,

I have a file which contains a list of users,

..
peter
paul
gary
...

I am trying to prepend UID , starting from 500 in the below format so I can add them to the server kickstart file that I am building,

..
500eter
501aul
502:gary
..

My script is below,

#!/bin/bash

for x in `cat $file`
do
for (( c=500;c<520;c++ ))
do
echo $c:$x
done
done

520 is a result of wc -l $file,

But the above print out 500..520 for all users in the file,

Can someone please point to the mistake I am making here,

Thanks,
 
Old 10-20-2011, 09:39 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
The paste command can avoids the loop:
Code:
paste -d: <(seq 500 1019) file
I used 1019 since you stated the file contains 520 different names, so that the first one is numbered 500 and the last one 1019. Is this what you're looking for?

Regarding your script, you have two nested loops, hence for every x (user) it prints out 500:x, 501:x, ... 519:x, 520:x. Rather you actually need a single loop and a counter to increment at each passage:
Code:
c=500
for x in $(cat $file)
do
  echo $c:$x
  ((c++))
done
 
Old 10-20-2011, 11:16 AM   #3
++nick++
Member
 
Registered: Dec 2008
Location: Bellevue,WA
Distribution: RHEL 5 , Fedora ,Sabayon,Solaris,Vmware,AWS
Posts: 107

Original Poster
Rep: Reputation: 18
Amazing colucix ,it is working like a charm now , I was unable to get the counter to work properly earlier,

Thanks Again , Good Night
 
Old 10-20-2011, 02:03 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Please use [code][/code] tags around your code, to preserve formatting and to improve readability. (You have over a hundred posts, so you should be aware of this by now )

You can turn off smilies when posting too.


Now for some scripting tips:

1) Don't use a for loop when reading from files or commands. Any input with a space in it would break it. Use a while+read loop instead.

http://mywiki.wooledge.org/DontReadLinesWithFor

2) Useless use of cat. Inside a command substitution $(<filename) does just the same job (well, except that any final newline will be removed). And if you use the while loop I mentioned in #1, you can feed it directly (example below).

3) $(..) is highly recommended over `..`. Please don't use backticks, they're hard to read, easy to mistake, and completely unnecessary.

4) Always quote variable expansions, as word-splitting is done after expansion. (This is also why the command substitution in #1 is a problem).

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


With that said, here's a better example of colucix's code:
Code:
c=500

while read x ; do
	echo "$c:$x"
	((c++))
done <"$file"
 
  


Reply

Tags
incremental, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to define a variable In a while loop? fatsheep Programming 16 07-30-2014 01:12 PM
[SOLVED] variable is gone at the end of while loop ted_chou12 Programming 9 03-28-2011 03:57 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 07:45 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 04:43 AM


All times are GMT -5. The time now is 09:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration