Question

description: the list /home/foo/dirlist.txt contains /tmp/a .. /tmp/z

objective: create subdirs such as /tmp/a/2014/04, /tmp/b/2014/04 ... /tmp/z/2014/04

error: it only creates subdirs when the file has only one directory such as /tmp/a and the end result is expected /tmp/a/2014/04 after execution. however, when the file has several entries such as /tmp/a ... /tmp/z no subdirs are ever created*

how can i achieve my objective? many thanks in advance for any tips and pointers.

## /home/foo/dirlist.txt contains /tmp/a  /tmp/b .. /tmp/z
archdirs=`/usr/bin/cat "/home/foo/dirlist.txt"` 

YYYY=`/usr/bin/date '+%Y'`
MM=`/usr/bin/date '+%m'`

for i in "${archdirs[@]}"; 
do 
    if [ ! -d "$i/${YYYY}/${MM}" ]; then
            /usr/bin/mkdir -p "$i/${YYYY}/${MM}"
fi
done
Was it helpful?

Solution

Please understand that this answer is not a fish, it's a fishing lesson.

Start by debugging this with some print statements to see that the array iteration even attempts to use the values you think it's using. As in try printing i are all the i's even looked at? What about the test statement, does test even pass? (and why bother checking if the row exists? just escape the error?)

Then if that's not the problem move one level deeper and so forth.

If we don't do this there are dozens of other possibilities that could be causing the problem.

At this point you should probably realize that the problem had nothing to do with mkdir, years, months, aix or anything other than your bash syntax.

Good luck!

OTHER TIPS

Your variable archdirs is not an array but a single string. Change your script in this way to read line by line:

archdirs=`/usr/bin/cat "/home/foo/dirlist.txt"` 

YYYY=`/usr/bin/date '+%Y'`
MM=`/usr/bin/date '+%m'`

cat "/home/foo/dirlist.txt" | while read i 
do 
    if [ ! -d "$i/${YYYY}/${MM}" ]; then
            /usr/bin/mkdir -p "$i/${YYYY}/${MM}"
fi
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top