Reputation: 17041
I want to write a shell script that loops through all directories under a directory, and call a java program with the directory name as an argument at each iteration.
So my parent directory is provided as an argument to the shell script: eg:
. myShell.sh /myFolder/myDirectory
There are 100 directories under /myFolder/myDirectory
. For each "directory_i", i want to run:
java myProg directory_i
If someone can provide me with a working shell script that'll be perfect!
Upvotes: 0
Views: 5860
Reputation: 78215
You could use find
.
The myShell.sh script might look a bit like this, this is a version that will recursively process any and all subdirectories under your target.
DIR="$1"
find "$DIR" -type d -exec java myProg {} \;
The exact set of find
options available depends on your variety of unix. If you don't want recursion, you may be able to use -maxdepth
as Neeraj noted, or perhaps -prune
, which starts get a bit ugly:
find "$DIR" \( ! -name . -prune \) -type d -exec java myProg {} \;
EDIT: Added prune example.
Upvotes: 10
Reputation: 19779
#!/bin/bash -f
files=`ls $1`
for file in $files; do
if [ -d $file ];then
java myProg $file
# java your_program_name directory_i
fi
done
Upvotes: 2
Reputation: 146231
#!/bin/sh
for i in */.; do
echo "$i" aka "${i%/.}"
: your_command
done
Upvotes: 1