2017-05-03 4 views
-1

私はここ数時間、この読み取り機能についていました。私はStack Overflowとオンライン上のすべてを見てきましたが、このread関数の使い方を理解することはできません。別のスクリプトでテキストファイルを読み込ませることはできますが、現在作成しているスクリプトでは機能しません。Bash:ファイルを読み込んだ後、適切なテキストとエラーテキストを新しいテキストファイルに分割します。

私が現在作成しようとしているスクリプトは、MIPSコードを含むinput.txtというテキストファイルを読む必要があります。あるファイルに正しいコードを、別のファイルに不正なコードを分割する必要があります。例えば。 "add $ s0 $ s1 $ s2"は正しく、correct.txtに入れられますが、 "add $ s0 $ s1 $ z2"は間違っていて、wrong.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

あなたはhttp://www.shellcheck.net/にスクリプトを貼り付けコピーした場合、あなたは不満の一つは、あなたがしていることであることがわかります入力と出力 – MoonschoolGamer

+0

を追加してください'readTest'関数を呼び出す際に引数を渡さずに、その関数に' $ 1'を使用している場合は、助けを求める前に – 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 
関連する問題