I'm building a script that may received start and end date as parameters. I whant to make it as flexible as possible so I'm accepting epoch and date in a way that "date --date=" command may accept. In order to know if parameter provided is an epoc or a "date --date=" string I evaluate if the value is a number. For this a built a function:
function isNumber ()
{ [[ "$1" =~ "^[0-9]+$" ]] && echo true || echo false
}
The intended use of the funtion is as following
while getopts :e:F:G:hmo:r:s:vZ dOpts
do
case $dOpts in
"e") if $(isNumber "$OPTARG")
then
dEnd="$OPTARG"
else
dEnd=$(date --date="$OPTARG" +%s)
fi
;;
"s") if $(isNumber "$OPTARG")
then
dStart="$OPTARG"
else
dStart=$(date --date="$OPTARG" +%s)
fi
;;
esac
done
shift $((OPTIND-1))
I'm running on:
# cat /proc/version
Linux version 2.6.32-279.19.1.el6.x86_64 (mockbuild@x86-001.build.bos.redhat.com) (gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) ) #1 SMP Sat Nov 24 14:35:28 EST 2012
And my bash version is:
# rpm -qa bash
bash-4.1.2-9.el6_2.x86_64
Every time I evaluate the function it returns false. I made this test on my console and it do return false every time.
# isNumber e-1month
false
# isNumber 1420088400
false
# [[ 1420088400 =~ '[0-9].' ]] && echo true || echo false
false
# [[ 1420088400 =~ '[0-9]+' ]] && echo true || echo false
false
# [[ "1420088400" =~ '^[0-9]+$' ]] && echo true || echo false
false
What I'm I doing wrong with this redex evaluation? I'm I missing an option on bash? This is my invocation line of the script
#!/bin/bash
Thank you for attention to this message.