Substituting New Relic query element with Bash variable - Bash - string

I would like to query New Relic (a logging/monitoring service) for some metrics using the New Relic API. I am running this query through a Docker container using a Bash script, and attempting to pass in a dynamic variable.
Here is my simplified script:
##!/bin/bash
day='Since 1 day ago'
#GPU Metrics Query
newrelic nrql query --accountId {account_id} --query 'SELECT * FROM NvidiaGpuMetricSample $day'
The query that the New Relic API accepts is:
newrelic nrql query --accountId {account_id} --query 'SELECT * FROM NvidiaGpuMetricSample Since 1 day ago'
And the error message I receive with this Bash script above is:
level=fatal msg="NRQL Syntax Error: Error at line 1 position 37, unexpected '$'"
I have tried to use different methods to pass in the variable, for example using 'eval $day' and passing that in a new variable or using /"$day"/ within the query string, but nothing seems to give the API what it is looking for.
Summary: I would like to pass a string variable into a string query using Bash.

Use double quotes instead of single quotes.
newrelic nrql query --accountId {account_id} --query "SELECT * FROM NvidiaGpuMetricSample $day"
Referencing variables in Bash is possible only inside double quotes, see https://tldp.org/LDP/abs/html/quotingvar.html

Related

How to get the id of a VM in azure where power state is running and a specific tag is null?

So I am trying to get the ID of all VMs across all subscriptions and regions, where a specific tag is null. For this I am using the following command
az vm list -d --query '[?!not_null(tags.run)]|[].id'
Please note: I want to get the ids only if the tag doesn't exist
Here notice I need to use single quotes to cover the query as I am using the '!' operator to inverse the not_null() function. If I were to use double quotes bash will throw an event not found error.
So now the problem arises when I also want to add a condition to check the current state of the VM and return id only if it is running and tag doesn't exist.
az vm list -d --query '[?!not_null(tags.run)] | [?powerState=="VM running"].id'
Here I have to wrap VM running in double quotes and this gives me an empty output as the string is not being matched because the query expects single quotes like so -
"[?powerState=='VM running'].id"
Could someone help me with a workaround for this?
Use raw string literals for VM running string. You just have to surround your string with a back tick and a double quote.
az vm list -d --query '[?!not_null(tags.run)]|[?powerState==`"VM running"`].id'

Linux - Store a sql select value in a variable bash

I want to store the value of the sqlite statement in a variable
backup=$(sqlite3 "/home/miguel/Desktop/SO/ProjetoFinal/Backup_Principal.db" "SELECT periocidade_backup FROM STORAGE WHERE path'$path';")
But when i echo $backup it returns the following:
sqlite3 "/home/miguel/Desktop/SO/ProjetoFinal/Backup_Principal.db" "SELECT periocidade_backup FROM STORAGE WHERE path='$path';"
What am I doing wrong?
the part of your code '$path' is using a single quote which is literal and show exactly as what is in the quotes, which would not use the variable's value. using speech marks like the following should work, "'$path'"

I needs to execute one sql query against two DBs in at a time and export the data to csv files [duplicate]

I have file1.sh file and which internally needs to execute one sql query against two Oracle DBs at a same time and needs to export date to csv fiiles, below is the sample shellscript which executes the query against two dbs.
....
#!bin/bash
set -X
sqlplus -S ${user1}#${DBCONNECTIONNAME_1}/${Pwd} Datesquery.sql & >> ${Targetdirectory}/csvfile1.csv
sqlplus -S ${user1}#${DBCONNECTIONNAME_2}/${Pwd} Datesquery.sql & >> ${Targetdirectory}/csvfile2.csv
sed 1d csvfile2.csv > file2noheader.csv
cat csvfile1.csv file2noheader.csv > ${Targetdirectory}/Expod.csv
....
But it does not connect to DB and does not execute any query and simply displays sqlplus manual as how to use the connection string, please let me know how to call one query against two dbs and execute them in parrallay and binds output to separate csv files.
A given sqlplus session can only connect to one db at a time, so your requirement 'at the same time' is essentially a non-starter. If 'at the same time' really means 'sequentially, in the same script, then you are back to fixing your connect string. And at that you 'have more errors than an early Mets game' (with apologies to any NY Mets fans).
First, your script indicates that your sqlplus command is the very first actual command following specification of your shell processor and 'set -x'. Yet you make heavy use of environment variables as substitutions for username, password, and connection name - without ever setting those variables.
Second, your use of an '&' in the command line is totally confusing to both me and the parser.
Third, you need to preceed your reference to the sql script with '#'.
Fourth, your order of elements in the command line is all wrong.
Try this
#!/bin/bash
orauser1=<supply user name here>
orapw2=<supply password here>
oradb_1=<supply connection name of first database>
#
orauser1=<supply user name here>
orapw2=<supply password here>
oradb_1=<supply connection name of first database>
#
Targetdirectory=<supply value here>
#
sqlplus -S ${orauser1}/${orapw1}#${oradb_1} #Datesquery.sql >> ${Targetdirectory}/csvfile1.csv
sqlplus -S ${orauser2}/${orapw2}#${oradb_1} #Datesquery.sql >> ${Targetdirectory}/csvfile2.csv
Or create a database link form one DB to other and then run both sqls in one db, one over DB link.
select * from tab1
union
select * from tab1#db_link

How to stop GitLab from evaluating $ inside variable value?

I am passing GitLab variable while running CI/CD pipeline as below.
type - variable
key - password
value - {"a": "abc$def#pqr"}
I am reading it in some GitLab pipeline stage as below.
echo $password
It is showing as below.
{"a":"abc#pqr"}
But I want it to show as below.
{"a":"abc$def#pqr"}
I don't want it to evaluate $def as blank
Note:
I tried with \ escaping and with single quotes too.
I need this to be in json kind of format itself for further use.
Works fine on using double $$ instead of single $

BigQuery Command Line - How to use parameters in the query string?

I am writing a shell script which involves BigQuery commands to query an existing table and save the results to a destination table.
However, since my script will be run periodically, I have a parameter for the date for which the query should run.
For example, my script looks like this:
DATE_FORMATTED=$(date +%Y%m%d)
bq query --destination_table=Desttables.abc_$DATE_FORMATTED "select hits_eventInfo_eventLabel from TABLE_DATE_RANGE([mydata.table_],TIMESTAMP($DATE_FORMATTED),TIMESTAMP($DATE_FORMATTED)) where customDimensions_index = 4"
I get the following error:
Error in query string: Error processing job 'pro-cn:bqjob_r5437894379_1': FROM clause with table wildcards matches no table
How else can I pass the variable $DATE_FORMATTED to the TABLE_DATE_RANGE function from BigQuery in order to help execute my query?
Use double quotes "" + single quote ''. For example, in your case:
TIMESTAMP("'$DATE_FORMATTED'")
OR
select "'$variable'" as dummy from your_table
You are probably missing the single quotes around the $DATE_FORMATTED value inside the TIMESTAMP functions. Without the quotes it's going to be defaulting to the EPOCH time.
Try with:
TIMESTAMP('$DATE_FORMATTED'),TIMESTAMP('$DATE_FORMATTED')

Resources