Mac Bash脚本失败吗?

时间:2012-05-23 22:09:50

标签: macos qt bash shell terminal

此脚本需要在Mac OSX上运行。以下脚本是构建QT QRC(资源文件定义),它只不过是具有不同扩展名的XML文件。我已经测试了mac上终端中隔离的每个脚本。一切都应该工作,但我无法让for循环正常执行。

此脚本应该:

  1. 列出当前目录中的所有文件
  2. 删除由find
  3. 生成的./
  4. 创建正确的XML
  5. 以下是结果的样子:

    <RCC>
        <qresource prefix="/">
            <file>login.html</file>
            <file>start.html</file>
            <file>base/files.html</file>
        </qresource>
    </RCC>
    

    这是我目前的剧本:

     #!/bin/bash
        #Define the File
            file="Resources.qrc"
        #Clear out the old file, we want a fresh one
            rm $file
        #Format the start of the QRC file
            echo "<RCC>" >> $file
            echo "  <qresource prefix=\"/\">" >> $file
            #Iterate through the directory structure recursively
                for f in $(find . -type f)
                    do
                        #Ensure the file isn't one we want to ignore
                            if [[ $f != "*.qrc" && $f != "*.rc" && $f != "*.h" && $f != "*.sh" ]]
                            then
                                #Strip out the ./ for the proper QRC reference
                                    echo "<file>$f</file>" | sed "s/.\///" >> $file
                            fi
                    done
        #Close the QRC file up
            echo "  </qresource>" >> $file
            echo "</RCC>" >> $file
    

    这就是终端告诉我的事情:

    'build-qrc.sh: line 11: syntax error near unexpected token `do
    'build-qrc.sh: line 11: `           do
    

    任何时候我尝试做一个shell for循环它给我同样的错误。我尝试过半冒号等无济于事。有任何想法吗?感谢。


    感谢chepner,这是最终的脚本。它为QT生成一个完美的QRC资源文件,用于将html项嵌入到webkit驱动的应用程序中。

    #!/bin/bash
    #Define the Resource File
    file="AncestorSyncUIPlugin.qrc"
    #Clear out the old file if it exists, we want a fresh one
    if [ -f $file ] 
    then
        rm $file
    fi
    # Use the -regex primary of find match files with the following
    # extensions: qrc rc sh h. Use -not to negate that, so only files
    # that don't match are returned. The -E flag is required for
    # the regex to work properly. The list of files is stored in
    # an array
    target_files=( $(find -E . -type f -regex ".*\.(png|jpg|gif|css|html)$") )
    
    # Use a compound statement to redirect the output from all the `echo`
    # statements at once to the target file. No need to remove the old file,
    # no need to append repeatedly.
    {
        #Format the start of the QRC file
        echo "<RCC>"
    
        # Use single quotes to avoid the need to escape the " characters
        echo '  <qresource prefix="/">'
    
        # Iterate over the list of matched files
        for f in "${target_files[@]}"
        do
            # Use parameter expansion to strip "./" from the beginning
            # of each file
            echo "  <file>${f#./}</file>"
        done
        #Close the QRC file up
        echo "  </qresource>"
        echo "</RCC>"
    } > $file
    

2 个答案:

答案 0 :(得分:3)

我没有看到任何语法错误,但$f != "*.qrc"不应该按照您的意图运行。它测试f对文字字符串*.qrc的值。这是你的脚本,使用各种技巧缩短:

#!/bin/bash
#Define the File
file="Resources.qrc"
# Use the -regex primary of find match files with the following
# extensions: qrc rc sh h. Use -not to negate that, so only files
# that don't match are returned. The -E flag is required for
# the regex to work properly. The list of files is stored in
# an array
target_files=( $(find -E . -type f -not -regex ".*\.(q?rc|s?h)$") )

# Use a compound statement to redirect the output from all the `echo`
# statements at once to the target file. No need to remove the old file,
# no need to append repeatedly.
{
    #Format the start of the QRC file
    echo "<RCC>"

    # Use single quotes to avoid the need to escape the " characters
    echo '  <qresource prefix="/">'

    # Iterate over the list of matched files
    for f in "${target_files[@]}"
    do
        # Use parameter expansion to strip "./" from the beginning
        # of each file
        echo "    <file>${f#./}</file>"
    done
    #Close the QRC file up
    echo "  </qresource>"
    echo "</RCC>"
} > $file

答案 1 :(得分:0)

  

任何时候我尝试做一个shell for循环它给我同样的错误。我尝试过半冒号等无济于事。有什么想法吗?

我遇到了同样的问题。对我来说,它似乎是由CRLF(Windows)行结尾引起的。一旦我切换到LF(Unix),一切都很顺利。