2014-12-06 185 views
0

我正在學習在Shell中製作小程序,並且正在研究如何定義使用系統計算器(bc -l)的函數。我會告訴我想要做的事:Unix Shell腳本函數

#!/bin/bash 
square(){ 
I need help here! 
} 
cube(){ 
I need help here! 
} 
rectangle(){ 
I need help here! 
} 
exit(){ 
help 
} 
I need help here! 
echo "Little program that computes the following:" 
echo "a) Surface of a square" 
echo "b) Volume of a cube" 
echo "c) Surface of a rectangle" 
echo "e) exit" 
echo "Choose the option you want to compute" 
read answer 
if [ $respuesta == "a" ] 
then echo "What's the side of the square" 
read l` 

該行我現在該怎麼calll我的功能「吃」用戶的答案,然後顯示計算後不要。事情變得最糟糕,因爲在計算之後,我必須詢問用戶他是否想繼續或退出。如果有人能幫助我,我將非常感激。對不起我的英語。我不是母語的人。

+1

你可以像這樣調用你的函數:square – tinySandy 2014-12-06 01:48:21

回答

0

這是你在找什麼?:

menu="some items to choose from: 
       A) something 
       B) something else" 
sample() { 
    a="$1" 
    b="$2" 
    c="$3" 
    printf "c: %s" "$c" 
    echo "$a+$b" | bc 
    echo "$a*$b" | bc 
    echo "$a/$b" | bc 
} 

add=$(echo "2+2" | bc) 
printf "Added: %s\n" "$add" 

echo "$menu" 
read -p "choose: " choice 

case "$choice" in 
    A) sample "30" "5";; 
    B) sample "2" "2" "2";; 
    *) echo "not a choice";; 
esac 
+0

非常感謝jmunsch – Carlos 2014-12-06 19:20:57

0

這裏是一個支持的BASH與SELECT語句中的代碼。對於正方形,立方體,矩形的功能,您應該很好地完成它。

#!/usr/bin/env bash 

square(){ 
echo "I need help here! - square" 
} 
cube(){ 
echo "I need help here! - cube" 
} 
rectangle(){ 
echo "I need help here! - rectangle" 
} 
exit(){ 
echo "help" 
} 

options=("Surface of a square" "Volume of a cube" "Surface of a rectangle" "exit") 

select opt in "${options[@]}" 
do 
    case $opt in 
     "${options[0]}") 
      echo "you chose choice 1" 
      square 
      ;; 
     "${options[1]}") 
      echo "you chose choice 2" 
      cube 
      ;; 
     "${options[2]}") 
      echo "you chose choice 3" 
      rectangle 
      ;; 
     "${options[3]}") 
      break 
      ;; 
     *) echo invalid option;; 
    esac 
done