LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Trying to delete older files from ftp server using shell script. (https://www.linuxquestions.org/questions/programming-9/trying-to-delete-older-files-from-ftp-server-using-shell-script-4175574273/)

addy54 03-07-2016 11:49 PM

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?

translator1111 03-08-2016 02:05 AM

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.

grail 03-08-2016 02:05 AM

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

addy54 03-08-2016 03:29 AM

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}') )

ardvark71 03-08-2016 04:44 AM

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...

addy54 03-08-2016 05:15 AM

Thanks.

ardvark71 03-08-2016 05:17 AM

Quote:

Thanks.
You're welcome :)

grail 03-08-2016 08:04 AM

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"

schneidz 03-08-2016 08:25 AM

this would be far simpler and more secure if key-based ssh was used; in 2016, is this not an option (default) ?

grail 03-08-2016 08:51 AM

schneidz ... you just talking crazzzyyy now :D


All times are GMT -5. The time now is 11:59 PM.