1

I want to get proper output from FOR loop inside WHILE. When i'm using command like this all is OK:

for i in `find ./ -name "*.processed" -mtime +0`; do echo "$i is COOL"; done; 

Output:
1.processed is COOL
2.processed is COOL ...

But, when i'm using this into bash/shell script, find put a list of all files with proper mask into variable (not one by one). Note, redirect "echo $i is COOL" to "wc -l" returns number of all files, damn. See following: Entries of confif file like:

/export/home/.../ProcessedDumps;*.processed

All paths are full paths.

#!/bin/bash

CONF_FILE=$1
DAYS_OLD=0
counter=0
IFS=";"

if [ "$1" = "-h" -o "$1" = "-help" -o "$#" -ne "1" ]; then
    echo "Just archive your files easy!"
    echo "Usage: `basename $0` /path_to_conf/config.cfg" && echo "Exit!"
    exit 1
fi

echo "#########################"
date '+Date: %Y.%m.%d %T'
echo

while read LOG_DIR MASK
do
    cd $LOG_DIR
    echo "Dir changed to `pwd`"
    echo "Searching with mask \"$MASK\""
    for i in `find . -name "$MASK"`
    do
    echo "$i is COOL"
    echo "test"
    done
done < $CONF_FILE

echo
echo "Total archived files: $counter"

echo
date '+Date: %Y.%m.%d %T'

Output:
1.processed
2.processed
...
n.processed is COOL
test

Is bash provides nested loops with different kinds (inner FOR, outer WHILE). Have any ideas?

13
  • I don't see any problem with nested loops. It looks like the output of find isn't being split so you're only foring over one thing. Is IFS set? Commented Dec 5, 2013 at 7:53
  • 2
    have you tried find . -name $MASK? Commented Dec 5, 2013 at 7:53
  • when you do a cd do you give absolute path or relative? Commented Dec 5, 2013 at 7:54
  • please paste content of CONF_FILE Commented Dec 5, 2013 at 7:55
  • I'm confused... Shouldn't find ./ -name "*.processed -mtime +0" have been find ./ -name "*.processed" -mtime +0 ? (quotes only around file name...) Commented Dec 5, 2013 at 7:56

2 Answers 2

1

Rather than setting IFS globally, which is interfering with your for loop, set it locally for the read in the while loop:

while IFS=';' read LOG_DIR MASK; do
    cd $LOG_DIR
    echo "Dir changed to `pwd`"
    echo "Searching with mask \"$MASK\""
    for i in `find . -name "$MASK"`
    do
        echo "$i is COOL"
        echo "test"
    done
done < $CONF_FILE
Sign up to request clarification or add additional context in comments.

Comments

0

the problem seems to be with the shell expansion, the follow excerpt worked as expected in my test here

echo logdir '"*.c"' | while read LOG_DIR MASK
do
  MASK=${MASK#'"'};
  MASK=${MASK%'"'};
  find -name "$MASK";
  for i in `find -name "$MASK"`; do
    echo "[$i]";
  done
done

EDIT: the IFS also has an important role if the filename has whitespaces, as noted by hobbs, in this case, one can use

echo logdir '"*.png"' | while read LOG_DIR MASK
do
  MASK=${MASK#'"'};
  MASK=${MASK%'"'};
  SAVEIFS=$IFS
  IFS='!'
  for i in `find -printf '%h/%f!' -name "$MASK"`; do
    ls -l "$i"
  done
  IFS=$SAVEIFS
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.