0

please bear with me and my questions, but I just started programming in bash yesterday effectively.

I have a script that does a lot of stuff so far. And I came to a point where I need to iterate through variables. I created these variables before using a while loop. The $Time variable for example looks like this:

2016-01-29 17:07:00Z
2016-01-29 17:26:20Z
2016-01-29 17:26:20Z
2016-01-29 00:07:00Z

The Grabinterval variable like this:

hour
minute
minute
day

The first step for me is to check if a different variable is not empty. If it is not I go on with checking line per line inside the $Grabinterval variable what kind it is day, hour or minute. That is done by a while loop as well and works good. But no the problems are rising. Now I want to calculate a time difference between a time that was created earlier outside of the while loop and the time given in the first line of the $Time variable. I tried this using the following code:

while read -r line; do
if [[ ! -z "$Filelocation" ]]
then 
    if [[ $line = "day" || $line = "days" ]]
    then 
        Interval="Days"
        GrabTimeNew=$(date +'%Y-%m-%d 00:0'$UPOFFSET':00Z')
        default=$(date --date "-1 day" +'%Y-%m-%d 00:0'$UPOFFSET':00Z')
        start=$(date -d"$line$Time" +%s)
        end=$(date -d"$GRABtime" +%s)
        TimeDiff=$(( $start - $end ))
    fi
fi
done <<< $Grabinterval

This is only on part of the bigger while loop, the other two parts looking for hour and minuteare pretty much the same. The way I tried it here using $line$Time gives me following error message:

date: invalid date ‘day2016-01-29 17:07:00Z\n2016-01-29 17:26:20Z\n2016-01-29 17:26:20Z\n2016-01-29 00:07:00Z’

So it goes over all lines instead of only the dayline I want it to go through. Is there a way to use the first line of the $Timevariable inside the $Grabinterval variable?

I would love to use a for loop, but I have no idea how to use it later in the command block to have the wanted line read in the command block of the if statement.

Thanks, BallerNacken

EDIT: Tried something like this now, but not working either:

while read -r GI TI; do
if [[ ! -z "$Filelocation" ]]
then 
    if [[ $GI = "day" || $GI = "days" ]]
    then 
        Interval="Days"
        GrabTimeNew=$(date +'%Y-%m-%d 00:0'$UPOFFSET':00Z')
        default=$(date --date "-1 day" +'%Y-%m-%d 00:0'$UPOFFSET':00Z')
        start=$(date -d"$TI" +%s)
        end=$(date -d"$GRABtime" +%s)
        TimeDiff=$(( $start - $end ))
    fi
    if [[ $GI = "hours" || $GI = "hour" ]]
    then 
        Interval="Hours"
        GrabTimeNew=$(date +'%Y-%m-%d %H:0'$UPOFFSET':00Z')
        default=$(date --date "-1 hour" +'%Y-%m-%d %H:0'$UPOFFSET':00Z')
        start=$(date -d"$TI" +%s)
        end=$(date -d"$GRABtime" +%s)
        TimeDiff2=$(( $start - $end ))
    fi
    if [[ $GI = "min" || $GI = "minutes" || $GI = "minute" ]]
    then 
        Interval="Minutes"
        GrabTimeNew=$(date +'%Y-%m-%d %H:%M:20Z')
        default=$(date --date "-1 minute" +'%Y-%m-%d %H:%M:00Z')
        start=$(date -d"$TI" +%s)
        end=$(date -d"$GRABtime" +%s)
        TimeDiff3=$(( $start - $end ))
    fi
fi
done < <(paste <(echo "$Grabinterval") <(echo "$Time"))

I don't get any error messages, but no variables were created inside the if statement/while loop.

codeforester
  • 39,467
  • 16
  • 112
  • 140
BallerNacken
  • 305
  • 7
  • 16
  • What do you mean "no variables were created" -- how do you know? You're not printing anything – glenn jackman Feb 10 '16 at 12:40
  • I run the code in the console and see, if they were created and if yes if they have something in them, with `echo $TimeDiff` for example. – BallerNacken Feb 10 '16 at 13:07
  • So... what about that "doesn't work"? – glenn jackman Feb 10 '16 at 13:18
  • What do you mean? I am confused... My script doesn't work, yes. And I have no idea why, that's why I am here, hoping someone has an idea. Even if I put echo everywhere to print want is being created and what not, I don't get anything from that while loop. Using only `done <<< $Grabinterval` gives all the variables I want from inside the while loop, but only for the lines in `$Grabinterval`. So I am doing something wrong with my changes to read from two variables. But I can't figure out what... – BallerNacken Feb 10 '16 at 13:34
  • I think you're saying that you don't see any output from the echo commands inside the while loop. Well, you do enclose the contents of the while loop in an if statement: is $Filelocation empty? Between the `while` and that first `if`, put `echo "$GI - $TI"` -- do you see that output? Do *those* variables hold the expected values? – glenn jackman Feb 10 '16 at 13:49
  • Okay `$GI` has the data that should be in there but has as well the data that should be stored in `$TI`. `$Filelocation` is definitively not empty. If I print it directly before the if statement it has the correct content. So `$GI` reads both variables (`$Grabinterval` and `$Time`). And `$TI` is empty. – BallerNacken Feb 10 '16 at 13:57
  • What do you have in `$IFS`? – glenn jackman Feb 10 '16 at 14:40
  • That's it! Forgot the IFS for this while loop (it was tab). Thank you very much for your patience! – BallerNacken Feb 10 '16 at 15:28
  • No problem. Just remember for the next time that "doesn't work" is not a useful problem description. Tell us what *does* happen – glenn jackman Feb 10 '16 at 15:31

2 Answers2

0

You need to quote the variable to keep the newlines in the <<< $var.

list=$'a\nb\nc'
while read a ; do echo $a ; done <<< $list
while read a ; do echo $a ; done <<< "$list"
choroba
  • 231,213
  • 25
  • 204
  • 289
0

You might want to try something like this:

while read -r interval datetime; do
    #...
done < <(paste <(echo "$Grabinterval") <(echo "$Time"))

That will read one line from Grabinterval and the corresponding line from Time

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • That looks good, but how does the program know how when to use `$line` from `$Grabinterval` and when from `$Time`? At the moment both is in the same while loop. The `$line` in the if statement has to go into the `$Grabinterval` variable and the `$line` inside the if statement needs to go into `$Time`. – BallerNacken Feb 10 '16 at 10:08