You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
919 B
Bash
46 lines
919 B
Bash
#!/bin/bash
|
|
|
|
extension=$1
|
|
path=$2
|
|
cmd="${@:3}"
|
|
|
|
execInDir() {
|
|
file=$1
|
|
|
|
# get folder
|
|
folder=$(dirname "$file")
|
|
# get local file
|
|
filename=$(basename "$file")
|
|
(
|
|
# switch to it
|
|
cd "$folder"
|
|
# exec cmd with filename behind it
|
|
$cmd $filename
|
|
)
|
|
}
|
|
|
|
# Check if path is file and matches extension
|
|
if [[ -f "$path" && "${path##*.}" = "$extension" ]]; then
|
|
execInDir $path
|
|
else
|
|
if [[ -f "$path" ]]; then
|
|
# Strip any filename
|
|
path=$(dirname "$path")
|
|
fi
|
|
|
|
# Try 3 times
|
|
for i in {0..3}; do
|
|
# Get files with extension
|
|
files=$(find "$path" -iname "*.$extension")
|
|
if [[ "$files" = "" ]]; then
|
|
# If there are none, go up and search again
|
|
path="${path}/.."
|
|
else
|
|
for file in $files; do
|
|
execInDir $file
|
|
done
|
|
break
|
|
fi
|
|
done
|
|
fi
|