English 中文(简体)
Mac Bash 脚本在 Do 上失败?
原标题:Mac Bash Script fails on Do?

此脚本需要在 Mac OSX 上运行。 以下脚本是用来构建 QT QRC( 资源文件定义), 仅是一个带有不同扩展名的 XML 文件 。 我测试了在 Mac 终端中孤立的脚本的每一部分 。 这一切都是正常的, 但我无法获取循环来正确执行 。

本脚本应:

  1. List all files in the current directory
  2. Strip out the ./ produced by the find
  3. Create the proper 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

每当我试图做一个环绕的贝壳时,它也会给我同样的错误。我已经尝试过半圆形和类似的东西,却毫无用处。有什么想法吗?谢谢。


< streng> 在此完成脚本, 感谢 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
最佳回答

我看不到任何语法错误, 但 $f! > = " *. qrc" 不应该按照您想要的方式工作。 这将测试 < code> 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
问题回答

每当我试图做一个弹壳来环绕它时,我都会犯同样的错误。我已经尝试过半圆形和类似的东西,却毫无用处。有什么想法吗?

我也有同样的问题。对我来说,这似乎是由 CRLF (Windows) (CRLF) (Windows) 线条结尾造成的。一旦我转到 LF (Unix), 一切都会好起来的。





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签