I'm trying to do a simple for{} loop and I encountered problem with assign a variable inside loop. I will try to explain it on example:
DIFF=(`date +%s -d 20120203`-`date +%s -d 20120126`)/86400 # Difference between 2 dates.
echo $DIFF
end_date_change='20120126'
for i in {1..$DIFF} #Difference between 2 dates inside loop
do
end_date_change=$(end_date_change -d "-1 day") # Now, after every loop I would like to decrease date
echo $end_date_change
done
So the output should looks:
20120126
20120127
20120128
20120201
20120202
20120203
Anyone? Thanks in advance
Related
Well I'm trying to get the difference between two dates in Seconds.MilliSeconds The dates are in Zulu format
I have tried these two approaches doesn't work out for me
ms1=$(date -d '2022-04-22T03:47:56.744551446Z' +%N)
ms2=$(date -d '2022-04-22T03:47:57.095419744Z' +%N)
msdiff=$((ms1 - ms2))
echo "$msdiff"
$ dateutils.ddiff 2022-04-22T03:47:56.744551446Z 2022-04-22T03:47:57.095419744Z -f '%N'
0s
Is there any better way to get the difference in Seconds.MilliSeconds In linux for Z format time zones
Suggesting
ms1=$(date -d '2022-04-22T03:47:56.744551446Z' +%s.%N)
ms2=$(date -d '2022-04-22T03:47:57.095419744Z' +%s.%N)
msdiff=$(awk "BEGIN{print $ms2 - $ms1}")
echo "msdiff=$msdiff"
Output:
msdiff=0.350868
Is there an option of date function?
How do I get LOAD_TEST_START to be 2 minutes forward?
How do I get LOAD_TEST_END to be 2 minutes back?
LOAD_TEST_START=$(date -u +%FT%TZ)
LOAD_TEST_END=$(date -u +%FT%TZ)
This answer helped me and works on the linux-version of date.
LOAD_TEST_START=$(date +%FT%TZ -d "2 minutes ago")
LOAD_TEST_END=$(date +%FT%TZ -d "2 minutes")
On macos/FreeBSD one can use
LOAD_TEST_START=$(date -v+2M +%FT%TZ)
LOAD_TEST_END=$(date -v-2M +%FT%TZ)
Not sure if this is what you want, but you can get the current epoch value and then add/subtract 120 seconds (2 minutes) from it and convert the results back to date.
dateval=`date +%s`
echo "orig "`date -d #$dateval +%FT%T%Z`
start=`expr $dateval + 120`
end=`expr $dateval - 120`
LOAD_TEST_START=`date -d #$start +%FT%T%Z`
LOAD_TEST_END=`date -d #$end +%FT%T%Z`
echo "start "$LOAD_TEST_START
echo "end "$LOAD_TEST_END
I want to compare current date to date present in file [every 30 seconds and in background]
I don't know if I have to do an infinite loop or a while read line loop ...
So, every match of current date with a date of a file, I print the current line matched in xmessage.
( I add a date using ./script.sh 0138 test )
The date is 0138 in this form : +%H%M.
I already tried this but my loop works only one time for the first date in file. Only one window of xmessage is opened... How can this work for each date added ?
while true
do
currentDate="$(date +%H%M)"
futureDate="$(cat test.txt | cut -d ' ' -f 1 | grep "${currentDate}" | head -n 1)"
printLine="$(cat test.txt | grep "${currentDate}")"
if test "${currentDate}" = "${futureDate}"
then
xmessage "${printLine}" -buttons Ok -geometry 400x100 -center
fi
sleep 30
done &
Exemple of file : test.txt
0142 test xmessage
0143 test other xmessage
0144 other test
0145 other xmessage !
Thanks for helping me !
Your main problem is that the execution halts until you "confirm" the message by clicking OK.
You could work around that by detaching xmessage (put an ampersand at the end of the line:
xmessage -buttons Ok -geometry 400x100 -center "${printLine}"&
In a Linux script: I have a file that has two time entries for each message within the file. A 'received time' and a 'source time'. there are hundreds of messages within the file.
I want to calculate the elapsed time between the two times.
2014-07-16T18:40:48Z (received time)
2014-07-16T18:38:27Z (source time)
The source time is 3 lines after the received time, not that it matters.
info on the input data:
The input has a lines are as follows:
TimeStamp: 2014-07-16T18:40:48Z
2 lines later: a bunch of messages in one line and within each line, multiple times is:
sourceTimeStamp="2014-07-16T18:38:27Z"
If you have GNU's date (not busybox's), you can give difference in seconds with:
#!/bin/bash
A=$(date -d '2014-07-16T18:40:48Z' '+%s')
B=$(date -d '2014-07-16T18:38:27Z' '+%s')
echo "$(( A - B )) seconds"
For busybox's date and ash (modern probably / BusyBox v1.21.0):
#!/bin/ash
A=$(busybox date -d '2014-07-16 18:40:48' '+%s')
B=$(busybox date -d '2014-07-16 18:38:27' '+%s')
echo "$(( A - B )) seconds"
you should be able to use date like this (e.g.)
date +%s --date="2014-07-16T18:40:48Z"
to convert both timestamps into a unix timestamp. Getting the time difference between them is then reduced to a simple subtraction.
Does this help?
I would use awk. The following script searches for the lines of interest, converts the time value into a UNIX timestamp and saves them in the start, end variables. At the end of the script the difference will get calculated and printed:
timediff.awk:
/received time/ {
"date -d "$1" +%s" | getline end
}
/source time/ {
"date -d "$1" +%s" | getline start
exit
}
END {
printf "%s seconds in between", end - start
}
Execute it like this:
awk -f timediff.awk log.file
Output:
141 seconds in between
Does exist in linux bash something similar to the following code in PHP:
list($var1, $var2, $var3) = function_that_returns_a_three_element_array() ;
i.e. you assign in one sentence a corresponding value to 3 different variables.
Let's say I have the bash function myBashFuntion that writes to stdout the string "qwert asdfg zxcvb".
Is it possible to do something like:
(var1 var2 var3) = ( `myBashFuntion param1 param2` )
The part at the left of the equal sign is not valid syntax of course. I'm just trying to explain what I'm asking for.
What does work, though, is the following:
array = ( `myBashFuntion param1 param2` )
echo ${array[0]} ${array[1]} ${array[2]}
But an indexed array is not as descriptive as plain variable names.
However, I could just do:
var1 = ${array[0]} ; var2 = ${array[1]} ; var3 = ${array[2]}
But those are 3 more statements that I'd prefer to avoid.
I'm just looking for a shortcut syntax. Is it possible?
First thing that comes into my mind:
read -r a b c <<<$(echo 1 2 3) ; echo "$a|$b|$c"
output is, unsurprisingly
1|2|3
I wanted to assign the values to an array. So, extending Michael Krelin's approach, I did:
read a[{1..3}] <<< $(echo 2 4 6); echo "${a[1]}|${a[2]}|${a[3]}"
which yields:
2|4|6
as expected.
I think this might help...
In order to break down user inputted dates (mm/dd/yyyy) in my scripts, I store the day, month, and year into an array, and then put the values into separate variables as follows:
DATE_ARRAY=(`echo $2 | sed -e 's/\// /g'`)
MONTH=(`echo ${DATE_ARRAY[0]}`)
DAY=(`echo ${DATE_ARRAY[1]}`)
YEAR=(`echo ${DATE_ARRAY[2]}`)
Sometimes you have to do something funky. Let's say you want to read from a command (the date example by SDGuero for example) but you want to avoid multiple forks.
read month day year << DATE_COMMAND
$(date "+%m %d %Y")
DATE_COMMAND
echo $month $day $year
You could also pipe into the read command, but then you'd have to use the variables within a subshell:
day=n/a; month=n/a; year=n/a
date "+%d %m %Y" | { read day month year ; echo $day $month $year; }
echo $day $month $year
results in...
13 08 2013
n/a n/a n/a
Chapter 5 of the Bash Cookbook by O'Reilly, discusses (at some length) the reasons for the requirement in a variable assignment that there be no spaces around the '=' sign
MYVAR="something"
The explanation has something to do with distinguishing between the name of a command and a variable (where '=' may be a valid argument).
This all seems a little like justifying after the event, but in any case there is no mention of a method of assigning to a list of variables.
let var1=var2=var3=0
or
var1=var2=var3="Default value"