I have a function in my script that dont work like I want. The goal is to validate numbers and then formating into decimal.
So I created a test-file thats looks like my function ...
#!/bin/bash
TMP_NUM="$1"
if [[ "$TMP_NUM" = *[[:digit:]]* ]]; then
TMP_DECIMAL=$(awk -v decimal="$TMP_NUM" 'BEGIN {printf("%f", decimal) }')
if [[ "$TMP_DECIMAL" =~ ^[0-9]+([.][0-9]+)?$ ]] ; then
echo "$TMP_DECIMAL"
else
echo "Failed !"
fi
else
echo "failed !"
fi
exit 0
the results ...
suleiman@antec:~$ test.sh 0.123456789
0.123457
suleiman@antec:~$ test.sh 0.123456
0.123456
suleiman@antec:~$ test.sh Hello World
failed !
suleiman@antec:~$ test.sh 0Hello World
0.000000
suleiman@antec:~$ test.sh 0.00xyz01
0.000000
How do I have to change the test parameters to get only numbers formated, and an error message when any letters are behind any numbers, except eE ?
And why the hack my function kills some value in it, like in the first test result.
suleiman@antec:~$ test.sh 0.123456789
0.123457
Aucun commentaire:
Enregistrer un commentaire