1

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"

2
  • Yes, someone can help you.
    – Anthon
    Sep 12, 2014 at 4:05
  • 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. Jul 28, 2016 at 7:33

2 Answers 2

9

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
5
  • 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!"
    – Divshah
    Sep 12, 2014 at 4:24
  • @Divshah: 13 is not greater than 12 and less than 1
    – cuonglm
    Sep 12, 2014 at 4:26
  • @Divshah, change your -a flag in the second if loop to -o.
    – Ramesh
    Sep 12, 2014 at 4:27
  • Finally I wrote this:
    – Divshah
    Sep 12, 2014 at 5:23
  • you can find it in a new answer... please help :(
    – Divshah
    Sep 12, 2014 at 5:27
-2

In the else if statement, you need to write it like this:

elfi [con]; then echo "statement"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.