1834418 Members
1854 Online
110067 Solutions
New Discussion

Re: script question

 
SOLVED
Go to solution
Greta Blamire
Frequent Advisor

script question

Just a quick & hopefully easy question:
I'd like to write a script to list a series of filenames followed by their contents. This is what I have so far, but it doesn't list the filename only the directory:
#Create listing of the indices in the custom directory.
#
echo "=========Custom Indices Report [`date '+%D %T' `]====================="
cd /usr/local/bin/indices
for I in `ls` ; do
echo "$PWD. [`date '+%D %T'`]"
cat $I
echo "\n"
done
Thanks!
If you can't face the facts, change them!
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor
Solution

Re: script question

Hi Greta:

Change:

# echo "$PWD. [`date '+%D %T'`]"

to:

# echo "$PWD/$I. [`date '+%D %T'`]"

...and you will have what you want.

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: script question

Hi Greta,

You're very close. Just before the cat ${i} statement insert echo "File: ${i}".

If there are no directories within this directory, you are ok; otherwise you should surround your echo, echo, cat, echo statements
with a test for regular files:

for i in ...
do
if [ -r "${i}" -a -f "${i} ]
then
echo "Date Time"
echo "Filename: ${i}"
cat ${i}
echo
fi

done


Regards, Clay
If it ain't broke, I can fix that.