Created
January 16, 2015 20:38
-
-
Save markogresak/c60117be5135f6fc9cdc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Napišite skripto streznik.sh, ki bo komunicirala s svetom preko | |
| # imenovane cevi streznik.cev. Na to cev lahko drugi procesi pošiljajo | |
| # ukaze strežniku: | |
| # - connect ime_cevi (povezovanje, podano je ime cevi, kamor naj strežnik pošilja odgovore) | |
| # - add N1 N2 N3 N4 ... (seštej podana števila) | |
| # - mult N1 N2 N3 N4 ... (zmnoži podana števila) | |
| # - disconnect (odklopi se od strežnika) | |
| # funkcija za ukaz add | |
| function add { | |
| sum=0 | |
| # preberi argumente v tabelo `arr` | |
| read -ra arr <<< $1 | |
| # sestej vse argumente v `arr` | |
| for i in "${arr[@]}"; do | |
| sum=$(( $sum + $i )) | |
| done | |
| echo $sum | |
| } | |
| function mult { | |
| product=1 | |
| # preberi argumente v tabelo `arr` | |
| read -ra arr <<< $1 | |
| # zmnozi vse argumente v `arr` | |
| for i in "${arr[@]}"; do | |
| # preskoci vrednost, ce velja `$1 == 0` | |
| if [[ $i -eq 0 ]]; then continue; fi | |
| product=$(( $product * $i )) | |
| done | |
| echo $product | |
| } | |
| pipe="streznik.cev" | |
| # ce cev ne obstaja, jo ustvari | |
| if [[ ! -p $pipe ]]; then | |
| mkfifo $pipe | |
| fi | |
| # beri iz cevi | |
| while read value < $pipe; do | |
| echo "prebran ukaz: $value" | |
| case $value in | |
| # ukaz connect | |
| connect*) | |
| # preberi argumente v tabelo `arr` | |
| read -ra arr <<< "$value" | |
| # uporabi prvi argument kot ime cevi | |
| outPipe=${arr[1]} | |
| continue | |
| ;; | |
| # ukaz add | |
| # shrani sestevek argumentov v `result` | |
| add*) result=$(add "$value") ;; | |
| # ukaz mult | |
| # shrani zmnozek argumentov v `result` | |
| mult*) result=$(mult "$value") ;; | |
| # ukaz disconnect | |
| # prekini izvajanje zanke | |
| disconnect) break ;; | |
| esac | |
| echo "rezultat: $result" | |
| # zapisi rezultat v izhodno cev | |
| printf $result > $outPipe | |
| done | |
| echo "konec" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment