2017-04-25 113 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