Linux/Unix Shell Scripting - SHELL SCRIPT...NO IDENTIFICO LOS ERRORES EN PROGRAMA

 
Vista:

SHELL SCRIPT...NO IDENTIFICO LOS ERRORES EN PROGRAMA

Publicado por Mariella (1 intervención) el 17/10/2012 03:45:35
$ ./datediff_with_errors 20121016
Very BAD error # debe ser 0
$ ./datediff_with_errors 20121017
Very BAD error # debe ser 1
$ ./datediff_with_errors 20121231
Very BAD error # debe ser 76
$ ./datediff_with_errors 20111016
Very BAD error # debe ser 366
$ ./datediff_with_errors 20101016 # debe ser 731 pero “se cuelga”
^C # para “matarlo”, Ctrl-C o
# abrir otro terminal y killall datediff*

Terminado
$ nl datediff_with_errors


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
# by vk for INF627 (15/10/2012)
 
usage () {
    echo "Usage: ./datediff date1 [date2]"
    echo "Calculates the days difference between two dates"
    echo "Examples: ./datediff 20121231     # days to the New Year"
    echo "          ./datediff 20120930     # days from the beginning of the month"
    echo "          ./datediff 20121216 20120820     # days of the PUCP semester"
    echo "          ./datediff 1941-09-09 2011/10/12 # days of Dennis Ritchie"
    echo
}
 
if [ "$#" -eq 0 ]; then
    usage; exit 1
fi
 
arg1=$(date -d "$1" +%Y%j 2>/dev/null)
if [ -z "$arg1" ]; then
    echo "Invalid 1st parameter"; exit 2
fi
 
if [ "$#" -eq 1 ]; then
    arg2=$(date +%Y%j)
else
    arg2=$(date -d "$2" +%Y%j 2>/dev/null)
fi
if [ -z "$arg2" ]; then
    echo "Invalid 2nd parameter"; exit 3
fi
 
if [ "$arg1" -gt "$arg2" ]; then
    date1="$arg2"; date2="$arg1"
else
    date1="$arg1"; date2="$arg2"
fi
y1=${date1:0:4}; d1=${date1:4:3}
y2=${date2:0:4}; d2=${date2:4:3}
 
if [ "$y1" -eq "$y2" ]; then
    days=$(($d2 - $d1))
else
    y=$y1
    days=$(($(date +%j -d ${y}1231) - $d1))
    y=$(($y + 1))
    while [ "$y" -lt "$y2" ]
    do
        days=$(($days + $(date +%j -d ${y}1231)))
    done
    days=$(($days + $d2))
fi
 
# Check the algorithm
arg1=$(date -d "$1" +%Y%m%d)
if [ "$#" -eq 1 ]; then
    arg2=$(date +%Y%m%d)
else
    arg2=$(date -d "$2" +%Y%m%d)
fi
if [ "$arg1" -gt "$arg2" ]; then
    date1="$arg2"; date2="$arg1"
else
    date1="$arg1"; date2="$arg2"
fi
if [ "$(date -d "$date1 +$days days" +%Y%j)" != "$date2" ]; then
    echo "Very BAD error"; exit 4
fi
# It's ok

echo "$days"
exit 0
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder