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"

Can anyone help me?

  • Yes, someone can help you. – Anthon Sep 12 '14 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. – Stéphane Chazelas Jul 28 '16 at 7:33

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

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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