Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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 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
share|improve this answer
    
Thanks! That worked. – Divshah Sep 12 '14 at 4:23
    
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

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

elfi [con]; then echo "statement"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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