use*_*332 3 find osx-mountain-lion
我正在尝试在 Mac OSX Mountain Lion 上使用以下命令来获取我的邮件文件夹列表作为我的 .muttrc 文件的一部分:
`echo -n "+ "; find ~/.mail/fastmail -maxdepth 1 -type d -name ".*" -printf "+'%f' "`
find: -printf: unknown primary or operator
-bash: +: command not found
Run Code Online (Sandbox Code Playgroud)
我怎样才能重写它以获得所需的结果?
OSXfind 没有-printf动作。该+: command not found错误是因为你的命令括在反引号(``), so the shell is treating the results of the找到command as a command and attempting to execute them, specifically it is trying to execute+`这是你运行该命令打印的第一件事情。如果你运行你会得到同样的错误
`echo -n "+"`
Run Code Online (Sandbox Code Playgroud)
反勾号用于将命令的结果保存到变量中,因此上面给出了错误,但这不会:
foo=`echo -n "+"`
Run Code Online (Sandbox Code Playgroud)
你没有说你想要的输出是什么。根据您的问题,我假设您想获取给定目录中以 a 开头的所有文件夹的列表,.并将它们的名称打印在同一行上,以+. 如果是这样,您可以执行以下操作:
find ~/.mail/fastmail -maxdepth 1 -type d -name ".*" -exec echo -n "+'{}' " \;
Run Code Online (Sandbox Code Playgroud)
示例输出:
+'/home/terdon/.mail/fastmail/.bar' +'/home/terdon/.mail/fastmail/.foo'
Run Code Online (Sandbox Code Playgroud)
将此命令的输出作为输入传递给另一个程序(mailbox例如),请执行以下操作:
mailbox `find ~/.mail/fastmail -maxdepth 1 -type d -name ".*" -exec echo -n "+'{}' " \;`
Run Code Online (Sandbox Code Playgroud)
或者
mailbox $(find ~/.mail/fastmail -maxdepth 1 -type d -name ".*" -exec echo -n "+'{}' " \;)
Run Code Online (Sandbox Code Playgroud)
回应 OP 的评论:
如果您只想要所有文件夹,则不需要-name, 删除引号,只需不要引用{}。我还将假设您不想要父文件夹 ( fastmail),因此-mindpeth 1:
find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d -exec echo -n "+{} " \;
Run Code Online (Sandbox Code Playgroud)
删除路径稍微复杂一些,因为与您可能期望的相反,您不能只basename在-exec调用中使用。你需要发挥创意,这里有几个选择:
解析 awk
mailbox `find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d |
awk -F"/" '{printf "+%s ",$NF}'`
Run Code Online (Sandbox Code Playgroud)
-F"/"告诉awk使用/的字段分隔符,然后打印+,然后最后一个字段($NF),这将是该文件夹的名称。
使用for循环(假设您的文件夹名称没有奇怪的字符或空格)
mailbox `for dir in $(
find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d
); do echo -n "+$(basename $dir) "; done`
Run Code Online (Sandbox Code Playgroud)
如果您的文件夹名称包含空格或奇怪的字符,请改用:
mailbox `find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d |
while IFS= read -r dir; do echo -n "+$(basename $dir) "; done`
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
5511 次 |
| 最近记录: |