Is there any way to control the sorting that occurs when I run a, for d in * ; do; echo $i; done; in my directory? I have this folder structure:
root/
|__CloudUtils/
|__ComponentSplit/
|__ComponentStatistics/
|__ETLManager/
|__MergeComponent/
|__NormalizeComponent/
|__Reducer/
And I want to change the order so mvn will compile NormalizeComponent second right after CloudUtils.
for d in */ ; do
d=${d::-1}
mvn clean deploy -f "$d"
done
Is there a way to do it?
CodePudding user response:
A variation on Charles's answer:
shopt -s extglob
exception='NormalizeComponent'
dirs=( !("$exception")/ )
dirs=( "${dirs[0]}" "$exception"/ "${dirs[@]:1}" )
for d in "${dirs[@]}"; do
echo "$d"
done
outputs
CloudUtils/
NormalizeComponent/
ComponentSplit/
ComponentStatistics/
ETLManager/
MergeComponent/
Reducer/
CodePudding user response:
You can control the sort results only insofar as there exists a collation order that matches what you need; LC_COLLATE can be used to pick the ordering to use -- but in this case, none of the ones your operating system ships will match the behavior you want.
You can use an extglob with a negative assertion to leave specific directories out of the glob's result, so you can hardcode the first and second items, and do everything else in the ordering the locale's collation settings provide:
#!/usr/bin/env bash
[ -n "$BASH_VERSION" ] || { echo "ERROR: only bash is supported" >&2; exit 1; }
shopt -s extglob
for d in CloudUtils NormalizeComponent !(CloudUtils|NormalizeComponent)/; do
mvn clean deploy -f "${d%/}"
done
