Storing query result in a variable with bash script - linux

I have a query say :
"select .... " > $my_result
This will store in the my_result but as a file. How to store in a my_result variable itself if that query gives only one row without creating a file.

Simplest way would be:
my_result=$(select ...)
And when you want to print it:
echo $my_result

Related

Update multiple column with bind variable using cx_Oracle.executemany()

I have been trying to update some columns of a database table using cx_Oracle in Python. I created a list named check_to_process which is a result of another sql query. I am creating log_msg in the program based on success or failure and want to update same in the table only for records in check_to_process list. When I update the table without using bind variable <MESSAGE = %s>, it works fine. But when I try to use bind variable to update the columns it gives me error :
cursor.executemany("UPDATE apps.SLCAP_CITI_PAYMENT_BATCH SET MESSAGE = %s, "
TypeError: an integer is required (got type str)
Below is the code, I am using:
import cx_Oracle
connection = cx_Oracle.connect(user=os.environ['ORA_DB_USR'], password=os.environ['ORA_DB_PWD'], dsn=os.environ['ORA_DSN'])
cursor = connection.cursor()
check_to_process = ['ACHRMUS-20-OCT-2021 00:12:57', 'ACHRMUS-12-OCT-2021 16:12:01']
placeholders = ','.join(":x%d" % i for i,_ in enumerate(check_to_process))
log_msg = 'Success'
cursor.executemany("UPDATE apps.SLCAP_CITI_PAYMENT_BATCH SET MESSAGE = %s, "
"PAYMENT_FILE_CREATED_FLAG='N' "
"WHERE PAYMENT_BATCH_NAME = :1",
[(i,) for i in check_to_process], log_msg, arraydmlrowcounts=True)
Many thanks for suggestions and insights!
Your code has an odd mix of string substitution (the %s) and bind variable placeholders (the :1). And odd code that creates bind variable placeholders that aren't used. Passing log_msg the way you do isn't going to work, since executemany() syntax doesn't support string substitution.
You probably want to use some kind of IN list, as shown in the cx_Oracle documentation Binding Multiple Values to a SQL WHERE IN Clause. Various solutions are shown there, depending on the number of values and frequency that the statement will be re-executed.
Use only bind variables. You should be able to use execute() instead of executemany(). Effectively you would do:
cursor.execute("""UPDATE apps.SLCAP_CITI_PAYMENT_BATCH
SET MESSAGE = :1
WHERE PAYMENT_BATCH_NAME IN (something goes here - see the doc)""",
bind_values)
The bottom line is: read the documentation and review examples like batch_errors.py. If you still have problems, refine your question, correct it, and add more detail.

Struct name from variable in Matlab

I have created a structure containing a few different fields. The fields contain data from a number of different subjects/participants.
At the beginning of the script I prompt the user to enter the "Subject number" like so:
prompt='Enter the subject number in the format SUB_n: ';
SUB=input(prompt,'s');
Example SUB_34 for the 34th subject.
I want to then name my structure such that it contains this string... i.e. I want the name of my structure to be SUB_34, e.g. SUB_34.field1. But I don't know how to do this.
I know that you can assign strings to a specific field name for example for structure S if I want field1 to be called z then
S=struct;
field1='z';
S.(field1);
works but it does not work for the structure name.
Can anyone help?
Thanks
Rather than creating structures named SUB_34 I would strongly recommend just using an array of structures instead and having the user simply input the subject number.
number = input('Subject Number')
S(number) = data_struct
Then you could simply find it again using:
subject = S(number);
If you really insist on it, you could use the method proposed in the comment by #Sembei using eval to get the struct. You really should not do this though
S = eval([SUB, ';']);
Or to set the structure
eval([SUB, ' = mydata;']);
One (of many) reasons not to do this is that I could enter the following at your prompt:
>> prompt = 'Enter the subject number in the format SUB_n: ';
>> SUB = input(prompt, 's');
>> eval([SUB, ' = mydata;']);
And I enter:
clear all; SUB_34
This would have the unforeseen consequence that it would remove all of your data since eval evaluates the input string as a command. Using eval on user input assumes that the user is never going to ever write something malformed or malicious, accidentally or otherwise.

Evaluate variable value from string dynamically

We need to get the value of dynamically constructed variables.
What I mean is we have a variable loaded from a property file called data8967677878788node. So when we run echo $data8967677878788node we get the output test.
Now in data8967677878788node the number part 8967677878788 needs to be dynamic. That means there could be variables like
data1234node
data346346367node
and such.
The number is an input argument to the script. So we need something like this to work
TESTVAR="data`echo $DATANUMBER`node"
echo $$TESTVAR #This line gives the value "test"
Any idea on how this can be accomplished
You can use BASH's indirect variable expansion:
data346346367node='test'
myfunc() {
datanumber="$1"
var1="data${datanumber}node"
echo "${!var1}"
}
And call it as:
myfunc 346346367
Output:
test
Your code is actually already pretty close to working, it just needs to be modified slightly:
TESTVAR="data`echo $DATANUMBER`node"
echo ${!TESTVAR}
If $DATANUMBER has the value 12345 and $data12345node has the value test then the above snippet will output test.
Source: http://wiki.bash-hackers.org/syntax/pe#indirection

Condition IF In Power Query's Advanced Editor

I have a field named field, and I would like to see if it is null, but I get an error in the query, my code is this:
let
Condition= Excel.CurrentWorkbook(){[Name="test_table"]}[Content],
field= Condition{0}[fieldColumn],
query1="select * from students",
if field <> null then query1=query1 & " where id = '"& field &"',
exec= Oracle.Database("TESTING",[Query=query1])
in
exec
but I get an error in the condition, do you identify the mistake?
I got Expression.SyntaxError: Token Identifier expected.
You need to assign the if line to a variable. Each M line needs to start with an assignment:
let
Condition= Excel.CurrentWorkbook(){[Name="test_table"]}[Content],
field= Condition{0}[fieldColumn],
query1="select * from students",
query2 = if field <> null then query1 & " some stuff" else " some other stuff",
exec= Oracle.Database("TESTING",[Query=query2])
in
exec
In query2 you can build the select statement. I simplified it, because you also have conflicts with the double quotes.
I think you're looking for:
if Not IsNull(field) then ....
Some data types you may have to check using IsEmpty() or 'field is Not Nothing' too. Depending on the datatype and what you are using.
To troubleshoot, it's best to try to set a breakpoint and locate where the error is happening and watch the variable to prevent against that specific value.
To meet this requirement, I would build a fresh Query using the PQ UI to select the students table/view from Oracle, and then use the UI to Filter the [id] column on any value.
Then in the advanced editor I would edit the generated FilteredRows line using code from your Condition + field steps, e.g.
FilteredRows = Table.SelectRows(TESTING_students, each [id] = Excel.CurrentWorkbook(){[Name="test_table"]}{0}[fieldColumn])
This is a minor change from a generated script, rather than trying to write the whole thing from scratch.

AutoIT, Using Variable in DirCopy Paths

I am attempting to use a Local variable in a DirCopy command but when I insert the $Variable into the C:\Users\store$Variable\Desktop path it attempts to read the path literally instead of using the $Variable.
The objective is to create a prompt for the Store number and insert that number into a bunch of DirCopy lines to ensure we get the profiles that contain only that number. The issue is that the profiles are one word, ex. store123, reciever123.
This is what I put together so far but I can't get it to take the variable in this way.
Local $STORE = InputBox ( "Store Number" , "What Store is This?" )
DirCopy ( "\\192.168.1.3\C$\Documents and Settings\store$STORE\Desktop" , "C:\Users\Store$STORE\desktop" )
DirCopy ( "\\192.168.1.3\c$\Documents and Settings\Profile$STORE\Desktop" , "C:\Users\Profile$STORE\Desktop")
Is there a formatting issue? or is this not possible in AutoIT?
Method 1: Concatenation
In order to use Variables inside strings, you need to concatenate them, by using the & operator:
$nVar = 42
$sStr = "Hello, " & $nVar & "World!"
; $sStr will now hold: "Hello, 42World!"
Method 2: Expansion
However, there is a Opt() flag ExpandVarStrings that enables inline variable use:
Opt("ExpandVarStrings", 1) ;0=don't expand, 1=do expand
$nVar = 42
$sStr = "Hello, $nVar$World!"
; $sStr contains: "Hello, 42World!"

Resources