2

This script needs to run on Mac OSX. The following script is to build a QT QRC (resource file definition) which is nothing more than an XML file with a different extension. I've tested each piece of the script isolated in terminal on the mac. All is as it should work, but I can't get the for loop to execute properly.

This script should:

  1. List all files in the current directory
  2. Strip out the ./ produced by the find
  3. Create the proper XML

Here's what the result should look like:

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

Here's my current script:

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

And this is what terminal keeps telling me:

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

Any time I try to do a shell for loop it give me the same error. I've tried semi-colns and the like to no avail. Any ideas? Thanks.


Here's the finalized script thanks to chepner. It generates a perfect QRC resource file for QT for use with embedding html items into a webkit driven app.

#!/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
6
  • I just tested your script and it's working fine for me... How do you call the script on the terminal?
    – leemes
    May 23, 2012 at 22:17
  • Either ./qrc.sh or sh qrc.sh or bash qrc.sh. I'm on Lion. I'm kind of confused.
    – Dovy
    May 23, 2012 at 22:20
  • Did you also try for ...; do (without new line after semicolon)? That's the syntax I'm used to, but as I said before: your script works perfectly on my machine...
    – leemes
    May 23, 2012 at 22:24
  • I did try that. I think my terminal is messed up. Works on other lion machines. I think I am just going to format my lappy. Bah.
    – Dovy
    May 23, 2012 at 22:28
  • 1
    Try set -x at the beginning of your script (or change your shebang to #!/bin/bash -x) to see a trace of your script when you run it. That may give you more information and lead to a diagnosis. May 24, 2012 at 3:12

2 Answers 2

3

I don't see any syntax errors, but $f != "*.qrc" shouldn't work the way you intend it. That tests the value of f against the literal string *.qrc. Here's your script, shortened using various tricks:

#!/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
  • Thanks! I appeciate your help.
    – Dovy
    May 24, 2012 at 16:31
0

Any time I try to do a shell for loop it give me the same error. I've tried semi-colns and the like to no avail. Any ideas?

I just had the same problem. For me, it seems to have been caused by CRLF (Windows) line endings. Once I switched to LF (Unix), everything went fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.