LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2012, 01:41 PM   #1
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Rep: Reputation: Disabled
Loop in Shell Programming


I have the following script that reads a file "sort_2" that contains one column of IPs. For every loop, it tries to ftp an IP to get the a file named "vsys.tar".

I think I'm doing some error in the looping as I receive this error "syntax error: unexpected end of file".


Code:
#!/bin/bash

file=sort_2
while read -a num; do
	
	HOST=${num}
	USER=user_name
	PASS=user_pass

	ftp -inv $HOST << EOF

	user $USER $PASS
	cd test
	get vsys.tar
	bye
	EOF
done < "$file"
exit 0
 
Old 07-17-2012, 01:44 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the line containing the second EOF should not contain anything else (spaces or tabs before or after that 3 letters) just the keyword EOF
 
Old 07-17-2012, 04:24 PM   #3
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Original Poster
Rep: Reputation: Disabled
Thanks pan64 for your quick and accurate response. Further, I'm looking to save the result of every ftp session to identical file. for example, the first IP-1 to a file named IP1.ftp and so on; any idea regarding this?

Last edited by ali2011; 07-17-2012 at 04:27 PM.
 
Old 07-17-2012, 08:11 PM   #4
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
increment a variable

Increment a variable as the while loop progresses:

Code:
#!/bin/bash

FILE="sort_2"

USER="user_name"
PASS="user_pass"

a=0

if [[ -s "$FILE" ]]
then
	while read -a HOST
	do
		ftp -inv $HOST << EOF
			user $USER $PASS
			cd test
			get vsys.tar
			bye
EOF
		a=$(expr $a + 1)
		mv -vf vsys.tar ${HOST}${a}.ftp
	done < "$FILE"
else
	echo "Sorry.  $FILE does not exist"
	echo "Exiting ..."
	exit 1
fi
 
Old 07-18-2012, 02:07 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I would use this:

.netrc
 
Old 07-18-2012, 06:07 AM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
To indent the EOF and make it more readable, you can use:
Code:
command <<-EOF
This will strip off any leading TAB characters (not only for the EOF, but for all lines of the here document).
 
Old 07-18-2012, 10:00 AM   #7
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Original Poster
Rep: Reputation: Disabled
Thank you all for your perfect comments. However, tonyfreeman, I think I didn't clarify my question enough so you got it on the other side. The file I'm interested to save per IP is not the transferred file itself "vsys.tar", but the ftp report while getting this file (i.e., the speed and other info while the ftp transmission is taken place).

e.g., the following info that I need to be stored in a file named "133.101.33.4.ftp"

Code:
Connected to 133.101.33.4 (133.101.33.4).
220 (vsFTPd 2.0.5)
331 Please specify the password.
230 Login successful.
250 Directory successfully changed.
local: TEST.mp3 remote: TEST.mp3
227 Entering Passive Mode (133,101,33,4,45,211).
150 Opening BINARY mode data connection for TEST.mp3 (5492819 bytes).
WARNING! 22955 bare linefeeds received in ASCII mode
File may not have transferred correctly.
226 File send OK.
5492819 bytes received in 1.21 secs (4555.78 Kbytes/sec)
221 Goodbye.
 
Old 07-18-2012, 10:09 AM   #8
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Original Poster
Rep: Reputation: Disabled
While trying the code above, I encounter another problem, which is that the ftp for some reason doesn't succeed for some IPs in the loop. How can I add another loop for only failed IPs whose stored reports (i.e., IP.ftp) don't have the speed info (i.e., in particular the WORD "Kbytes/sec")?

I look to make the failed IPs to be repeated 3 times after fishing the sotr_2, and if failure again, just go to the new 3 attempts of next failed IP.

Last edited by ali2011; 07-18-2012 at 10:10 AM.
 
Old 07-18-2012, 10:17 AM   #9
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
You can write the output of the ftp command to a file:
Code:
ftp -inv $HOST <<-EOF >$HOST.ftp
 
  


Reply



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
[SOLVED] shell programming help: while loop jestinjoy Linux - Newbie 4 06-28-2010 03:11 AM
C programming Loop question? chibi2666 Programming 8 06-16-2009 09:15 PM
Bash programming while loop Histamine Programming 3 10-29-2007 06:35 PM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:14 PM.

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
Open Source Consulting | Domain Registration