.bash_profile関数にパイプできますか?

.bash_profile関数にパイプできますか?

コマンドラインを使用してApple Finderでファイルを強調表示する優れた機能がありました。基本的には、osascriptを囲むラッパーです。

私からMac OS X:端末でファイルカラーラベルを変更する方法次のようになります。

# Set Finder label color
label(){
  if [ $# -lt 2 ]; then
    echo "USAGE: label [0-7] file1 [file2] ..."
    echo "Sets the Finder label (color) for files"
    echo "Default colors:"
    echo " 0  No color"
    echo " 1  Orange"
    echo " 2  Red"
    echo " 3  Yellow"
    echo " 4  Blue"
    echo " 5  Purple"
    echo " 6  Green"
    echo " 7  Gray"
  else
    osascript - "$@" << EOF
    on run argv
        set labelIndex to (item 1 of argv as number)
        repeat with i from 2 to (count of argv)
          tell application "Finder"
              set theFile to POSIX file (item i of argv) as alias
              set label index of theFile to labelIndex
          end tell
        end repeat
    end run
EOF
  fi
}

私はそれをviaに入れてvim .bash_profile実行し、をsource .bash_profile使用して関数を実行できましたlabel 2 /Users/brett/Desktop/test.txt。完璧。

しかし、以前のPHP mysql_query(ステートメントをすべてPDOに更新して編集する必要があるファイルを視覚的に強調表示したい場合はどうすればよいですか?

普段は走り、

find /Users/brett/Development/repos/my-repo/ -name "*.php" -print0 | xargs -0 grep -Iil 'mysql_query(' | xargs -I '{}' -n 1 label 2 {}

しかし戻ってきます。

xargs: label: No such file or directory

走ろうとしなければならないという内容を読んexport -f labelだが、それも役に立たないようです。

.bash_profile関数でパス/ファイルをgrepパイプする方法を知っている人はいますか?xargs

ベストアンサー1

labelあなたと話すにはxargs

export -f label
find /Users/brett/Development/repos/my-repo/ -name "*.php" -print0 |
  xargs -0 grep -Iil 'mysql_query(' |
  xargs -I {} -n 1 bash -c 'label 2 {}'

label 2 {}関数xargsを直接呼び出すことはbash -c 'label 2 {}'できないためxargs、関数をlabel親シェルの子プロセスとしてエクスポートし、子シェルをフォークしてそこで関数を処理します。bash

メモ:

  • ~/.bash_profile通常、非ログインシェルでは取得できないため、関数を呼び出しシェルにエクスポートする必要がexport -f labelあります。labelxargs

  • この-cオプションは、bash実行するコマンドにオプション引数文字列から読み込むように指示します。

おすすめ記事