0

I am currently a bash script that shall check some data. What I got so far is:

!/bin/bash

#!/bin/bash

find "./" -mindepth 1 -maxdepth 1 -type d -print0 | while IFS= read -r -d '' file; do
    folder=${file##*/}
    echo "Checking ${folder} for sanity..."
    ./makeconfig ${folder} | while read -r line; do
        title=`echo $line | awk -F' ' '{print $2}'`
        echo $title
    done
done

Now what it currently does is: Search every directory in ./ and extract the folders name (thus: removing the ./ from the result of find). Then give it to a self-written tool, which will output some lines like this:

-t 1 -a 2
-t 3 -a 5
-t 7 -a 7
-t 9 -a 8

of which I gather the value behind -t via awk. This also works so far, the problem is, the outer while loop stops after the first iteration, thus checking only one folder. My guess is that the two read commands of the inner and outer loop are colliding somehow. The tool makeconfig definitiveley returns 0 (no error) always. I tried to debug it using sh -x script.sh but it does not show me anything I can deal with.

Can someone point me in the right direction here what is going wrong? If you need ANY further informations, I can give them to you. Ive written a quick mimicking program if you want to test the bash script here (also a script now, just echoing some stuff), just make it executable via chmod +x:

echo "-t 3 -a 4"
echo "-t 6 -a 1"
echo "-t 9 -a 5"

Just put this with the script in a folder and create some subfolders, that should do it to make it work (as much as it does).

Thanks in advance!

EDIT: This is NOT a duplicate as mentioned. The problem here are more the nested read commands than the print0 (maybe that has also something to do with it, but not entirely).

Community
  • 1
  • 1
Nidhoegger
  • 4,973
  • 4
  • 36
  • 81
  • Use `-print`, not `-print0`. – Barmar May 25 '15 at 19:59
  • That `print0` and `IFS` look suspicious to me. Try just `print`. – bishop May 25 '15 at 20:00
  • This works for some cases, but somehow it still stops in the middle (but doing a little bit more iterations) – Nidhoegger May 25 '15 at 20:03
  • If your files have spaces in them, you need to deploy some tricks. [Examples.](http://stackoverflow.com/questions/7039130/bash-iterate-over-list-of-files-with-spaces) – bishop May 25 '15 at 20:04
  • @Barmar: This is not a duplicate. – Nidhoegger May 26 '15 at 06:24
  • That question shows how to read the output of `find -print0`. Isn't that what you need to know? – Barmar May 26 '15 at 06:24
  • No, the problem is the nested read command. The outer read command somehow conflicts with the inner one, resulting in only one loop iteration. Even the solution suggestes in the other thread (ive read it before posting!) wont do any good to the script. – Nidhoegger May 26 '15 at 06:25

1 Answers1

0

IFS= isn't setting the field separator to the null string (\0), but unsetting it entirely, so the entire output of the find command is being read at once. If you run it without the -print0 argument to find it'll be easier to work with in bash. Two other alternatives:

  1. use xargs to run a shell script on each item found with that being the sole argument
  2. use -exec to run the shell script on each item.