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? |
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 M. |
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) |
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}') ) |
Quote:
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... |
Thanks.
|
Quote:
|
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" |
this would be far simpler and more secure if key-based ssh was used; in 2016, is this not an option (default) ?
|
schneidz ... you just talking crazzzyyy now :D
|
All times are GMT -5. The time now is 11:59 PM. |