2017-05-03 13 views
-1

我一直堅持這一最後幾個小時閱讀功能。我已經看遍了堆棧溢出和在線,不能解決如何使用這個讀取功能。我能夠使它在單獨的腳本中讀取文本文件,但無法使其在我當前創建的腳本中工作。擊:讀取文件,那麼良好的文字和錯誤文本分隔成新的文本文件

腳本目前我正在試圖創建閱讀需求稱爲文本文件input.txt中包含MIPS代碼。它需要將一個文件中正確的代碼和另一個文件中的錯誤代碼分開。例如。 「add $ s0 $ s1 $ s2」將是正確的,並將其放入correct.txt中,而「add $ s0 $ s1 $ z2」將不正確,並將其放入incorrect.txt中。

# Argument names. 
file=$1 
correct=$2 
incorrect=$3 

function usageMessage() { 
echo "Please enter three arguments (the input file, correct instruction output file and incorrect instruction output file)." 
echo "Usage: valspit [input.txt] [correct.txt] [incorrect.txt]" 
} 

function readTest() { 
while read -r "$file" 
do 
echo "$file" 
done < "$1" 
} 

if [ $# -ne 3 ]; then 
usageMessage 
exit 1 
fi 

if [ $# -eq 3 ]; then 
readTest 
exit 1 
fi 

如果有人能告訴我如何讓我讀功能的工作,也告訴我如何使它逐行讀取文件中的行,那麼這將是巨大的。

謝謝。

編輯,這裏是輸入和預期的輸出: input.txt中:

#input.txt  
add $s0 $s1 $s2  
sub $s2 $t0 $t3  
add $s0 $s1 $z2  
lw $t1 8($t2)  
addi $t3 $s0 -9  
sw $s3 4($t0)  
lw $t11 70000($s0) 

incorrect.txt應該輸出:

add $s0 $s1 $z2  
lw $t11 70000($s0) 

而correct.txt應該輸出輸入的其餘部分。 TXT

+1

請提供輸入和預期的輸出 – 123

+0

增加了輸入和輸出 – MoonschoolGamer

+0

如果你複製粘貼你的腳本http://www.shellcheck.net/,你將看到的問題之一是,你是在調用'readTest'函數時沒有傳遞任何參數,但在請求任何人提供幫助之前,您正在使用函數 – Sundeep

回答

0

我不知道驗證碼MIPS的邏輯,但這裏是你的代碼應該是什麼樣子至今。

# Argument names. 
file=$1 
correct=$2 
incorrect=$3 

function usageMessage() { 
    echo "Please enter three arguments (the input file, correct instruction" 
    echo "output file and incorrect instruction output file)." 
    echo "Usage: valspit [input.txt] [correct.txt] [incorrect.txt]" 
} 

function validate() { 
    string="[email protected]" 

    true   # not sure the logic that goes here 
} 

function readTest() { 
    local file="$1"   # if we use $1 in a function, the function nees an argument 

    while read -r line; do # read into the variable line 
     if validate "$line"; then 
     echo "$line" >$correct 
     else 
     echo "$line" >$incorrect 
     fi 
    done <"$file"   # read from file passed to us 
} 

if [[ $# -ne 3 ]]; then 
    usageMessage 
    exit 1     # exit 1 could have went in the function too 
else      # no need for two if's, an else works better 
    readTest $file   # there was no reason to exit as a failure here 
fi