I am trying to retrieve the filesize associated with the Logfilename listed in ip_DBfiles.txt with the logfilenames from the server.When matched the size of the file and the filename should be written to 3rd file op_DBfiles.txt
1.ip_DBfiles.txt
Date Logfilename
01/06/2021 /server/base/a.log
02/06/2021 /server1/base1/b.log
2.cd to /server/lgs
db_listfiles="$(cat ${ip_DBfiles} |awk '{print $3}')"
for i in "${db_listfiles[@]}"
do
find . -type f -print|egrep "(${i})" > /dev/null 2>&1
if [ $? -eq 1 ] ;
then
echo "no action"
else
echo
find . -type f -print|egrep "(${i})" -exec du -ah {} \; > filesize.txt
fi
done
- op_DBfiles.txt
Number Date Logfilename size
1 01/06/2021 /server/base/a.log 5
2 02/06/2021 /server1/base1/b.log 6
when using stat I get the following error msg
stat: cannot stat `/server/base/a.log\n/server1/base1/b.log': No such file or directory
Though the file exists it generates empty file in filesize.txt with find command
Appreciate your help!
#!/bin/bash; It is possible that different shells are executing your script file, and the shebang line will make sure that bash will execute it (if that is what you want). - Also, the input${db_files[@]}, are you sure that it is found in both cases?-name "(${i})"will only match names exactly matching(${i})(as a shell glob), whereas-print|egrep "(${i})"will match any files whose name contains(${i})(as an extended regular expression). You haven't shared enough information to know whether that's significant.b_fileat all, I'm guessing it's not the full scriptdb_listfilesas a plain variable (with filenames separated by newlines), but trying to use it as if it were an array. Probably the simplest way to fix this would be to use it without double-quotes (for i in ${db_listfiles}) and hope that word-splitting splits it correctly. Also,> filesize.txtwill delete all previous contents each time through the loop; you probably want>> filesize.txt. BTW, I had to fix your formatting. See this reference.