Posted a Question Yesterday

Was wondering if it was too stupid and got deleted? Because it's gone now from the board.

I was asking about For Loops ----?

#!/usr/bin/bash

bank=`cat /export/home/usr/banklist.txt`
cdir=`cat /export/home/usr/mountlist.txt`
for d in $cdir
do
ls -l /apps/data/custdata/$d/$i/incoming/
ls -l /apps/data/custdata/$d/$i/outgoing/

for d in $bank
do

ls -l /apps/data/custdata/$d/$i/incoming/
ls -l /apps/data/custdata/$d/$i/outgoing/
done;
done;

The question was asking how to fill in the variables from two different lists of files and when they match peform an action ls -ltr for example?

If this gets deleted it must mean I need to go to the Unix for dummies thread? :smiley:

Sorry. We had a technical problem yesterday and lost the database. We restored a backup, but we lost a couple of hours of posts.

I don't understand your question, but you show two "for" loops, one nested inside the other. No problem there. But both are using the same variable d. The inner loop needs its own variable.

Ok well not sure I have one list say

lisa a

1
2
3
4

list b

d
e
f
g

So I need something like if $a and $b exist at the same time, then do

ls -l /apps/data/custdata/$a/$b/incoming/
ls -l /apps/data/custdata/$a/$b/outgoing/

The problem being soemtimes there will be an /apps/data/custdata/1/d/incoming/ directory but there will be also times when an /apps/data/custdata/3/f/incoming/ won't exist --- I want to do an ls on the two combos from the two different list files that match?

Try something like:

for a in 1 2 3 4 ; do
   for b in d e f g ; do
       if [[ -d /apps/data/custdata/$a/$b/incoming/ ]] ; then
          ls -l /apps/data/custdata/$a/$b/incoming/
       fi
       if [[ -d /apps/data/custdata/$a/$b/outgoing/ ]] ; then
          ls -l /apps/data/custdata/$a/$b/outgoing/
       fi
   done
done

That -d is just testing to see if a directory exists with that path.

I got zero out put when I ran the script so I did set -xv

and I got data like this

+ [[ -d /apps/data/custdata/tmp2/noma/outgoing/ ]]
+ [[ -d /apps/data/custdata/tmp2/lima/incoming/ ]]
+ [[ -d /apps/data/custdata/tmp2/urma/outgoing/ ]]
+ [[ -d /apps/data/custdata/tmp2/filoma/incoming/ ]]
+ [[ -d /apps/data/custdata/tmp2/triloma/outgoing/ ]]
+ [[ -d /apps/data/custdata/tmp2/cbass/incoming/ ]]
+ [[ -d /apps/data/custdata/tmp2/estate/outgoing/ ]]
+ [[ -d /apps/data/custdata/tmp2/fondu/incoming/ ]]
+ [[ -d /apps/data/custdata/tmp2/kingson/outgoing/ ]]
+ [[ -d /apps/data/custdata/tmp2/lupra/incoming/ ]]
+ [[ -d /apps/data/custdata/tmp2/jag/outgoing/ ]]
+ [[ -d /apps/data/custdata/tmp2/tyson/incoming

So it looks like it passed the if statement but doesn't get a + for the ls ?

Do those directories, as listed, exist?

Yes they do exist so they exist but I don't get an ls of them.

  1. Replace the [[ with the word "test" and get rid of the "]]"

  2. Remove the trailing slash on the directory paths for the if/test

  3. Remember UNIX file systems are case sensitive

Even though the trailing slash is theoretically irrelevant, it has caused me problems in the past where somehow "/a/b/c/" not equivilant to "/a/b/c".

I see no reason to replace the brackets with the "test" command. Is there a functional difference? I prefer the brackets for readability.

Personal choice, I like to see what is actually happening, ie, "if runs a program to determine which branch is executed".

It's not irrelevant, just different. The test says "is the following entry a directory?" Putting a trailing slash says "Don't match unless this is a directory" it's redundant.

It starts to become extremely important to know the difference when you use mv, if you include a trailing / on the desitnation path, it will fail if the dir does not exist. If you leave it off, it will rename the source to the name of the desitnation directory but only if it doesn't exist :confused:

''

I've made all the changes I get the same results.

#!/usr/bin/bash

bank=`cat /export/home/usr/banklist.txt`
cdir=`cat /export/home/usr/mountlist.txt`

for d in $cdir ;do
for i in $bank ;do

  if  [[ -d /apps/data/custdata/$d/$i/incoming ]] ; then
  ls -ltra /apps/data/custdata/$d/$i/incoming
  fi

 if [[ -d /apps/data/custdata/$d/$i/outgoing ]] ; then
 ls -ltra /apps/data/custdata/$d/$i/outgoing
  fi

done
done;

Or

#!/usr/bin/bash

bank=`cat /export/home/usr/banklist.txt`
cdir=`cat /export/home/usr/mountlist.txt`

for d in $cdir ;do
for i in $bank ;do

  if  test -d /apps/data/custdata/$d/$i/incoming  ; then
     ls -ltra /apps/data/custdata/$d/$i/incoming
  fi

 if test -d /apps/data/custdata/$d/$i/outgoing  ; then
    ls -ltra /apps/data/custdata/$d/$i/outgoing
  fi

done
done;

both get no output.

So let's take a look at the new code then...

And a fully qualified subset list of expected files....

I may had to delete this...

If I could PM it let me know.

Try this

find /apps -name "incoming" -type d -print