Artificial intelligent assistant

I need to count date diff using bash I have file with dates: Mar 16 Mar 12 Mar 13 Mar 19 Mar 14 Mar 17 and I need to calculate amount of days that passed till now. I come with this function: datediff() { d1=$(date -d "$1" +%s); d2=$(date -d "$2" +%s); echo $(( (d1 - d2) / 86400 )) days; } $ datediff 'now' '13 Mar' 114 days but I need some loop that will calculate that for every line

You can use a `while` loop, where the condition is based on the ability to read from standard input:


$ cat input.txt
Mar 16
Mar 12
Mar 13
Mar 19
Mar 14
Mar 17
$ cat ex.sh
#!/bin/bash

datediff() {
local d1="$(date -d "$1" +%s)"
local d2="$(date -d "$2" +%s)"

echo "$(( (d1 - d2) / 86400 )) days"
}

while read line; do
datediff 'now' "${line}"
done < "${1}"
$ ./ex.sh input.txt
111 days
115 days
114 days
108 days
113 days
110 days


The script here takes a single argument: the input file. While it can read a line from the file, it calls your `datediff` function passing `now` and the content of the `line` that it read from the file.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy c293ab2817672243e434eb04956e8e8c