2017-04-19 81 views
1

我不斷收到上述錯誤,我無法對其進行編碼。有人可以幫助檢查錯誤,請讓我知道,所以我可以運行它。這是使用替換算法。如果能解決這個問題,我們將非常感激。謝謝。意外令牌附近的語法錯誤完成

#!/bin/bash 
# 
cnt=0 #page fault 
k=0 #pointer 
pg=(0 0 0 0) #frame_number 

IFS=";""," 

filecontent=`cat < Input.csv` 

array=($filecontent) 

echo -e "Page Fault \tpage 0 \t page 1\t page 2\tpage 3 " 

for ((a=0; a<${#array[@]}; ++a)) 

do 

    frame[$a]="-" 
    usebit[$a]= 0 


for pg in "${array[$a]}"; 

do 

    flag=false 

for ((a=0; a<${#array[@]}; ++a)); 

do 
    if [${frame[$b]} = $pg]; 

then 

    usebit[$b]=1 
    flag=true 
    break 

    fi 

done 

let check=0 

if [ $flag=false]; 

then 
    while ($check -le $array]; 

    if [${usebit[$k]}=1; 

    then 
     usebit[$k]=0 
     let "k++" 

    else 

     frame[$k]=$pg 
     usebit[$k]=1 

     echo "Page Fault = replace frame $k" 

     let "cnt++" 
     let "k++" 

    if [$k = $array]; 

    then 

     k=0 
    fi 

     break; 
    fi 

     if [$k = $array]; 

     then 

      k=0 

    fi 


     let "check++" 

    done 

    echo -e " $cnt \t\t ${pg[0]}\t ${pg[1]} \t ${pg[2]} \t ${pg[3]} " 
    done 

    echo "The number of page fault for page frame of 4 in clock policy 
    algorithm is $cnt" 
+1

有幾個語法錯誤。一個好的起點是將你的代碼粘貼到一個shellchecker中:http://www.shellcheck.net/並且你的代碼通過了這個測試。 – odradek

+0

您並未關閉所有的條件語句和循環。如果[$ {usebit [$ k]} = 1'應該是'if [$ {usebit [$ k]} -eq 1]',那麼我計算一個缺少'fi'和一個缺少'done' –

+0

也不匹配的參數'只有一個例子 - 至少還有一個 –

回答

2

http://www.shellcheck.net/在幫助編寫shell腳本方面做了很好的工作。從你的代碼中的錯誤是這樣的:

Line 32: 
if [${frame[$b]} = $pg]; 
^-- SC1009: The mentioned parser error was in this if expression. 
    ^-- SC1035: You need a space after the [ and before the ]. 
    ^-- SC1073: Couldn't parse this test expression. 
         ^-- SC1020: You need a space before the ]. 
         ^-- SC1072: Missing space before ]. Fix any mentioned problems and try again. 

Line 46: 
if [ $flag=false]; 
^-- SC1009: The mentioned parser error was in this if expression. 
    ^-- SC1073: Couldn't parse this test expression. 
       ^-- SC1020: You need a space before the ]. 
       ^-- SC1072: Missing space before ]. Fix any mentioned problems and try again. 

...等等...

+0

Line 23: for pg in「$ {array [$ a]}」; ^ - SC1009:提到的解析器錯誤是在這個循環中。 這是什麼意思? –

+0

下一行說明你需要什麼 - 「'[']裏面的空格」。 – Jakuje

+0

@Jakuje不錯的網站,解決我的問題。 +1 –

相關問題