To Check whether the Input is a Year or Not?

Hi Guys

I have a Requirement that the input will be given and i have to check whether the input is a Year or not.

For Example

2004,2009 and so on forth will be considered a year and anything else like
12:15 or else will not be.

Have built the below Code

set -x

echo " Enter the Variable to be tested"
read Test_Var

case $Test_Var in

[1000-9999] )  echo "It is a Year";;
 * )           echo "Is is not a Year";;
esac

But everything is going to the "Not Year" Part.

I am little bit new to Shell Scripting. So needed your Help.

Regards
Ajesh

case $Test_Var in
[1-9][0-9][0-9][0-9] )  echo "It is a Year";;
                   * )  echo "Is is not a Year";;
esac
1 Like

Try

case $Test_Var in  
    [1-9][0-9][0-9][0-9] )  echo "It is a Year";; 
     * )           echo "Is is not a Year";;
esac
1 Like
echo " Enter the Variable to be tested"
read Test_Var

if [[ "$Test_Var" -gt 999  && "$Test_Var" -lt 10000 ]]
then
echo "It is a Year";
else
echo "Is is not a Year";
fi

Simultaneous post :wink:

I had tried that but when it comes to a symbol lile ; or >>. the Shell gives a compilation Error while running.

Thanks Anyway

Note that

case $Test_Var in

[1000-9999] )  echo "It is a Year";;
 * )           echo "Is is not a Year";;
esac

will match a single character that is 1, 0, 0, any single digit in the range 0 through 9, 9, 9, or 9. So if Test_var had been set to "6", it would say it is a year. Bracket expressions don't look for ranges of numeric values.

Hope this helps you understand what was going wrong.