2014-12-06 3 views
0

쉘 (Shell)에서 작은 프로그램을 만드는 법을 배우고 있으며 시스템 계산기 (bc -l)를 사용하는 함수를 정의하는 방법을 모색하고있었습니다. 난 내가 할 노력하고있어 표시됩니다 :유닉스 쉘 스크립트 함수

#!/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

당신은이처럼 함수를 호출 할 수 있습니다 : 평방 – tinySandy

답변

0

당신이 여기에 선택 문 BASH에서 지원되는 코드?

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

0

찾고있는 무슨이입니다. square, cube, rectangle의 함수는 끝내야합니다.

#!/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