【问题标题】:How to detect if a file is a folder in bash script on macOS?如何检测文件是否是 macOS 上 bash 脚本中的文件夹?
【发布时间】:2017-01-11 21:59:13
【问题描述】:

我已经使用 Automator 创建了一个 macOS 服务,它实际上会将 Finder 中的每个文件附加到一个新的 Thunderbird 撰写窗口,并且只是一个简单的 bash 脚本。

    for f in "$@"
do
        open -a /Applications/Thunderbird.app/ "$f"
done

此服务也适用于任何文件夹,但可以肯定的是,您不能将文件夹附加到撰写窗口。但我现在的想法是让脚本检测文件是文档还是文件夹。如果是文件,请附上。如果是文件夹,请先将其压缩,然后再附加。顺便说一句:

if file is folder than
// zip compress folder
// attach *.zip to Thunderbird compose window
else // seems to be a document
// attach document to Thunderbird compose window

但是如何检测文件是否为文件夹,然后在 bash 脚本中将其压缩为 zip 文件?

【问题讨论】:

  • 见:help test | less

标签: bash macos zip thunderbird


【解决方案1】:
if [[ -d "$file" ]]; then
  # do your thing for the directory
else
  # do the other thing for the file
fi

更多详情,请查看相关问题:How do I tell if a regular file does not exist in Bash?

【讨论】:

    【解决方案2】:

    代码:

    #!/bin/bash
    if [ -d "$f" ]; then
        upload_file="$f.zip"
        # zip compress folder
        zip "$f.zip" "$f"
    elif [ -f "$f" ]; then # seems to be a document
        upload_file="$f.zip"
    else # Unknown file type
        echo "Unknown file type." 1>&2
        exit 1
    fi
    # attach file to Thunderbird compose window
    open -a /Applications/Thunderbird.app/ "$upload_file"
    exit 0
    

    说明:
    在 bash 中,“文件夹”被称为“目录”。您应该在 test 中查看手册页。

    $ man test
    

    与您相关的部分是:

    NAME
     test, [ -- condition evaluation utility
    
    SYNOPSIS
     test expression
     [ expression ]
    

    ...

     -d file       True if file exists and is a directory.
    
     -e file       True if file exists (regardless of type).
    
     -f file       True if file exists and is a regular file.
    

    测试文件是否为目录:

    test -d "$f"
    

    [ -d "$f" ]
    

    测试文件是否为普通文件:

    test -f "$f"
    

    [ -f "$f" ]
    

    编辑:在示例代码中引用变量以避免通配符和分词。

    【讨论】:

      【解决方案3】:

      此命令[ -f "$filename" ] 将为文件返回true,而[ -d "$dirname" ] 将为目录返回true。

      我建议也使用文件检查,因为您可能拥有既不是目录也不是文件的东西。

      【讨论】:

        【解决方案4】:

        我会这样处理:

        if [ -d "$fileDirectory" ]; then myCommandDirectories;
        elif [ -f "$fileDirectory" ]; then myCommandFiles;
        elif [ -z "$fileDirectory" ]; then myCommandEmptyArgument;
        else myCommandNotFileDirectory; fi
        

        在上面的代码中,语法if [ -d ... ] 将测试参数是否为directory,语法if [ -f ... ] 将测试参数是否为file,语法if [ -z ... ] 将测试参数是否是unset 或设置为empty string,如果参数不是这些,您仍然可以执行某个命令/脚本(在myCommandNotFileDirectory 上面的示例中)。

        注意:我包括检查空字符串,即使问题中没有问到,因为这是我通常会做的“质量/错误”控制测试——变量@ 987654330@ 在这种情况下永远不应该为空,如果是,我想知道(它会告诉我脚本无法正常工作),因此我通常会将该命令重定向到日志文件,就像这样:

        elif [ -z "$fileDirectory" ]; then somecommand && echo "empty fileDirectory string ocurred" >> /var/log/mylog;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-27
          • 1970-01-01
          • 2014-07-09
          • 1970-01-01
          • 2013-07-08
          • 2023-01-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多