2012-05-23 94 views
2

此腳本需要在Mac OSX上運行。以下腳本是構建一個QT QRC(資源文件定義),它不過是一個具有不同擴展名的XML文件。我已經在Mac上測試了每個在終端中分離的腳本。所有這一切都應該工作,但我無法讓for循環正確執行。Mac Bash腳本失敗了嗎?

這個腳本應該:

  1. 列出當前目錄
  2. 地帶的所有文件了./由生產找到
  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 

任何時候,我嘗試做了循環殼它給我同樣的錯誤。我已經嘗試過半球和類似的東西無濟於事。有任何想法嗎?謝謝。


感謝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 
+0

我剛測試過你的腳本,它對我來說工作正常......你如何在終端上調用腳本? – leemes

+0

可以是./qrc.sh或sh qrc.sh或bash qrc.sh.我在獅子上。我有點困惑。 – Dovy

+0

你是否也嘗試'爲...;做'(分號後沒有換行)?這是我習慣的語法,但正如我之前所說:您的腳本完美地在我的機器上工作... – leemes

回答

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 
+0

謝謝!我接受你的幫助。 – Dovy

0

任何時候,我嘗試做了循環殼它給我同樣的錯誤。我已經嘗試過半球和類似的東西無濟於事。有任何想法嗎?

我剛剛有同樣的問題。對我而言,它似乎是由CRLF(Windows)行結尾引起的。一旦我切換到LF(Unix),一切都很順利。