#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Programing help (While and Read command)


    Hi i am new to shell scripting. a self learner.
    I am trying tutorials as of now. This below code shows error. and i couldn't find out any workaround. please help me.

    #!/bin/sh
    #script to create simple menu function
    #menu items
    #


    while :
    do
    clear
    echo "--------------------"
    echo "Main Menu"
    echo "-------------------"
    echo "[1] show today date/time"
    echo "[2] show files in current driectory"
    echo "[3] show calender"
    echo "[4] start editor to write letters"
    echo "[5] exit/stop the function or program"
    echo "-------------------"

    echo "Enter your choice between [1]~[5]"

    read userchoicee

    case $userchoicee in
    1) echo "today date 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!input choicce only between [1] ~[5]";
    echo "press a key..." ; read ;;
    esca

    done
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    Apart from having 'esca' in stead 'esac' to terminate the case statement?
    Would you care to share the error you are having?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Hi Simon,
    Thanks for your quick response.
    i used 'esac' instead of 'esca' to terminate the command. But still i am getting the same error. Please find the error below.

    ####Error########
    mesh01@msilcae91:~/Desktop$ ./test09
    ./test09: line 32: unexpected EOF while looking for matching `"'
    ./test09: line 37: syntax error: unexpected end of file
    ############End##############
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    As the error message says, you are missing a delimiter of one sort of another. I looked over the code and missed it - then I copied your code and 'tidied it' a little and the error leaped out ...
    Code:
    #!/bin/sh
    #script to create simple menu function
    #menu items
    #
    
    
    while :
    do
      clear
      echo "--------------------"
      echo "Main Menu"
      echo "-------------------"
      echo "[1] show today date/time"
      echo "[2] show files in current driectory"
      echo "[3] show calender"
      echo "[4] start editor to write letters"
      echo "[5] exit/stop the function or program"
      echo "-------------------"
    
      echo "Enter your choice between [1]~[5]"
    
      read userchoicee
    
      case $userchoicee in
        1)
          echo "today date is `date`, press a key..."
          read dummy
        ;;
        2)
          echo "files in `pwd`"
          ls -l
          echo "press a key..."
          read dummy
        ;;
        3)
          cal
          echo "press a key..."
          read dummy
        ;;
        4)
          vi
        ;;
        5)
          exit 0
        ;;
        *)
          echo "Oops! Input choice only between [1] ~[5]"
          echo "press a key..."
          read dummy
        ;;
      esac
    
    done
    Check the actions done for option 2 and count the number of quotes.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Hi Simon,
    This is working.
    Thanks for your kind support and timing..

    great..
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    Happy to help.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Hi, i am testinh more example of the below program.
    This gives an error
    please help me out to resolve it.

    ###### error ######
    ./test14: syntax error at line 22: `done' unexpected
    ###### error end ######


    ###### program #######

    #!/bin/bash
    while true
    do
    ping cwshpmu0000 2> test1.txt
    ping cwshpmu0715 2> test2.txt

    fir1=`cat test1.txt`
    fir11="ping: unknown host cwshpmu0000"

    fir2=`cat test2.txt`
    fir22="ping: unknown host cwshpmu0715"

    if [ "$fir1" = "$fir11" ]; then
    echo "`date`,cwshpmu0000" >> t.txt
    fi
    if [ "$fir2" = "$fir22" ]; then
    echo "`date`, cwshpmu0715" >> t.txt

    sleep 60
    done


    ########### end of program #####
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    I found out an error.
    That i corrected.
    (i missed fi command in the second condition)
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    Well spotted.

    Can I make a small suggestion? The use of the backticks (the ` character) is deprecated for use as the 'command substitution' delimiters. The use of $(command here ...) is recommended, if only for the fact that it can make things clearer.

    Originally Posted by man page
    3.5.4 Command Substitution

    Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed as follows:

    $(command)

    or

    `command`

    Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

    When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

    Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with backslashes.

    If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo