2

I have this code but doesn't work.

    line="20170425"
    anycopia=${line:0:4}
    mescopia=${line:4:2}
    diacopia=${line:6:2}
    echo $anycopia
    echo $mescopia
    echo $diacopia
    DATE=$(date +%Y%m%d)
    any=${DATE:0:4}
    mes=${DATE:4:2}
    dia=${DATE:6:2}
    echo $any
    echo $mes
    echo $dia
    if [ $anycopia == $any ]; then
        echo "equals"
    else
        echo "not equals"
    fi

Error:

    syntax error near unexpected token fi

I've tried changing "then" but it doesn't matter, just like this:

    if [ $anycopia == $any ]
    then
        echo "equals"
    else
        echo "not equals"
    fi

And same error going on all over the time.

PD: Other answers in Stack Overflow with same question didn't work for me.

Edit: I did this command:

    hexdump -C script.sh

This is the output:

    00000000  6c 69 6e 65 3d 22 32 30  31 37 30 34 32 35 22 0d  |line="20170425".|
    00000010  0a 61 6e 79 63 6f 70 69  61 3d 24 7b 6c 69 6e 65  |.anycopia=${line|
    00000020  3a 30 3a 34 7d 0d 0a 6d  65 73 63 6f 70 69 61 3d  |:0:4}..mescopia=|
    00000030  24 7b 6c 69 6e 65 3a 34  3a 32 7d 0d 0a 64 69 61  |${line:4:2}..dia|
    00000040  63 6f 70 69 61 3d 24 7b  6c 69 6e 65 3a 36 3a 32  |copia=${line:6:2|
    00000050  7d 0d 0a 65 63 68 6f 20  24 61 6e 79 63 6f 70 69  |}..echo $anycopi|
    00000060  61 0d 0a 65 63 68 6f 20  24 6d 65 73 63 6f 70 69  |a..echo $mescopi|
    00000070  61 0d 0a 65 63 68 6f 20  24 64 69 61 63 6f 70 69  |a..echo $diacopi|
    00000080  61 0d 0a 44 41 54 45 3d  24 28 64 61 74 65 20 2b  |a..DATE=$(date +|
    00000090  25 59 25 6d 25 64 29 0d  0a 61 6e 79 3d 24 7b 44  |%Y%m%d)..any=${D|
    000000a0  41 54 45 3a 30 3a 34 7d  0d 0a 6d 65 73 3d 24 7b  |ATE:0:4}..mes=${|
    000000b0  44 41 54 45 3a 34 3a 32  7d 0d 0a 64 69 61 3d 24  |DATE:4:2}..dia=$|
    000000c0  7b 44 41 54 45 3a 36 3a  32 7d 0d 0a 65 63 68 6f  |{DATE:6:2}..echo|
    000000d0  20 24 61 6e 79 0d 0a 65  63 68 6f 20 24 6d 65 73  | $any..echo $mes|
    000000e0  0d 0a 65 63 68 6f 20 24  64 69 61 0d 0a 69 66 20  |..echo $dia..if |
    000000f0  5b 20 24 61 6e 79 63 6f  70 69 61 20 3d 3d 20 24  |[ $anycopia == $|
    00000100  61 6e 79 20 5d 3b 20 74  68 65 6e 0d 0a 20 20 20  |any ]; then..   |
    00000110  20 65 63 68 6f 20 22 68  6f 6c 61 22 0d 0a 65 6c  | echo "hola"..el|
    00000120  73 65 0d 0a 20 20 20 20  65 63 68 6f 20 22 61 64  |se..    echo "ad|
    00000130  65 75 22 0d 0a 66 69 0d  0a                       |eu"..fi..|
    00000139

PDD: I'm running this with Bash on Ubuntu on Windows.

Edit2:

    user@DESKTOP-UO9KRO4:/mnt/d$ cat -v script.sh
    line="20170425"^M
    anycopia=${line:0:4}^M
    mescopia=${line:4:2}^M
    diacopia=${line:6:2}^M
    echo $anycopia^M
    echo $mescopia^M
    echo $diacopia^M
    DATE=$(date +%Y%m%d)^M
    any=${DATE:0:4}^M
    mes=${DATE:4:2}^M
    dia=${DATE:6:2}^M
    echo $any^M
    echo $mes^M
    echo $dia^M
    if [ $anycopia == $any ]; then^M
        echo "hola"^M
    else^M
        echo "adeu"^M
    fi^M
Inian
  • 80,270
  • 14
  • 142
  • 161
GGarriga
  • 25
  • 1
  • 2
  • 7
  • This looks like it should work. Unless there are some invisible characters causing a problem. Can you run `hexdump -C` on the script file (`hexdump -C yourscript.sh`) and post the output? – janos Oct 27 '17 at 07:56
  • 1
    Hi, it runs OK on my computer. – Jiahao Cai Oct 27 '17 at 07:56
  • what is the output of `cat -v yourscript?` – nbari Oct 27 '17 at 08:05
  • 1
    The hexdump doesn't match to the code above. – clemens Oct 27 '17 at 08:06
  • Check this. https://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-n-in-a-bash-script – Jiahao Cai Oct 27 '17 at 08:11
  • `[ "$x" = "$y" ]` is the proper syntax (note the one `=`, not `==`). Some shells support the incorrect `==`, but not all. (bash should) The `^M` are sort of telling don't you think? – David C. Rankin Oct 27 '17 at 08:15
  • I think this question is not exactly a duplicate. dos2unix and unix2dos can be relevant solutions. I personally prefer the "conv" utility, if you have it installed, which I find more convenient. – Ellis Oct 27 '17 at 09:00
  • Thanks Ellis, I think the same as you, but welp. – GGarriga Oct 27 '17 at 10:18

1 Answers1

2

^M is a carriage return, and is commonly seen when files are copied from Windows. Run dos2unix to clean up those meta-characters.

dos2unix script.sh

Also as a safe coding practice,

  1. Always double-quote your variables to not let them split when they contains spaces or any shell meta characters
  2. Define a proper she-bang i.e. the interpreter using which the script should run. (Most cases if bash is available #!/usr/bin/env bash or #!/bin/bash)
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    Or, if you are using, for example, Notepad++, be sure to set the env to Unix(LF) bottom right area of the screen. – dat789 Oct 27 '17 at 08:16
  • 1
    It worked thanks! Thanks to everyone who helped me! – GGarriga Oct 27 '17 at 08:16
  • 1
    if you find yourself often copying scripts over to Ubuntu, I'd suggest either Atom or Geany as your text editor, or both whichever finds you fancy. Glad it's sorted for you. – dat789 Oct 27 '17 at 08:18