sh script if then else error



  • @Glenn-De-Vriendt I'm afraid using character(s) in shell script is a bad idea.

    This can be a working version of your script (19 lines).

    #!/bin/sh
    if [ "$1" = "1" ]
    then
      echo "####################################"
      echo "## launching webradio Topradio... ##"
      echo "####################################"
      url=http://jwmp3.topradio.be/topradio.mp3
    elif [ "$1" = "2" ]
    then
      echo "####################################"
      echo "## launching webradio Topradio... ##"
      echo "####################################"
      url=http://jwmp3.topradio.be/topradio.mp3
    #else
      echo "##########################################"
      echo "## launching webradio Hitradio Maroc... ##"
      echo "##########################################"
      url=http://hitradio-maroc.ice.infomaniak.ch/hitradio-maroc-128.mp3
    fi
    

    The same script without some unnecessary lines (14 lines).

    #!/bin/sh
    if [ "$1" = "1" ]
    then
      echo "####################################"
      echo "## launching webradio Topradio... ##"
      echo "####################################"
      url=http://jwmp3.topradio.be/topradio.mp3
    elif [ "$1" = "2" ]
    then
      echo "##########################################"
      echo "## launching webradio Hitradio Maroc... ##"
      echo "##########################################"
      url=http://hitradio-maroc.ice.infomaniak.ch/hitradio-maroc-128.mp3
    fi
    

    Happy scripting!



  • @György-Farkas

    Thanks for your tips. I changed the € into # and the quotes after echo. The else was for having a default (which I would change later in the first if => $1 = 1 OR $1 IS NULL).

    if  [ "$1" = "1" ]
    then 
      echo "##########################################"
      echo "## launching webradio Hitradio Maroc... ##"
      echo "##########################################"
      url=http://hitradio-maroc.ice.infomaniak.ch/hitradio-maroc-128.mp3
      
    elif [ "$1" = "2" ]
    then
      echo "####################################"
      echo "## launching webradio Topradio... ##"
      echo "####################################"
      url=http://jwmp3.topradio.be/topradio.mp3
    fi
    

    I still have the same error:
    ####################################
    '## stop playing sounds... ##
    ####################################
    : no process killed
    parameter
    : not found: line 8:
    webradio.sh: line 36: syntax error: unexpected end of file (expecting "then")
    root@Omega-FAF7:~#

    Even if I run the most basic script I get the same error:

    #!/bin/bash
    # Basic if statement
    if [ 1 = 1 ] 
    then
      echo "Equal"
    fi
    

    if.sh: line 6: syntax error: unexpected "fi" (expecting "then")

    I also tried to change #!/bin/bash to #!/bin/sh or leave it but saddly the same result.

    Also a simple case statement doesn't work:

    #!/bin/sh
    
    # use case statement to make decision
    case $choose in
       "one") echo "First option.";;
       "two") echo "Second option.";;
       "three") echo "Third option.";;
       "four") echo "Fourth option.";;
       "five") echo "Fifth option.";;
       "six") echo "Last option.";;
       *) echo "Sorry, no parameter found!";;
    esac
    

    root@Omega-FAF7:~# sh case.sh "one"
    : not foundne 2:
    case.sh: line 4: syntax error: unexpected word (expecting "in")

    Do I need to install something for using this statements?

    Thanks in advance.



  • @Glenn-De-Vriendt Hmm. This script runs on my Omega2 (FW v0.3.2 b232) flawlessly.

    #!/bin/bash
    # Basic if statement
    if [ 1 = 1 ] 
    then
      echo "Equal"
    fi
    

    Do I need to install something for using this statements?

    My Omega2 is almost in factory state. I've only changed the passwords and installed 'nano'.

    Omega2's default shell is 'ash'.
    So - I think - the correct shebang is #!/bin/ash or #!/bin/sh.



  • Thanks @György-Farkas

    The default shell is indeed 'ash'. I changed the shebang to #!/bin/ash but I've got the same result.



  • @Glenn-De-Vriendt Yes. This was only a notice.

    The default shell is indeed 'ash'. I changed the shebang to #!/bin/ash but I've got the same result.


    I don't understand how did you get this error message:
    line 36: syntax error: unexpected end of file (expecting "then")
    if your script is definitely shorter has only 19 lines.



  • @Glenn-De-Vriendt Here you are a working "simple" case example:

    #!/bin/sh
    
    # use case statement to make decision
    
    case $1 in
      1)
        echo "First option."
        ;;
      2)
        echo "Second option."
        ;;
      3)
        echo "Third option."
        ;;
      4)
        echo "Fourth option."
        ;;
      5)
        echo "Fifth option."
        ;;
      6)
        echo "Sixth option."
        ;;
      *)
        echo "Sorry, no parameter found!"
        ;;
    esac
    


  • Thanks @György-Farkas

    When I run the script, I see this:

    root@Omega-FAF7:~# sh /root/case2.sh                                                                                                                
    : not found.sh: line 2: 
    : not found.sh: line 4: 
    /root/case2.sh: line 5: syntax error: unexpected word (expecting "in")
    root@Omega-FAF7:~#
    


  • @Glenn-De-Vriendt Let us know your case2.sh please.



  • @Glenn-De-Vriendt Hmm. Your 'case2.sh' file is a Windows text file where the end of line is 'CR LF' (0x0d 0x0a).

    root@Omega-99A5:~# hexdump -C case2.sh
    00000000  23 21 2f 62 69 6e 2f 73  68 0d 0a 0d 0a 23 20 75  |#!/bin/sh....# u|
    00000010  73 65 20 63 61 73 65 20  73 74 61 74 65 6d 65 6e  |se case statemen|
    00000020  74 20 74 6f 20 6d 61 6b  65 20 64 65 63 69 73 69  |t to make decisi|
    00000030  6f 6e 0d 0a 0d 0a 63 61  73 65 20 24 31 20 69 6e  |on....case $1 in|
    00000040  0d 0a 20 20 31 29 0d 0a  20 20 20 20 65 63 68 6f  |..  1)..    echo|
    00000050  20 22 46 69 72 73 74 20  6f 70 74 69 6f 6e 2e 22  | "First option."|
    00000060  0d 0a 20 20 20 20 3b 3b  0d 0a 20 20 32 29 0d 0a  |..    ;;..  2)..|
    00000070  20 20 20 20 65 63 68 6f  20 22 53 65 63 6f 6e 64  |    echo "Second|
    00000080  20 6f 70 74 69 6f 6e 2e  22 0d 0a 20 20 20 20 3b  | option."..    ;|
    00000090  3b 0d 0a 20 20 33 29 0d  0a 20 20 20 20 65 63 68  |;..  3)..    ech|
    000000a0  6f 20 22 54 68 69 72 64  20 6f 70 74 69 6f 6e 2e  |o "Third option.|
    000000b0  22 0d 0a 20 20 20 20 3b  3b 0d 0a 20 20 34 29 0d  |"..    ;;..  4).|
    000000c0  0a 20 20 20 20 65 63 68  6f 20 22 46 6f 75 72 74  |.    echo "Fourt|
    000000d0  68 20 6f 70 74 69 6f 6e  2e 22 0d 0a 20 20 20 20  |h option."..    |
    000000e0  3b 3b 0d 0a 20 20 35 29  0d 0a 20 20 20 20 65 63  |;;..  5)..    ec|
    000000f0  68 6f 20 22 46 69 66 74  68 20 6f 70 74 69 6f 6e  |ho "Fifth option|
    00000100  2e 22 0d 0a 20 20 20 20  3b 3b 0d 0a 20 20 36 29  |."..    ;;..  6)|
    00000110  0d 0a 20 20 20 20 65 63  68 6f 20 22 53 69 78 74  |..    echo "Sixt|
    00000120  68 20 6f 70 74 69 6f 6e  2e 22 0d 0a 20 20 20 20  |h option."..    |
    00000130  3b 3b 0d 0a 20 20 2a 29  0d 0a 20 20 20 20 65 63  |;;..  *)..    ec|
    00000140  68 6f 20 22 53 6f 72 72  79 2c 20 6e 6f 20 70 61  |ho "Sorry, no pa|
    00000150  72 61 6d 65 74 65 72 20  66 6f 75 6e 64 21 22 0d  |rameter found!".|
    00000160  0a 20 20 20 20 3b 3b 0d  0a 65 73 61 63 0d 0a     |.    ;;..esac..|
    0000016f
    

    In a Linux text file the end of line is 'LF' (0x0a).

    root@Omega-99A5:~# hexdump -C case2.sh
    00000000  23 21 2f 62 69 6e 2f 73  68 0a 0a 23 20 75 73 65  |#!/bin/sh..# use|
    00000010  20 63 61 73 65 20 73 74  61 74 65 6d 65 6e 74 20  | case statement |
    00000020  74 6f 20 6d 61 6b 65 20  64 65 63 69 73 69 6f 6e  |to make decision|
    00000030  0a 0a 63 61 73 65 20 24  31 20 69 6e 0a 20 20 31  |..case $1 in.  1|
    00000040  29 0a 20 20 20 20 65 63  68 6f 20 22 46 69 72 73  |).    echo "Firs|
    00000050  74 20 6f 70 74 69 6f 6e  2e 22 0a 20 20 20 20 3b  |t option.".    ;|
    00000060  3b 0a 20 20 32 29 0a 20  20 20 20 65 63 68 6f 20  |;.  2).    echo |
    00000070  22 53 65 63 6f 6e 64 20  6f 70 74 69 6f 6e 2e 22  |"Second option."|
    00000080  0a 20 20 20 20 3b 3b 0a  20 20 33 29 0a 20 20 20  |.    ;;.  3).   |
    00000090  20 65 63 68 6f 20 22 54  68 69 72 64 20 6f 70 74  | echo "Third opt|
    000000a0  69 6f 6e 2e 22 0a 20 20  20 20 3b 3b 0a 20 20 34  |ion.".    ;;.  4|
    000000b0  29 0a 20 20 20 20 65 63  68 6f 20 22 46 6f 75 72  |).    echo "Four|
    000000c0  74 68 20 6f 70 74 69 6f  6e 2e 22 0a 20 20 20 20  |th option.".    |
    000000d0  3b 3b 0a 20 20 35 29 0a  20 20 20 20 65 63 68 6f  |;;.  5).    echo|
    000000e0  20 22 46 69 66 74 68 20  6f 70 74 69 6f 6e 2e 22  | "Fifth option."|
    000000f0  0a 20 20 20 20 3b 3b 0a  20 20 36 29 0a 20 20 20  |.    ;;.  6).   |
    00000100  20 65 63 68 6f 20 22 53  69 78 74 68 20 6f 70 74  | echo "Sixth opt|
    00000110  69 6f 6e 2e 22 0a 20 20  20 20 3b 3b 0a 20 20 2a  |ion.".    ;;.  *|
    00000120  29 0a 20 20 20 20 65 63  68 6f 20 22 53 6f 72 72  |).    echo "Sorr|
    00000130  79 2c 20 6e 6f 20 70 61  72 61 6d 65 74 65 72 20  |y, no parameter |
    00000140  66 6f 75 6e 64 21 22 0a  20 20 20 20 3b 3b 0a 65  |found!".    ;;.e|
    00000150  73 61 63 0a                                       |sac.|
    00000154
    

    Good luck.



  • @György-Farkas
    Thank you very much. I never thought of that. I'll try it and let it know. I wrote it in the editor.



Looks like your connection to Community was lost, please wait while we try to reconnect.