baf*_*mca 5 shell bash pipe xargs function
我收到了一个很棒的功能,可以使用命令行在 Apple 的 finder 中突出显示文件。它基本上是 osascript 的包装器。
我从Mac OS X: How to change the color label of files from the Terminal 得到它,它看起来像这样,
# 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
}
Run Code Online (Sandbox Code Playgroud)
我把它放在 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 {}
Run Code Online (Sandbox Code Playgroud)
但它返回,
xargs: label: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我读到我应该尝试 running export -f label
,但这似乎也无济于事。
有谁知道我如何将路径/文件grep
通过管道传输xargs
到 .bash_profile 函数?
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 {}'
Run Code Online (Sandbox Code Playgroud)
label 2 {}
请注意后一个调用如何xargs
更改为bash -c 'label 2 {}'
. 由于xargs
无法直接调用该函数,我们将label
函数导出到bash
父 shell 的子进程中,然后 fork 一个子 shell 并在那里处理该函数。
笔记:
~/.bash_profile
通常不是由非登录 shell 获取的,因此export -f label
需要将label
函数导出到 调用的 shell xargs
。
该-c
选项指示bash
从选项参数字符串中读取要执行的命令。
归档时间: |
|
查看次数: |
977 次 |
最近记录: |