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-10-2016, 01:09 AM   #1
addy54
LQ Newbie
 
Registered: Mar 2016
Posts: 13

Rep: Reputation: Disabled
Getting incorrect ouput of array


Hello Folks,

How are you all?

I have written below script to get the list of files from ftp.

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

ftpserver="pns95a-4402.corpny.csfb.com"
ftpuser="adoshi9"
ftppass=cs@Cogni2016
putdir="/app/opera/operaqa7/TgtFiles/PAC/test"
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
`
filelist=( $(printf "( $listing\n )" | tail -n +5 |head -n -1 | awk '{print $9'} ) )
echo "${filelist[@]}"
echo "${#filelist[@]}"
actuallist=( $(printf "( $listing\n )" | tail -n +5 |head -n -1 ) )


for (( FNO=0; FNO < ${#filelist[@]}; FNO++ ));do

fdate="${actuallist[`expr $FNO + 5`]} ${actuallist[`expr $FNO + 6`]} ${actuallist[`expr $FNO + 8`]}"
echo "$fdate";
done

Basically I am trying to get the list of files from ftp and compare it with current date timestamp. If it is older than 15 days it should get deleted with this script.

The issue is that in above code ouput of
echo "${#filelist[@]}" is 6 and output of
echo "${actuallist[@]}" is as below.


-rw-rw-rw- 1 addy test 2540 Sep 15 05:47 abc-20150630-20150630.gz
-rw-rw-rw- 1 addy test 126 Sep 15 05:47 abc-20150630-20150630.gz.tmp.ftp.dtl
-rw-rw-rw- 1 addy test 27676 Sep 11 2015 PAC-20150630-20150630.gz
-rw-rw-rw- 1 addy test 126 Sep 14 16:32 PAC-20150630-20150630.gz.tmp.ftp.dtl
-rw-rw-rw- 1 addy test 8890 Sep 15 04:22 AMS-20150630-20150630.gz
-rw-rw-rw- 1 addy test 126 Sep 15 04:22 AMS-20150630-20150630.gz.tmp.ftp.dtl


When script executes for loop it space as field seperater and consider the string as pne argument.

So when value of FN=0 then ouput of echo "$fdate"(which seems correct) is
Sep 15 abc-20150630-20150630.gz


But when value of FN increases in next iteration FN=1 and ouput of echo "$fdate" is
15 05:47 -rw-rw-rw- which is not supposed to be. Here I should get


Sep 15 abc-20150630-20150630.gz.tmp.ftp.dtl

what can be done here to achive above output?
 
Old 03-10-2016, 01:56 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So you are going to need to be a little clearer (at least I am getting a bit confused).

Firstly why are we comparing the output of counting items in the array and then what the items in the array are?
Code:
echo "${#filelist[@]}" <- - this returns a count

echo "${actuallist[@]}" <- - this return the items in the array
I have tried to perform some of your code locally and it does not perform as expected / shown:

1. printf on my computer does not need the brackets and it is causing the first round bracket to be the data stored in first item of the array

2. Not worrying about above point, the printf / tail / head array produced gives me every part of the listing as an array element, ie. -rw-rw-rw- is in its own element

So the above leads me to wonder if either IFS has been altered or ftp returns data differently.

Also, arrays do not require the expr command:
Code:
${actuallist[`expr $FNO + 5`]}

${actuallist[FNO + 5]}
So I know I didn't answer your problem, but as you can see some of the information is a little confusing
 
Old 03-10-2016, 02:49 AM   #3
addy54
LQ Newbie
 
Registered: Mar 2016
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks for your response Grail.

Now answer to your question.

Q:Firstly why are we comparing the output of counting items in the array and then what the items in the array are?

Check the output of below code. May be you get an idea.

Code:
filelist=( $(printf "( $listing\n )" | tail -n +5 |head -n -1 | awk '{print $9'} ) )
echo "${filelist[@]}"
echo "${#filelist[@]}"
echo
echo
actuallist=( $(printf "( $listing\n )" | tail -n +5 |head -n -1 ) )
echo "${actuallist[@]}"
echo "${#actuallist[@]}"


abc-20150630-20150630.gz abc-20150630-20150630.gz.tmp.ftp.dtl PAC-20150630-20150630.gz PAC-20150630-20150630.gz.tmp.ftp.dtl AMS-20150630-20150630.gz AMS-20150630-20150630.gz.tmp.ftp.dtl
6


-rw-rw-rw- 1 operaqa7 opera 2540 Sep 15 05:47 abc-20150630-20150630.gz -rw-rw-rw- 1 operaqa7 opera 126 Sep 15 05:47 abc-20150630-20150630.gz.tmp.ftp.dtl -rw-rw-rw- 1 operaqa7 opera 27676 Sep 11 2015 PAC-20150630-20150630.gz -rw-rw-rw- 1 operaqa7 opera 126 Sep 14 16:32 PAC-20150630-20150630.gz.tmp.ftp.dtl -rw-rw-rw- 1 operaqa7 opera 8890 Sep 15 04:22 AMS-20150630-20150630.gz -rw-rw-rw- 1 operaqa7 opera 126 Sep 15 04:22 AMS-20150630-20150630.gz.tmp.ftp.dtl
54
 
Old 03-10-2016, 03:18 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Ok, that is what I expected (btw. still don't need the brackets for printf ... you don't see any of it in your output due to tail and head)

Now you are faced with the general problem of parsing ls output, which is generally dangerous as date formatting and white space in file names can cause all sorts of issues.

To help alleviate some of the issue with the actuallist data, you could try adjusting the IFS value to be just a newline. This should at least then give you the same 6 lines.

Another alternative would be to use stat instead of ls and then only print out the information you want:
Code:
$ ls -l f4
-rw-r--r-- 1 grail users 148 24.01.2016 15:50 f4
$ stat -c "%Y|%n" f4
1453621844|f4
Here I have gone with the time since epoch, but if you look up the alternatives in man you can find which one suits you best.
Also, I have used a pipe as the delimiter to then set IFS and have the array lined up accordingly.

Hope some of that helps.
 
Old 03-10-2016, 04:05 AM   #5
bonnydeal
Member
 
Registered: Feb 2006
Posts: 47

Rep: Reputation: 29
I hope those are not your real login credentials. If they are please change them right now before you do anything else.

Quote:
Originally Posted by addy54 View Post
Hello Folks,

How are you all?

I have written below script to get the list of files from ftp.

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

ftpserver="pns95a-4402.corpny.csfb.com"
ftpuser="adoshi9"
ftppass=cs@Cogni2016
putdir="/app/opera/operaqa7/TgtFiles/PAC/test"
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
`
filelist=( $(printf "( $listing\n )" | tail -n +5 |head -n -1 | awk '{print $9'} ) )
echo "${filelist[@]}"
echo "${#filelist[@]}"
actuallist=( $(printf "( $listing\n )" | tail -n +5 |head -n -1 ) )


