1

I have a series of directories that are only different by a numerical tag.

arr=(0 1 2 3)
i=0
while [ $i -le ${arr}]
do
  dir="~Documents/seed" 
  dir+=${arr[i]}
  echo $dir #works
  cd dir #directory not found
  #do other things#
done

Is it possible to do this?

2
  • 1
    is there a slash missing before Documents? Commented Jun 16, 2014 at 21:43
  • You probably want [ $i -lt ${#arr[@]} ] in line 3 Commented Jun 16, 2014 at 22:08

1 Answer 1

2

This might be easier:

#!/bin/bash
for d in ~/Dcouments/seed*
do
   if [ -d "$d" ]; then
      echo $d
   fi
done

Note:

You have tarfiles in ~/Documents too (with names that also match the wildcard), so I have added an if statement that checks if it is a directory or a file and only reacts to directories.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, echo $d only lists one of the directories.
Please run ls -l ~/Documents and post the output and I'll have another look for you.
Ok, I see what you have now and have updated my answer - it should work now.

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.