【问题标题】:Mac Bash Script fails on Do?Mac Bash 脚本在 Do? 上失败?
【发布时间】:2012-05-23 22:09:50
【问题描述】:

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

这个脚本应该:

  1. 列出当前目录下的所有文件
  2. 去掉 find 生成的 ./
  3. 创建正确的 XML

结果应该是这样的:

<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

【问题讨论】:

  • 我刚刚测试了你的脚本,它对我来说运行良好......你如何在终端上调用脚本?
  • ./qrc.sh 或 sh qrc.sh 或 bash qrc.sh。我在狮子。我有点困惑。
  • 您是否也尝试过for ...; do(分号后没有换行)?这是我习惯的语法,但正如我之前所说:你的脚本在我的机器上运行良好......
  • 我确实尝试过。我认为我的终端搞砸了。适用于其他狮子机器。我想我只是要格式化我的lappy。呸。
  • 在脚本开头尝试set -x(或将shebang 更改为#!/bin/bash -x)以在运行脚本时查看脚本的痕迹。这可能会为您提供更多信息并导致诊断。

标签: macos qt bash shell terminal


【解决方案1】:

我没有看到任何语法错误,但 $f != "*.qrc" 不应该按照您想要的方式工作。这会根据文字字符串 *.qrc 测试 f 的值。这是您的脚本,使用各种技巧进行了缩短:

#!/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

【讨论】:

  • 谢谢!我很感激你的帮助。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 2013-01-14
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多