Got "syntax error near unexpected end of file" in bash script

29,078

You missed a fi for inner if:

if [ "$mes" -lt 12 -a "$mes" -gt 0 ]; then
  echo "muy bien, sigamos."
else
  if [ "$mes" -gt 12 -a "$mes" -lt 0 ]; then
    echo "Creo que eso ya no es un mes!"
    # Missed fi here
  fi
  exit
fi
Share:
29,078

Related videos on Youtube

Divshah
Author by

Divshah

Updated on September 18, 2022

Comments

  • Divshah
    Divshah over 1 year

    I'm making an script that gives me a day like this jjj/yyyy when I give it a day like this dd/mm/yyyy and I need it to have an error when you don't write correctly but I'm starting and it's not working :(

    #! /bin/bash
    
    #Primero debes ingresar el mes
    echo "Ingresa el número de un mes del año"
    read mes
    
    #Condicional 
    #Dependiendo si coloca bien $mes
    if [ "$mes" -lt 12 -a "$mes" -gt 0 ]; then
    echo "muy bien, sigamos."
        else
        if [ "$mes" -gt 12 -a "$mes" -lt 0 ]; then
        echo "Creo que eso ya no es un mes!";
    exit
    fi
    

    When I run it it says "syntax error near unexpected end of file"

    • Anthon
      Anthon over 9 years
    • Stéphane Chazelas
      Stéphane Chazelas almost 8 years
      Your code doesn't make sense. A number cannot be at the same time less than 0 and greater than 12, so your second condition can never be true. Also note that -lt and -gt are for strictly greater. -lt 12 and -gt 0 means from 1 to 11. Use -le for less than or equal.
  • Divshah
    Divshah over 9 years
    Now I have another issue, it doesn't display the second if part... I mean, If I put the number 13 it just ends, it doesn't say "Creo que esto ya no es un mes!"
  • cuonglm
    cuonglm over 9 years
    @Divshah: 13 is not greater than 12 and less than 1
  • Ramesh
    Ramesh over 9 years
    @Divshah, change your -a flag in the second if loop to -o.
  • Divshah
    Divshah over 9 years
    Finally I wrote this: