I have a code like this
v_time=12:12:12
correctTimeFlag=$(echo $v_time|awk '{for(i=1;i<=NF;i++) if( $i ~ /[0-2][0-9
]:[0-5][0-9]:[0-5][0-9]/){print i}}')
if [ correctTimeFlag -gt 0 ]
then
echo "correct"
fi
I need a equivalent function to replace the line
correctTimeFlag=$(echo $v_time|awk '{for(i=1;i<=NF;i++) if( $i ~ /[0-2][0-9
]:[0-5][0-9]:[0-5][0-9]/){print i}}')
by something like
correctTimeFlag=$( chkTimeFormat $v_time)
Please help
chkTimeFormat()
{
time="$1"
echo "$time" | grep -v "^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$" >/dev/null && echo "0" || echo "1"
}
How about:
case $v_time in
[0-2][0-9]:[0-5][0-9]:[0-5][0-9])
echo "correct"
esac
--
correctTimeFlag() {
case $1 in
[0-2][0-9]:[0-5][0-9]:[0-5][0-9]) return 0
esac
return 1
}
if correctTimeFlag "$v_time"; then
echo "\$v_time is correct"
fi
pamu
December 11, 2012, 4:01am
4
check_time () {
[[ $(echo $v_time|awk '{for(i=1;i<=NF;i++) if( $i ~ /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/){print i}}') ]] && echo "correct" || echo "0"
}
function get_time () {
echo $v_time | awk '{for(i=1;i<=NF;i++) if( $i ~ /[0-2][0-9]:[0-5][0-9]:[0-5][0-9]/){print i}}'
}
v_time="12:12:12"
correctTimeFlag=$(get_time)