눈바래다

코프로세스는 특수 양방향 파이프라인으로 스크립트에서 다른 코맨드의 입력에 쓰기를 하고 표준출력으로부터 입력을 받을수 있도록 해준다.

코맨드의 끝에 붙는 |& 기호는 해당 코맨드를 코프로세스로 진행되도록 한다.

코프로세스로 부터 읽고 쓰기 위해서는 print 코맨드나 read 코맨드에 -p 스위치를 사용해야 한다. 이때 출력은 표준출력으로 보내져야 하고 출력메시지의 끝에는 새줄문자가 와야 한다.

한편 exec 코맨드에 >&p <&p 연산자를 사용하면 여러개의 코프로세스를 동시에 진행할수 있다. 파일 기술자 4 코프로세스로 열려면 exec 4>&p 입력한다.

#!/bin/ksh

cat << EOF

****************************************

WELCOME TO THE CALCULATOR PROGRAM

****************************************

EOF

bc |&                                 #open coprocess

while true

do

 print "Select the letter for one for the operators below"

 cat << EOF

  a) +

  s) -

  m) *

  d) /

  e) ^

EOF

 read op

 case $op in

  a) op="+";;

  s) op="-";;

  m) op="*";;

  d) op="/";;

  e) op="^";;

  *) print "Bad operator"

   continue;;

 esac

 print -p scale=3                       #Write to the coprocess

 print "please enter the two number : "

 read num1 num2

 print -p "$num1" "$op" "$num2"        #Write to the coprocess

 read -p result                         #read from coprocess

 print $result

 

 print -n "Continue(y/n)"

 read answer

 case $answer in

  [Nn]*) break;;

 esac

done

print "End-Calculator"