output a file with a variable name in shell - bash

So I am trying to output a file with the name of like: lastlogin-"yyyymmdd" where it contains the current date.
I figured out the date should be : date +"%Y%m%d" and I tried to do a variable
now = date +"lastlogin-%Y%m%d.txt"
filename = ${now}
xxxxx > ${filename}
but nothing seems to work
Please help

Use command substitution:
lastlogin-"$(date '+%Y%m%d')".txt
To save in a variable:
filename="lastlogin-"$(date '+%Y%m%d')".txt"
and then do:
echo 'foobar' >"$filename"

You should use $() for command execution and storage of result:
now=$(date +"lastlogin-%Y%m%d.txt")

Related

Shell script to fetch log (json format) file between date and timestamp

The log file folder structure is \Mainfolder\folder1\year(2020)\month(07)\date(24)*.json.
Ex: \Mainfolder\folder1\2020\07\24\filename.json.
The .json file is getting created every hour, like 00:00:00_00:59:59.json, 01:00:00_01:59:59.json and so on.
I have to search under the .json file with following inputs.
My current inputs are keyword, start date. Currently I'm taking that Date, and keyword and able to get the output in a file.
Current script for your reference:
#!/bin/bash
set +x
DTE=$(date "+%d-%m-%Y-v%H%m%s")
Date=$1 #yyyy/mm/dd
Keyword=$2 #keyword in string
Start_Time=$3 #hh:mm
End_Time=$4 #hh:mm
BKT=bucketpath/mainfolder/
output=$(gsutil cat -h gs://bucketpath/mainfolder/"$Date"/* | egrep "$Keyword")
echo $output >> $"/tmp/folder/logoutput-$DTE"
gsutil cp -r /tmp/folder/logoutput-$DTE gs://bucketpath/mainfolder/
I have to add end date, Start_Time & End_Time and search in the .json file and get the output in a file like above.
I tried to use awk & sed, but i'm unable to get the output.
Could anyone help me on this script please.
Thanks in advance.
I prepared following script to collect the logs between date and timestamp along with keyword. My log file is in .json format.
The reason for posting here is, it might help someone who is looking for similar script.
#!/bin/bash
set +x
DTE=$(date "+%d-%m-%Y-v%H%m%s")
startdate=$1
enddate=$2
start_Time=$3
end_Time=$4
keyword=$5
BKT=storage/folder
i=$start_time
i1=$(sed 's/.\{3\}$//' <<< "$i")
j=$end_time
j1=$(sed 's/.\{3\}$//' <<< "$j")
curr="$startdate"
while true; do
echo "$curr"
[ "$curr" \< "$enddate" ] || break
output=$(gsutil cat -h gs://storage/folder/"$curr"/"$i1:00:00_$j1:59:59*" | sed -n '/"timestamp":"[^"]*T'$i':/,/"timestamp":"[^"]*T'$j':/p' | grep "$keyword")
echo $output >> $"/tmp/folder/mylog-$DTE"
curr=$( date +%Y/%m/%d --date "$curr +1 day" )
done
gsutil cp -r /tmp/folder/mylog-$DTE gs://storage/folder/

SED command for completing SQL query file

I've got a SQL query file to be filled with a variable (domainname) and two date values (start and end date).
The idea is to bash script the SED command to create SQL query files per month and domain.
Is it smart to search and replace the SQL query file with SED?
When I try so, I need to escape the characters from the date value.
The part of the SQL file looks as follows:
date < ''
AND
date >= ''
AND
to_domain = ''
The input of an example:
to_domain = testing.com date = 2017-04-01 date = 2017-04-02
Any ideas to do this smart?
I'm sorry but I don't have enough experience in SED.
I've Google'd my ass of with this one, but no luck so far.
Thanks in advance!
You can use environment variables in a template query file and generate the new file with envsubst to substitute env vars.
Given the following query-template.txt :
date < '$start_date'
AND
date >= '$end_date'
AND
to_domain = '$to_domain'
You can generate generate query.txt with envsubst and previously define your variables :
export start_date=2017-04-01
export end_date=2017-04-02
export to_domain=testing.com
envsubst < query-template.txt > query.txt
Or put all the variables export in a file vars.sh and source it before performing the substitution :
source vars.sh
envsubst < query-template.txt > query.txt

Bash: escaping variables

I'm writing a bash one liner.
This works (prints date to the console or tries to execute it):
-bash-4.1$ DATE=`$(date --date="2 days ago" +%F)` echo "${DATE}"
But this:
`DATE=$(date --date="2 days ago" +%F)` psql -d some_db -c "select row from table where started >= '${DATE}' and started < ('${DATE}'::date + '1 day'::interval);"
Gives:
ERROR: invalid input syntax for type timestamp: ""
LINE 1: ... table where started >= '' ...
Which means that ${DATE} is empty there.
What should I modify to make it work?
The first command is broken for a couple of reasons. Firstly, you are mixing backticks and $() in such a way that you are executing the output of the date command as if it were the name of another command.
To save the output of a command to a variable, use this syntax:
output=$(command)
Secondly, the variable would be expanded by the shell before it had been assigned a value, so you need to split up the command into two statements:
date=$(date_command); psql -c "select ... '$date'"
...or use a command substitution directly:
psql -c "select ... '$(date_command)"
Why do the date math in bash? PG can do it directly:
SELECT ... WHERE started >= (datefield - interval '2 day')
^^^^^^^^^^^^^^^^^

How to add single quotes to each string in a variable?

I am passing variable $$PSTCMT to a script as an argument:
test.sh $$PSTCMT
in the script the value is show as below:
PSTCMT1=$1
echo PSTCMT1
the output is:
abc,def,fgh
I want to replace above as below
PSTCMT ='abc','def','fgh'
echo $PSTCMT will give below output as
'abc','def','fgh'
If you are in BASH then you can do:
pstcmt1='abc,def,fgh'
pstcmt="'${pstcmt1//,/\',\'}'"
echo "$pstcmt"
'abc','def','fgh'
Without BASH you can use sed:
pstcmt="'$(sed "s/,/','/g" <<< "$pstcmt1")'"

how to get the next hour's date string in bash

I 'm using bash
I have a date string now, for example:
2015111301
(yyyymmddHH)
how to get the date string of next hour?
that is:
2015111302
Try this:
in="2015111301"
out="$(date -d "${in:0:8} ${in:8:2}:00:00 +1hour" '+%Y%m%d%H')"
echo "$out"
Output:
2015111302
See: 3.5.3 Shell Parameter Expansion
This below script worked for me
#!/bin/bash
tempval=$(echo 2015111301 | sed 's/\(.\{8\}\)/\1 /g')
tempval=$(echo "$tempval 1 hour")
date -d "$tempval" +%Y%m%d%H

Resources