0

What is the problem in that loop please?

while read result do filenames[ $j ]="$result" filedates[ $j ]=$(stat -c %y ${filenames[ $j ]} | cut -d ' ' -f1) (( j++ )) done << (ls -t *.gz) 

I am getting this error:

script.sh: line 13: syntax error near unexpected token `(' script.sh: line 13: `done << (ls -t *.gz)' 
1
  • 1
    Not at all clear what you mean. Is each of file2 through file5 the same age, which is 5 days older than file1? What are you trying to do? Commented Jan 9, 2014 at 21:09

2 Answers 2

2

It looks like you're attempting to use Process substitution, but not quite doing it correctly.

You need:

while read result do filenames[ $j ]="$result" filedates[ $j ]=$(stat -c %y ${filenames[ $j ]} | cut -d ' ' -f1) (( j++ )) done < <(ls -t *.gz) 

Note the critical differences in the spacing on the done line. You redirect with a single <; you then use the <(...) for process substitution. Using << means the code is looking for a 'here document'.

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you are have GNU coreutils:

stat -c "%Y %n" * | awk '{print $2, (prev ? (prev-$1)/86400 " days" : ""); prev=$1}' 

Given these files:

touch file1.tar.gz touch -t 01041200 file2.tar.gz touch -t 1312311200 file3.tar.gz touch -t 1312261200 file4.tar.gz touch -t 1312211200 file5.tar.gz 

output:

$ stat -c "%Y %n" * | awk '{print $2, (prev ? (prev-$1)/86400 " days" : ""); prev=$1}' file1.tar.gz file2.tar.gz 5.18753 days file3.tar.gz 4 days file4.tar.gz 5 days file5.tar.gz 5 days 

2 Comments

thanks alot thats work fine final questions please am using this loop while read result do filenames[ $j ]="$result" filedates[ $j ]=$(stat -c %y ${filenames[ $j ]} | cut -d ' ' -f1) (( j++ )) done << (ls -t *.gz) i got this error script.sh: line 12: syntax error near unexpected token (' script.sh: line 12: done << (ls -t *.gz)'
you can't post formatted code in a comment: please update the question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.