LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-20-2015, 02:39 PM   #1
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Rep: Reputation: 15
script while loop


Hello,
I'm working on a shell script that needs to read the a file (file1) a batch (read 2000 lines at a time) and then write those lines to seperate file which I'm using to run ldapmodify command. I need to check some other file size and once this file less than 200kb, i would need to run second batch and so on until file1 is empty.
Quote:
#!/bin/bash
set -x
filesize=200
server=10.11.xxx.xx
filename=fileimport.txt
taofile1=687686.txt
taofile=/var/opt/$taofile1

stty -echo
echo -n "Enter password: "
read passwd
stty echo
context="cn=user1,ou=org1,ou=org2,o=org"
if [[ -s "$filename" ]];then
for lines in $filename
do
head -99997 fileimport.txt >> ldapreadd.ldif && sed -i '1,+99996d' fileimport.txt
if [ -s ldapreadd.ldif ];then
ldapmodify -h $server -p 389 -D $context -w $passwd -v -x -f ldapreadd.ldif &> /dev/null
echo "LDIF script is running. Please wait..."
sleep 60
if [ -f "$taofile" ];then
taofilesize=`stat -c %s ${taofile}`
# if [ "$taofilesize" -ge 200 ];then
while [ `stat -c %s ${taofile}` -ge 200 ]
do
echo " Driver is processing LDIF file. Please wait..."
sleep 60
done
fi
rm -rf ldapreadd.ldif
fi
done
else
echo "$filename file is empty. Exiting script..."
exit
fi
I have 2 issues here. first one script is not reading exact lines as specified. Some times its reading more lines some times less lines. Second issues once it run the first batch, script is exiting. Please advise.
 
Old 01-20-2015, 05:55 PM   #2
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Maybe it's just me, but I find it difficult to understand what you are trying to do.
It would be easier to help if you used code indentation (like below), that would make it less cumbersome to look at it.
Code:
#!/bin/bash
set -x
filesize=200
server=10.11.xxx.xx
filename=fileimport.txt
taofile1=687686.txt
taofile=/var/opt/$taofile1

stty -echo
echo -n "Enter password: "
read passwd
stty echo
context="cn=user1,ou=org1,ou=org2,o=org"
if [[ -s "$filename" ]];then
	for lines in $filename
	do
		head -99997 fileimport.txt >> ldapreadd.ldif && sed -i '1,+99996d' fileimport.txt
		if [ -s ldapreadd.ldif ];then
			ldapmodify -h $server -p 389 -D $context -w $passwd -v -x -f ldapreadd.ldif &> /dev/null
			echo "LDIF script is running. Please wait..."
			sleep 60
			if [ -f "$taofile" ];then
				taofilesize=`stat -c %s ${taofile}`
				while [ `stat -c %s ${taofile}` -ge 200 ]
				do
					echo " Driver is processing LDIF file. Please wait..."
					sleep 60
				done
			fi
			rm -rf ldapreadd.ldif
		fi
	done
else
	echo "$filename file is empty. Exiting script..."
	exit
fi
On top of that, try to clean it up a bit. E.g. at the beginning you are declaring the variable filesize, but it's never used. Then there is an if clause which is commented out, which adds to the clutter.
Sorry if I sound discouraging here, that's not my intention. What I'm trying to say is that you'll get more help if you make it easy for people to help.

That said, here is my initial throught (which may be totally off, because I don't understand exactly what your doing): If I get it right the < 200kb condition is only checked every 60 seconds. If e.g. the number of kilobytes processed per minute was more than one, it would only be a coincidence if you were hitting the 200 exactly. Does it help if you reduce the sleep time?

I don't understand what you mean with "the first batch" and where you think you are limiting it to 2000 lines...
 
Old 01-21-2015, 04:10 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
This
Code:
	for lines in $filename
doesn't do what you think.
Try amending the 'set -x' to 'set -xv'

Also, when debugging, try to write the shortest possible code that demonstrates each problem, one at a time.
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
I need help with a loop game in script taz210 Linux - Newbie 6 11-20-2014 02:52 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 03:14 AM
shell script , while loop ykc Programming 5 03-30-2009 08:50 AM
How to loop a script? rollo Linux - General 3 12-21-2006 06:39 AM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration