2014-04-20 44 views
1

他是朋友。我正在創建一個腳本文件,以便能夠讀取用戶輸入並根據我詢問的問題顯示輸出。當我單獨運行選擇時,它們工作正常,但無法弄清楚爲什麼他們不會顯示,當我把碎片放在一起。在Linux中從用戶選擇bash shell獲取輸出時出錯

#!/bin/bash 
while test $loop = "y" 
do 
echo "$CLEAR" 
clear 
tput cup 5 12 ; echo "L- List all wines by name in alphabetical order." 
tput cup 6 12 ; echo "C- Count all wines in each type and list all 5 types with their count." 
tput cup 10 12; echo "E- Exit the program." 
read choice || continue 
case $choice in 
[Ll]) file="/home/students/domie/wine.txt"; sort -t ":" -k2 $file;; #no output here 
[Cc]) ./count ;; #look below for count script 
[Ee]) clear; exit ;; #this works when script runs 
*) tput cup 14 4; echo "Invalid Code,Press Enter and Try Again"; read choice ;; 
esac 
done 

和計數腳本:

$六計數

while IFS=":" read -ra line; do 
if ((${line[2]} == 2)); then 
    IFS=":" && echo "${line[*]}" 
    ((count++)) 

fi 
done < /home/students/domie/wine.txt 
echo "Count = $count" 

編輯: wine.txt

+0

你是什麼_putting平均碎片R_? – devnull

+0

從'wine.txt'文件中發佈示例數據。 –

+0

當我運行主腳本,並選擇選項L(按名稱按字母順序列出所有葡萄酒)列表不會顯示出來。當我單獨運行這個命令時,它將按順序顯示這個列表,但是當我把它放在主腳本中時它不會。 file =「/ home/students/domie/wine.txt」; sort -t「:」-k2 $文件 – endri

回答

0

試試這個:

#!/bin /bash 

file="wine.txt" 
count() { 
    # Replace below with your counting logic .. 
    while IFS=":" read -ra line; do 
     if ((${line[2]} == 2)); then 
     IFS=":" && echo "${line[*]}" 
     ((count++)) 
    fi 
    done < "$file" 
    echo "Count = $count" 
} 

pause() { 
    read -p "$*" 
} 

while :; do 
    clear 
    echo "L- List all wines by name in alphabetical order." 
    echo "C- Count all wines in each type and list all 5 types with their count." 
    echo "E- Exit the program." 
    read -p "Enter Choice: " choice || continue 
    case $choice in 
     [Ll]) sort -t ":" -k2,2 "$file" ; pause 'Press [Enter] key to continue...';; 
     [Cc]) count ; pause 'Press [Enter] key to continue...';; 
     [Ee]) exit ;; 
      *) pause "Invalid Code, Press Enter and Try Again" ;; 
    esac 
done 
+1

@ JS웃 你是一個天才! :)我真的很想表達我的讚賞,並捐贈一些東西給你的幫助,請讓我知道我在哪裏。並再次感謝你 – endri

+0

謝謝@endri。你的讚美綽綽有餘! ':)' –

+0

祝你好運,繼續做好工作@JS웃 – endri