Output not in correct format - cd script

I have a script that looks like this:

dirname2=/usr/tmp/filelist/*/*
for dirname2 in /tmp/filelist/*/*; do (cd $dirname2/catalog ||echo "file does not exist" && echo "$dirname2" |cut -d '/' -f 7,8 && echo $i && ls -la |awk 'NR>3 {SUM += $5} END {  print "Total number of kb " SUM }');done 2>>/tmp/allerrors

What happens is that it prints out what I need but I want the lines to be
so that the "files does not exist" has the path after it.

Example of what I need: "file does not exist -DBCRP/1374000000"

instead I am getting the path printed under the "file does not exist statement." How to make it like the above desired output?

DBCRP/1372000000

Total number of kb 6921804
DBCRP1373000000

Total number of kb 6926149
file does not exist
DBCRP/1374000000

Total number of kb 16240522
DBCRP/1375000000

If a one-liner is more than three screens wide it probably shouldn't be a one-liner.

( cd /tmp/filelist
for dirname2 in */*
do
        if [ ! -d $dirname2/catalog ] 
        then
                echo "File does not exist $dirname2" >&2
        else
                ls -la "$dirname2/catalog" | awk '(NR>3) && !/^d/ {SUM += $5} END {  print "Total number of kb " SUM }'
        fi
done )

First, you set dirname2 to ALL subdirs and folders in /usr/tmp/filelist....
Second, you use dirname2 as each of ALL subdirs and fodlers in /tmp/filelist...
In both cases, dirname2 will be a file OR a folder, depending on which item currenrtly is 'parsed'.

BTW, a 1 liner is cool, yes, but NOT to do error checking.

for dirname2 in /tmp/filelist/*/*; do (cd $dirname2/catalog ||echo "file does not exist" && echo "$dirname2" |cut -d '/' -f 7,8 && echo $i && ls -la |awk 'NR>3 {SUM += $5} END {  print "Total number of kb " SUM }');done 2>>/tmp/allerrors
  • Also the red parts do not work the way you use it.
    You can do && and ||, but after the || there cannot be another &&, those need some pratice (or a subshell), i'd suggest to use a regular if statement until then.
  • Orange is waaaay to long too...

hth, gn8

Fix that first please.

Dang, Corona688 was faster.