3
votes

I'm trying to iterate over each file in a directory. Here's my code so far.

while read inputline
do
  input="$inputline"
  echo "you entered $input";

if [ -d "${input}" ]
  then
    echo "Good Job, it's a directory!"

    for d in $input
      do
        echo "This is $d in directory."
      done
   exit

my output is always just one line

this is $input directory.

why isn't this code working? what am I doing wrong?

Cool. When I echo it prints out

$input/file

Why does it do that? Shouldn't it just print out the file without the directory prefix?

2
csh derived shells have a significantly different syntax (foreach) than those derived from the Bourne shell (for), so it is helpful to tag the question with the one you mean... - dmckee --- ex-moderator kitten
what is your use case for this? if no, you might as well use find -type d. - ghostdog74

2 Answers

7
votes
for d in "$input"/*
3
votes

If you want to simplify it somewhat and get rid of the directory check, you could just write it to work on files and directories, perhaps something like:

read inputline
ls "$inputline" | while read f; do
    echo Found "$f"
done