When I try to pass value directly in pattern /2020-04-01,ARS,AED/ is working but when I try to pass through variables I'm not getting the output
$temp1:=ExecutionDate;
$temp2:=Currency;
$temp3:=ConvertToCurrencyISO;
$response_text:="2020-04-01,ARS,AED,.0570575091\n2020-04-01,ARS,AUD,.0252416065"
$response_index:=$match($string($response_text),/$temp1,$temp2,$temp3/).index;
You can use the $eval function to dynamically create a regex. E.g.:
(
$temp1:=ExecutionDate;
$temp2:=Currency;
$temp3:=ConvertToCurrencyISO;
$regex := $eval('/' & $temp1 & ',' & $temp2 & ',' & $temp3 & '/');
$response_text:="2020-04-01,ARS,AED,.0570575091\n2020-04-01,ARS,AUD,.0252416065";
$response_index:=$match($response_text,$regex);
)
See https://try.jsonata.org/hrE61rzJt
Related
I am trying to debug a bash shell script where I am trying to surround a string/variable with single quotes. I am seeing the following results and am stumped on how to debug this. It obviously has something to do with the content of the variable. I thought the variable may be an array hence some of the echo statements. IN_JSON is being constructed via calls to "jq" to construct some JSON.
echo "IN_JSON = ${IN_JSON}"
echo "IN_JSON = ${IN_JSON[*]}"
echo "IN_JSON = '${IN_JSON[*]}'"
echo "IN_JSON = '" ${IN_JSON} "'"
echo "${#IN_JSON[#]}"
Output:
IN_JSON = {"name":"RX-CLAIM-FILLED"}
IN_JSON = {"name":"RX-CLAIM-FILLED"}
'N_JSON = '{"name":"RX-CLAIM-FILLED"}
'_JSON = ' {"name":"RX-CLAIM-FILLED"}
1
What's going on here and how do I troubleshoot this? It obviously has something to do with the contents of IN_JSON, but I'm not sure why or what is going on here.
The expansion of ${IN_JSON[*]} contains a carriage return character that resets the position of the cursor to beginning of the line, so that the next character ' is printed on beginning of the line.
Most probably, you want to run your file via dos2unix.
I have the following bash script for sending an email reports. I am trying to get it working correctly with mail command on bash and want to use variable substitution conditional which requires to use eval.
#!/bin/bash
LOG="/tmp/error.txt"
echo "test" > $LOG
INCLUDED="aaaaaaaaaaaaaaaaa"
EXCLUDED="bbbbbbbbbbbbbbbbb"
STR=INCLUDED
BODY="
Email Body:
\${!STR}"
#Set STR based on some condition
STR=EXCLUDED # set conditional with if statement
SUB="email subject"
PARAMS="-r \" Sender <ret#address.com>\""
PARAMS+=" -s \"$SUB\""
PARAMS+=" -A $LOG" # use conditional with if statement
eval echo \"MESSAGE: "${BODY}"\" | mail $(eval echo ${PARAMS}) "user#email.com"
While running the below code, I have to pass $PARAMS output without the quotes around the string as input to mail command, which does not recognise the diff options. The closest I can get to is using calling $(eval echo $PARAMS), but this garbels the subject since it remove the double quotes around the subject.
+ LOG=/tmp/error.txt
+ echo test
+ INCLUDED=aaaaaaaaaaaaaaaaa
+ EXCLUDED=bbbbbbbbbbbbbbbbb
+ STR=INCLUDED
+ BODY='
Email Body:
${!STR}'
+ STR=EXCLUDED
+ SUB='email subject'
+ PARAMS='-r " Sender <ret#address.com>"'
+ PARAMS+=' -s "email subject"'
+ PARAMS+=' -A /tmp/error.txt'
+ eval echo '"MESSAGE:' '
Email Body:
${!STR}"'
++ echo 'MESSAGE:
Email Body:
bbbbbbbbbbbbbbbbb'
++ eval echo -r '"' Sender '<ret#address.com>"' -s '$SUB' -A /tmp/error.txt
+++ echo -r ' Sender <ret#address.com>' -s email subject -A /tmp/error.txt
+ mail -r Sender '<ret#address.com>' -s email subject -A /tmp/error.txt user#email.com
How do I pass the $PARAMS to the mail command with passing the subject (-s) as quoted string ?
Use an array instead and put each string in an array element.
params=('-r')
params=("${params[#]}" ' Sender <ret#address.com>')
mail "${params[#]}"
Or simpler
params=('-r')
params+=(' Sender <ret#address.com>')
mail "${params[#]}"
See here: Bash: add value to array without specifying a key
Update, because I think you lack some fundamental shell programming knowledge.
Don't use eval! If you need to delay an evaluation, write a function, which performs the evaluation, when you call it.
Use single quotes for static strings and double quotes for strings containing variables.
Use arrays, if you need arrays.
Group commands in curly braces, if you need to group commands.
This is you example updated according to the rules above.
LOG=/tmp/error.txt
echo 'test' > $LOG
INCLUDED='aaaaaaaaaaaaaaaaa'
EXCLUDED='bbbbbbbbbbbbbbbbb'
STR=INCLUDED
body () { echo "
Email Body:
$STR"
}
#Set STR based on some condition
STR=EXCLUDED # set conditional with if statement
SUB='email subject'
PARAMS=('-r' 'Sender <ret#address.com>')
PARAMS+=('-s' "$SUB")
PARAMS+=('-A' "$LOG") # use conditional with if statement
{ echo -n 'MESSAGE: '; body; } | mail "${PARAMS[#]}" 'user#email.com'
You can test your code by putting a mail mock-up at the beginning of the program.
mail() { echo mail "$#"; cat; }
how do you pass a global variable into a string inside a function?
I have the following code that works for the most part:
td_query () { bteq << EOF |grep '^>' |sed -e "s/^>/;/g"
$(cat $HOME/.tdlogon)
DATABASE $schemaName;
.set width 10000;
.set titledashes off;
$1
.LOGOFF;
.QUIT;
.EXIT
EOF
}
rqstID="1357"
echo $(td_query "select '>'||'UPDATE schema.SEGN_$rqstID_PRCSS_TBL SET POPN_LVL_EXCLN ='||a.CODE_ID||' WHERE ' || b.SQL_FILE_NM ||' AND POPN_LVL_EXCLN IS NULL'
FROM SE_POPN_EXCLSN a
INNER JOIN SE_CODE_LIB b
ON
a.CODE_ID = b.CODE_ID;")
but the results come back:
UPDATE schema.SEGN_ SET POPN_LVL_EXCLN = 1002 WHERE MR_IND = 'Y'
missing this:
$rqstID_PRCSS_TBL
it should be:
UPDATE schema.SEGN_1357_PRCSS_TBL SET POPN_LVL_EXCLN = 1002 WHERE MR_IND = 'Y'
_ is a legal character in a shell variable. The shell is trying to find a variable by the name of $rqstID_PRCSS_TBL and getting an empty string. (That's why _PRCSS_TBL is disappearing from your output.)
You need to tell the shell where the variable name ends: schema.SEGN_${rqstID}_PRCSS_TBL
I have a string which I am passing into the command, the string contains multiple codes which are split up with a comma. When I pass this string through to the command line, it basically stops displaying text after the comma.
domainCodeList = "101, 102, 103, 104, 105"
'Now that we have populated the string with the possible domain codes, we now execute the batch file
strBatchName = SystemData.AppPath + "DetailedContacts.bat" & " " & domainCodeList
Shell strBatchName
When this is ran, the output I get in the command like is:
The code for my batch file is:
#echo off
echo %1
Any help on how I can pass commas to the command line would be great! Thanks
pause
strBatchName = SystemData.AppPath + "DetailedContacts.bat" & " " & """" & domainCodeList & """"
Or
strBatchName = SystemData.AppPath + "DetailedContacts.bat" & " " & Chr(34) & domainCodeList & Chr(34)
Parameters with spaces or special characters in it need to be quoted.
From your batch file use %1 to retrieve the argument with quotes or use %~1 to retrieve the argument without quotes.
I am using AutoHotKey to build a simple GUI tool that uses the Sysinternals tool PSLoggedOn. First if I use
run psloggedon.exe -l -x \\computername I don't get any output at all.
So I tried run %ComSpec% /C psloggedon.exe -l -x 1> %Temp%\psloggedon.txt and that gives me the output in domain\username format for each user logged in.
Like my first example I would rather just run psloggedon and get the output instead of first opening a command prompt; is that possible somehow?
Either way I want to take the output and avoid writing it to a file, but instead edit the output from the output stream to remove the "domain\" part and then just return the username. How can this be done without any third-party software?
This answer to another question on Serverfault shows a way to do it by reading the StdoutRead.
I too was looking at a way to avoid using a file but have decided it's actually the simplest option.
Code copied from user60738
#include <Constants.au3>
Dim $line, $line2, $file, $icount, $reg, $reg2, $reg3
$file = FileOpen("pclist.txt", 0)
While 1
$line = FileReadLine($file)
If #error Then ExitLoop
$reg3 = ""
$reg2 = ""
$reg = ""
If Ping($line, 200) Then
$reg = #ComSpec & ' /C "' & #ScriptDir & "\pstools\psloggedon.exe -l -x \\" & $line & '"'
$reg = Run($reg, "", #SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
While 1
$reg2 = StdoutRead($reg)
If #error Then ExitLoop
If $reg2 Then
$reg3 &= $reg2
EndIf
WEnd
If StringInStr($reg3, "Error") Then
$reg = "Error"
ElseIf StringInStr($reg3,"No one is logged") Then
$reg = "No one is logged on locally."
Else
$reg = StringTrimLeft($reg3, StringInStr($reg3, Chr(13), "", 3))
$reg = StringTrimLeft($reg, StringInStr($reg, "\"))
$reg = StringTrimRight($reg, StringLen($reg) - StringInStr($reg, Chr(13)))
$reg = StringStripWS($reg, 8)
EndIf
$icount += 1
$line2 &= $icount & #TAB & $line & #TAB & $reg & #CRLF
EndIf
TrayTip("Psloggedon", $icount & " " & $line & " User: " & $reg, 10)
WEnd
FileClose($file)
ConsoleWrite($line2)
FileDelete("C:\output.txt")
FileWrite("C:\output.txt", $line2)
ShellExecute("C:\output.txt")