LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem in case statement (https://www.linuxquestions.org/questions/linux-newbie-8/problem-in-case-statement-936328/)

rit 03-25-2012 09:30 AM

problem in case statement
 
hello everyone,

Code:

while :
 do
    clear
    echo "-------------------------------------"
    echo " Main Menu "
    echo "-------------------------------------"
    echo "[1] Show Todays date/time"
    echo "[2] Show files in current directory"
    echo "[3] Show calendar"
    echo "[4] Start editor to write letters"
    echo "[5] Exit/Stop"
    echo "======================="
    echo -n "Enter your menu choice [1-5]: "
    read yourch
    case $yourch in
      1) echo "Today is `date` , press a key. . ." ; read ;;
      2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ;  read ;;
      3) cal ; echo "Press a key. . ." ; read ;;
      4) vi ;;
      5) exit 0 ;;
      *) echo "Opps!!! Please select choice 1,2,3,4, or 5";
        echo "Press a key. . ." ; read ;;
 esac
done

when i remove read command from case 1,2,3 it is not displaying the echo content in all first 3 cases but it is working if read command is not removed.
please tell me why this is happening
and why -n is used with echo

catkin 03-25-2012 09:39 AM

Because without the reads, the loop iterates and the clear is executed. Remove the clear and see how it looks.

echo's -n option stops it appending a newline to whatever it echos. In this case it leaves the cursor after your prompt string (which looks right), otherwise the cursor would be on the next line in column one.

rit 03-25-2012 11:01 AM

Quote:

Originally Posted by catkin (Post 4635749)
Because without the reads, the loop iterates and the clear is executed. Remove the clear and see how it looks.

echo's -n option stops it appending a newline to whatever it echos. In this case it leaves the cursor after your prompt string (which looks right), otherwise the cursor would be on the next line in column one.

thankyou for your help


All times are GMT -5. The time now is 05:45 PM.