2017-04-25 81 views
1

我對bash相當陌生,並且一直在努力弄清楚如何從文件(例如data.txt或data.csv)中讀取數字到名爲calc的文件.bash。如何在bash中用逗號分隔整數讀取文件

我有接口工作,但我堅持如何讀取到calc.bash中的數字,所以我可以使計算工作。我也想知道如何保持2個數字的位置,我選擇+ - * /,然後使用另一個操作獲取更多數字。

例如。

我們在data.txt中有一個數字1,3,4,6,10,12END的列表 我讀了1和3並將它們加在一起。 如何保存我最後一次離開的地方,以便我可以用數字4進行另一個操作。所以1 + 3 = 4然後4 + 4 = 8然後如果我想減去它將一直保持8-6直到我打完END。但只限於他們選擇這樣做。

如果他們選擇不具有前一個數字+ - * /與下一個。您將轉到列表中的下兩個數字。所以,如果你是做1,3,那麼你移動到4,6

這是我的CalcUI.bash貌似

#!/bin/bash 
while true; do 
    read -p "Enter operation to be performed (+-/ or Q to Quit): " op 
    case $op in 
    [+] ) echo "You chose +"; echo "+" >> Inst.txt; break;; 
    [-]* ) echo "You chose -"; echo "-" >> Inst.txt; break;; 
    [*] ) echo "You chose *"; echo "*" >> Inst.txt; break;; 
    [/]* ) echo "You chose /"; echo "/" >> Inst.txt; break;; 
    [Qq]*) exit;; 
    * ) echo "Please answer using the following +-/ or Q to Quit";; 
    esac 
done 
while true; do 
    read -p "Use previous result as operand?(y/n): " pr 
    case $pr in 
    [Yy] ) echo "You chose y";echo "y" >> Inst.txt; break;; 
    [Nn]*) echo "You chose n";echo "n" >> Inst.txt; break;; 
    * ) echo " Please answer using y or n";; 
    esac 
done 
while true; do 
    read -p "Reset data file pointer to start of data file?(y/n) " reset 
case $reset in 
    [Yy] ) echo "You chose y"; break;; 
    [Nn]*) echo "You chose n"; break;; 
    * ) echo "Enter y or n";; 
    esac 
done 
exec ./Calc.bash & 

這是CalcUI.bash長什麼樣喜歡

Running CalcUI: 
Enter operation to be performed (+-*/ or Q to Quit): * 
Use previous result as operand? (y/n): n 
Reset data file pointer to start of data file? (y/n):n 

Calc.bash run on Tue Apr 4 14:46:24 CDT 2017 process id 2493 
Calculated result for: 3 * 35 
Result: 105 

press <enter> to continue 

我很難搞清楚如何溝通calc.bashdata.txtcalc.bashcalcUI.bash

+0

您好,歡迎SO,你嘗試過這麼遠嗎?你能提供一個最簡潔的例子,說明你已經完成了什麼,並顯示你堅持的地方? – zmo

+0

我用我的代碼更新了帖子。 – Przn

回答

1

使用功能。
當你在一個文件中做所有事情時,更容易看到發生了什麼。
您可以稍後在單獨的文件中創建實用程序,並且source thatfile可以在調用程序中提供可用的環境設置。
大多數時候我使用函數返回結果使用echo(並呼籲my_functionmy_result=$(my_function))。當您向控制檯寫入其他內容時,這會失敗。你可以使用全局變量。

將整數讀爲字段可以通過用換行符替換,來實現(使用tr)。
一個解決方案可能看起來像

function get_operator { 
    while true; do  
    read -p "Enter operation to be performed (+-/ or Q to Quit): " op 
    case $op in              
     [+]) echo "You chose +";break;;        
     [-]) echo "You chose -";break;;        
     [*]) echo "You chose \*, (not supported yet) try again";;  
     [/]) echo "You chose /";break;;        
     [Qq])exit;;             
     *) echo "Please answer using the following +-/ or Q to Quit";; 
     esac                
exit 
    done 
} 

function get_next_operator { 
    while true; do 
     read -p "Use previous result as operand?(y/n): " pr 
     case $pr in 
     [Yy]) break;; 
     [Nn]) echo "You chose n";get_operator; break;; 
     *) echo " Please answer using y or n";; 
     esac 
    done 
} 

unset first_integer 
unset op 
for k in $(echo "2,4,6,8,9" | tr ',' '\n'); do 
    echo "Next integer = $k" 
    if [ -z "${first_integer}" ]; then 
     first_integer=$k 
     continue 
    fi 
    if [ -z "${op}" ]; then 
     get_operator 
    else 
     get_next_operator 
    fi 
    printf "%s %s %s = " "${first_integer}" "${op}" "${k}" 
    ((first_integer = first_integer $op k)) 
    printf "%s\n" "${first_integer}" 
done