for (( FNO=0; FNO < ${#filelist[@]}; FNO++ ));do

fdate="${actuallist[`expr $FNO + 5`]} ${actuallist[`expr $FNO + 6`]} ${actuallist[`expr $FNO + 8`]}"
echo "$fdate";
done

Basically I am trying to get the list of files from ftp and compare it with current date timestamp. If it is older than 15 days it should get deleted with this script.

The issue is that in above code ouput of
echo "${#filelist[@]}" is 6 and output of
echo "${actuallist[@]}" is as below.


-rw-rw-rw- 1 addy test 2540 Sep 15 05:47 abc-20150630-20150630.gz
-rw-rw-rw- 1 addy test 126 Sep 15 05:47 abc-20150630-20150630.gz.tmp.ftp.dtl
-rw-rw-rw- 1 addy test 27676 Sep 11 2015 PAC-20150630-20150630.gz
-rw-rw-rw- 1 addy test 126 Sep 14 16:32 PAC-20150630-20150630.gz.tmp.ftp.dtl
-rw-rw-rw- 1 addy test 8890 Sep 15 04:22 AMS-20150630-20150630.gz
-rw-rw-rw- 1 addy test 126 Sep 15 04:22 AMS-20150630-20150630.gz.tmp.ftp.dtl


When script executes for loop it space as field seperater and consider the string as pne argument.

So when value of FN=0 then ouput of echo "$fdate"(which seems correct) is
Sep 15 abc-20150630-20150630.gz


But when value of FN increases in next iteration FN=1 and ouput of echo "$fdate" is
15 05:47 -rw-rw-rw- which is not supposed to be. Here I should get


Sep 15 abc-20150630-20150630.gz.tmp.ftp.dtl

what can be done here to achive above output?
 
  


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
boot-ouput slashmais Linux - General 2 12-05-2014 12:48 AM
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
how to ouput ls result Cigar Linux - General 2 03-06-2007 09:41 AM
Incorrect Mouse, Incorrect Keymap, and Trapped in X Kenji Miyamoto Debian 8 08-24-2005 02:42 PM
question about ouput of ls -l Itzac Linux - Newbie 1 05-09-2004 04:18 PM

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

All times are GMT -5. The time now is 10:41 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