LinuxQuestions.org
Review your favorite Linux distribution.
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 03-07-2016, 11:49 PM   #1
addy54
LQ Newbie
 
Registered: Mar 2016
Posts: 13

Rep: Reputation: Disabled
Trying to delete older files from ftp server using shell script.


Hello All,

I am trying to delete older files from ftp server. But before that I want to check how many files I got through ${#var[@]} but it always gives me output count 1 which is not expected. Please see below snippet of my code.


#!/bin/bash
#set -x
#get a list of files and dates from ftp and remove files older than n days

ftpserver="abc.xyz.com"
ftpuser="abcdef"
ftppass=xxxxxx
putdir="/opt/crtitcal/Arpit/TgtFiles/"
ndays=15


#work out cutoff dates.

MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`
TT=`date --date="$ndays days ago" +%s`

echo removing files older then $MM $DD from $ftpsite directory $putdir

#get dirlist from ftp

listing=`ftp -i $ftpserver << EOF
user $ftpuser $ftppass
binary
cd $putdir
ls -l
quit
EOF
`
#remove first 4 and last lines from output and stored it in a variable.
finallist="`printf "( $listing\n )" | tail -n +5 |head -n -1`"

echo "${#finallist[@]}"

echo "${#finallist[*]}"

###################################################
Both echo "${#finallist[@]}" and echo "${#finallist[*]}" gives me count 1 where I can see more that 25 files in the variable.

Can somebody suggest what can be done here?
 
Old 03-08-2016, 02:05 AM   #2
translator1111
Member
 
Registered: Jun 2010
Location: Slovakia
Distribution: Debian 8, Ubuntu 10.04 and 12.04; SLAX 6.0; ConnochaetOS 0.9.; LFS; Natty chip: VT1708S
Posts: 108
Blog Entries: 2

Rep: Reputation: 7
Dear arpit,
I myself just starting with scripting (chapter 8 of BasicShellScripting) so maybe my hint will not be useful.
However I will write to you.
Have you tried to debug your scripting inputting?
Code:
set -x
echo "${#finallist[@]}"

echo "${#finallist[*]}"
set +x
Regards,
M.
 
Old 03-08-2016, 02:05 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Please use [code][/code] tags for your code / data.

finallist is not an array. To assign an array in bash you need to do:
Code:
finallist=(a b c)
Also, I would suggest using $() instead of `` as it nests better and is much clearer
 
1 members found this post helpful.
Old 03-08-2016, 03:29 AM   #4
addy54
LQ Newbie
 
Registered: Mar 2016
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks Grail.

I have used below now and it worked...Thanks a lot for your inputs.

Code:
 finallist=( $(printf "( $listing\n )" | tail -n +5 |head -n -1 | awk '{print $6}') )
 
Old 03-08-2016, 04:44 AM   #5
ardvark71
LQ Veteran
 
Registered: Feb 2015
Location: USA
Distribution: Lubuntu 14.04, 22.04, Windows 8.1 and 10
Posts: 6,282
Blog Entries: 4

Rep: Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842
Quote:
I have used below now and it worked...Thanks a lot for your inputs.
Hi...

Welcome to the forum

If your issue is resolved, please mark this thread as "SOLVED" by clicking on "Thread Tools" directly above your initial post. Thanks!

Also, just as a suggestion, you might want to change your username using the instructions at the top of the page here, lest your email inbox ends up with a boatload of SPAM in the very near future.

Regards...
 
Old 03-08-2016, 05:15 AM   #6
addy54
LQ Newbie
 
Registered: Mar 2016
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks.
 
Old 03-08-2016, 05:17 AM   #7
ardvark71
LQ Veteran
 
Registered: Feb 2015
Location: USA
Distribution: Lubuntu 14.04, 22.04, Windows 8.1 and 10
Posts: 6,282
Blog Entries: 4

Rep: Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842
Quote:
Thanks.
You're welcome
 
Old 03-08-2016, 08:04 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Now that you have added awk into the mix, you can just use it instead of tail/head/awk, something like:
Code:
awk 'x{print x}NR > 4{x=$6}' <<<"$listing"
 
Old 03-08-2016, 08:25 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this would be far simpler and more secure if key-based ssh was used; in 2016, is this not an option (default) ?
 
Old 03-08-2016, 08:51 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
schneidz ... you just talking crazzzyyy now
 
  


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
Delete older files using Shell script Mitiraj Linux - Newbie 1 09-10-2014 01:35 AM
Delete backups older than 7 days through shell script Rk_Raj Linux - Newbie 4 04-09-2013 02:47 AM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
shell script - auto ftp files that older than 60 days bakeng Linux - Newbie 2 12-25-2007 10:35 PM
FTP files to remote server with a shell script Pezzoni Linux - Software 2 06-27-2007 07:01 AM

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

All times are GMT -5. The time now is 07:21 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
Open Source Consulting | Domain Registration