I am writing a script in bash.

If I am in directory Foo, I want to run this script in only the subdirectories of foo that are named foo with up to three numbers after foo.

Ex: foo1, foo23, foo345 etc..

Test is the name of my script.

This is what I have so far. I cannot remember how to specify exactly what directories I want this script to look for.

Other things of importance: I am running on ssh client.

for i in foo*
        if [ -d $i ]   
               sh ./test 
               echo "Test is working."

The right way to do that is making the script take the directory as a parameter:

for directory in foo*/
    ./test.sh "$directory"

The trailing slash in the for loop ensures that it loops over directories and symlinks to directories. Passing the directory to the script ensures that you can move the script and the target directory anywhere without doing anything special to make it work.

To be more specific with regards to selecting directories, you'll want to read about globs, especially the section about extglob.

  • First, thank you for your help. How would I specify that only to certain subdirectories though? Would I use regular expressions? – user2419571 Jan 28 '15 at 15:56
  • Also, thank you for your explanation too, that really helps. – user2419571 Jan 28 '15 at 15:58
  • Edit: I understand and get it now. Thanks again! – user2419571 Jan 28 '15 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.