私がそれを使用すると、多くの場合、find
次のようないくつかの結果が見つかります。
find -name pom.xml
./projectA/pom.xml
./projectB/pom.xml
./projectC/pom.xml
多くの場合、特定の結果(たとえばedit ./projectB/pom.xml
)を選択したい場合があります。find
出力を列挙して他のアプリケーションに渡すファイルを選択する方法はありますか?良い:
find <print line nums?> -name pom.xml
1 ./projectA/pom.xml
2 ./projectB/pom.xml
3 ./projectC/pom.xml
!! | <get 2nd entry> | xargs myEditor
?
[編集]上記の解決策の中に奇妙なエラーが発生しました。だから、再現段階を説明したい。
git clone http://git.eclipse.org/gitroot/platform/eclipse.platform.swt.git
cd eclipse.platform.swt.git
<now try looking for 'pom.xml' and 'feature.xml' files>
[編集]解決策1 これまでのところ、nl(列挙出力)、head、tailを関数として結合し、$(!!)を使用すると機能するようです。
つまり:
find -name pom.xml | nl #look for files, enumirate output.
#I then define a function called "nls"
nls () {
head -n $1 | tail -n 1
}
# I then type: (suppose I want to select item #2)
<my command> $(!!s 2)
# I press enter, it expands like: (suppose my command is vim)
vim $(find -name pom.xml |nls 2)
# bang, file #2 opens in vim and Bob's your uncle.
[編集]解決策2 「select」を使うのもうまくいくようです。前任者:
findexec () {
# Usage: findexec <cmd> <name/pattern>
# ex: findexec vim pom.xml
IFS=$'\n';
select file in $(find -type f -name "$2"); do
#$EDITOR "$file"
"$1" "$file"
break
done;
unset IFS
}
ベストアンサー1
bash
組み込みの機能を使用してくださいselect
。
IFS=$'\n'; select file in $(find -type f -name pom.xml); do
$EDITOR "$file"
break
done; unset IFS
コメントに追加された「ボーナス」質問の場合:
declare -a manifest
IFS=$'\n'; select file in $(find -type f -name pom.xml) __QUIT__; do
if [[ "$file" == "__QUIT__" ]]; then
break;
else
manifest+=("$file")
fi
done; unset IFS
for file in ${manifest[@]}; do
$EDITOR "$file"
done
# This for loop can, if $EDITOR == vim, be replaced with
# $EDITOR -p "${manifest[@]}"