2016-02-02 20 views
1

因此,我已经用输入脚本解决了我的第一个问题,但现在无论输入什么字母,都只添加数字。运行bash脚本时出现逻辑错误

这里是代码。任何帮助将不胜感激。

#!/bin/bash 

add() { 
     expr $x + $y 
} 

sub() { 
     expr $x - $y 
} 

mult() { 
     expr $x * $y 
} 

div() { 
     expr $x/$y 
} 

echo "Enter a for add, s for subtract, m for multiply or d for divide and 2 numbers" 
read choice x y 

if [ $choice=="a" ] 
then 
     add 
else 
     if [ $choice == "s" ] 
     then 
       sub 
     else 
       if [ $choice == "m" ] 
       then 
         mult 
       else 
         if [ $choice == "d" ] 
         then 
           div 

         fi 
       fi 
     fi 
fi 
+2

”没有机会输入“。不清楚你的意思,你认为你的脚本可以被称为'myscript a 3 4'吗?这是你的输入,否则你必须添加'read x?'在脚本中输入x值“'等等。另外'elif'会更适合你的二次测试。更好的办法是使用'case $ op a)add ... ;;; ....; esac'等。更好地澄清你的Q文本,而不是回应评论。祝你好运。 – shellter

+0

谢谢。我从添加输入的两种方法中改变了一半,并弄糊涂了。 – Bradg89

回答

2

首先的,您希望脚本read the values from the standard input,但你从参数恢复它。

第二个,你没有传递参数给函数。

第三,您不使用函数内的参数。

第四,在使用表达式时,您不会让操作符之间出现空格。

注意Rany Albeg的Wein说,这bash guide已经过时,他建议this one。我也推荐GNU official guideother formats)。

因此,假设你想用你的脚本一样./my-script.sh m 2 3,这里是你的代码,但工作:

#!/bin/bash                                           

add() {                    
    expr $1 + $2                 
}                     

sub() {                    
    expr $1 - $2                 
}                     

mult() {                   
    expr $1 \* $2                 
}                     

div() {                    
    expr $1/$2                 
}                    

echo "Enter a for add, s for subtract, m for multiply or d for divide and 2 numbers" 
x=$2                    
y=$3                    

if [ $1 == "a" ]                 
then                    
    add $x $y                 
else                    
    if [ $1 == "s" ]                
    then                   
     sub $x $y                
    else                   
     if [ $1 == "m" ]               
     then                  
      mult $x $y               
     else                  
      if [ $1 == "d" ]              
      then                 
       div $x $y              
      fi                 
     fi                  
    fi                   
fi 

最后,这是你的脚本微创修改为read the data from the standard input

#!/bin/bash                  

add() {                   
    echo "Result:"                                          
    expr $1 + $2                 
}                    

sub() {                   
    echo "Result:"                
    expr $1 - $2                 
}                    

mult() {                   
    echo "Result:"                
    expr $1 \* $2                
}                    

div() {                   
    echo "Result:"                
    expr $1/$2                 
}                    

echo "Enter a for add, s for subtract, m for multiply or d for divide and 2 numbers" 
read operation                 
echo "Read first parameter"              
read x                   
echo "Read second parameter"              
read y                   

if [ $operation == "a" ]               
then                    
    add $x $y                 
else                    
    if [ $operation == "s" ]              
    then                   
     sub $x $y                
    else                   
     if [ $operation == "m" ]             
     then                  
      mult $x $y               
     else                  
      if [ $operation == "d" ]            
      then                 
       div $x $y              
      fi                 
     fi                  
    fi  
fi 

另外,如果您遇到了一些麻烦,您可以在脚本的开头设置#!/bin/bash -xv,将调试消息添加到脚本中。

+3

tldp Bash指南已过时,并且在某些情况下显然是错误的。虽然部分可能是正确的,但建议避免使用它,特别是作为一个begginer。我建议使用这个[Bash指南](http://mywiki.wooledge.org/BashGuide)来代替:-) –

+0

谢谢Rany,我编辑了回复并添加了一个注释。链接现在重定向您推荐的指南。此外,我添加了GNU Bash官方指南链接。 –

+0

太棒了!谢谢 :-) –

3

好像你想获得X从第一和第二个参数($1$2)Y和读操作(A,S,d,男)从标准输入。 我修改您的代码一点,以克服原剧本的问题,并根据我的假设提供结果:

#!/bin/bash 

# First number. 
x=$1 
# Second number. 
y=$2 
# Result of either addition, subtraction, division or multiplication of  $x and $y. 
result=0 

# Reads operation from user. 
read -ep "Enter a for add, s for subtract, m for multiply or d for divide: " operation 

case $operation in 
    a) result=$((x + y));; 
    s) result=$((x - y));; 
    d) result=$((x/y));; 
    m) result=$((x * y));; 
    *) printf '%s: %s\n' "$operation" "Unknown operation" >&2; exit 1;; 
esac 

printf 'result: %s\n' "$result" 

用法示例:(脚本名称为sof.sh

./sof.sh 5 4 
Enter a for add, s for subtract, m for multiply or d for divide: a 
result: 9 

./sof.sh 5 4 
Enter a for add, s for subtract, m for multiply or d for divide: m 
result: 20 

./sof.sh 5 4 
Enter a for add, s for subtract, m for multiply or d for divide: s 
result: 1 

./sof.sh 5 4 
Enter a for add, s for subtract, m for multiply or d for divide: d 
result: 1 

PS

请注意以下事项:

  • expr在古代是shell代码用来做数学的程序。在像bash这样的POSIX shell中,使用$((expression))。在bash,ksh88 +,mksh/pdksh或zsh中,您还可以使用((expression))或'let 表达式'。
  • 尽管没有在脚本中使用originaly,但在Bash编程时,值得知道的是[[是一个类似于(但功能比[命令更强大)的bash关键字。看到这个Bash FAQTest and conditionals。 除非你正在为POSIX sh写作,否则建议使用[[。 “