I want to add some prefix to the field from output of hitting endpoint.
file.sh
result=$(curl -s <someEndpoint>)
echo $result
I have a cronjob to hit that endpoint regularly;
* * * * * /root/file.sh | jq .field > /root/file.txt 2>&1
I need to add "start" to the .field.
I created another file2.sh:
prefix="start"
val=$(cat /root/file.txt)
concat="${prefix}${val}"
echo $concat
the output looks like:
start"value from file.txt"
I want "start value from file.txt". how can I do that?
It seems like the better solution would be to use jq -r .field to output plaintext instead of a JSON string, but since you explicitly want to work with json, you can use jq again:
$ echo '"string"' | jq '"start" + .'
"startstring"
Related
I have a Jenkins freestyle job (yeah I know..) that sometimes needs to wait for another job with the same parameter to finish if it's running (or not run at all if it failed).
Using curl and the Jenkins API, is it possible to query a certain job and get the status of the last build where certainParam=certainValue?
(The reason I'm asking how to do that with curl is because it doesn't seem to be possible to do in freestyle job and the job can't be migrated to pipeilnes yet.. It seems like curl would be the east way..)
Thanks ahead!
So far I know there's no direct way to achieve it.
I wrote recursive script that search by the values on each build from the very last until match the information.
It prints every build url and the result of the query.
Dependencies
jq - install "jq.x86_64", Command-line JSON processor
Script
#!/bin/bash
user="admin"
pwd="11966e4dd8c33a730abf88e98fb952ebc3"
builds=$(curl -s --user $user:$pwd localhost:8080/job/test/api/json)
urls=$(echo $builds | jq '.builds[] | .url')
while read -r url
do
url=$(echo $url | sed -nr 's/"([^|]*)"/\1/p')
# get the build log
build=$(curl -s --user $user:$pwd "${url}api/json")
# transform the log in a simple structure
build=$(echo $build | jq '.result as $result | .actions[].parameters[]? | select(.name == "certainParam") | {(.name): .value, "result": $result}')
# check if the parameter value and the build result are the expected
result=$(echo $build | jq 'if .certainParam == "certainValue" and .result == "SUCCESS" then true else false end')
# print the result of each build
echo "url=${url}api/json;result=$result"
if [[ $result == true ]]
then
break
fi
done <<< $urls
Result
sh jenkins.sh
url=http://localhost:8080/job/test/12/api/json;result=false
url=http://localhost:8080/job/test/11/api/json;result=true
How do I escape the string to use a variable for the filename within the following script? Am I even getting the variable in a proper manner?
The scenario is I have a few dozen small flac files of voice messages. They need to be sent with the gcloud command/url individually and a return is sent and this return output is set as a variable. After I get the return response I need to send the same file and the return response to a database using the json file schema.
Edited: I have tried escaping by adding/wrapping in single quotes, double quotes and backticks. all return errors and all return errors show the url containing "$i" rather than a filename.flac.
When I try without escaping the string I get this error:
When I do it without escaping I get this:
add2.sh: line 4: syntax error near unexpected token `sudo'
add2.sh: line 4: ` sudo gcloud ml speech recognize `/var/www/html/library/422980-2560-WIN/$i --language-code='en-US' >STT.txt`'
Here is the script:
#!/bin/bash
cd /var/www/html/library/422980-2560-WIN
for i in *.flac;
sudo gcloud ml speech recognize `/var/www/html/library/422980-2560-WIN/"$i" --language-code='en-US' >STT.txt`
done
STT=`grep -Po '"transcript": *\K"[^"]*"' STT.txt | cut -d '"' -f2`
echo $STT
sudo gsutil cp /var/www/html/library/422980-2560-WIN/"$i".flac gs://422980
#rm -i -f -- /var/www/html/library/422980-2560-WIN/*.flac
sudo /usr/local/fuego --credentials /home/repeater/medialunaauth01-280236ff5e5f.json add 422980 '
{
"bucketObject": "https://storage.cloud.google.com/$i",
"fileDay": "filedaytest33",
"fileMonth": "filemonttest33",
"fileName": "filenametest33",
"fileTime": "filetimetest33",
"fileYear": "fileyeartest33",
"liveOnline": "0",
"qCChecked": "0",
"speechToText":"'"$STT"'",
"transcribedData": "",
}'
I'm trying to get the value for the bash binary call pg_isready to a bash variable
I've tried the following in my script:
#!/bin/bash
haspostgresdb = ${pg_isready -h "ipaddress"}
echo $haspostgresbd
haspostgresdb = ${pg_isready -h ipaddress}
echo $haspostgresbd
haspostgresdb = ${pg_isready -hipaddress}
echo $haspostgresbd
haspostgresdb = ${pg_isready -h"ipaddress"}
echo $haspostgresbd
All return bad substitution as the response. And I did some research and it looks like im doing it correctly
Any suggestions?
Use command substitution and get rid of blanks in the assign command:
haspostgresdb="$(pg_isready -h "ipaddress")"
I am trying to create a string with a query that will be save / send to another location, this string contains different variables.
The issue that I am having is that the echo of the variables are completely upside down and mix.
See code below:
tokenID=$(docker exec -ti $dockerContainerID /bin/sh -c "cat /tempdir/tokenfile.txt")
serverName="asdasd"
attQuery="$tokenID $serverName"
agentRegQuery="$./opt/mule/bin/amc_setup -H $attQuery"
echo TOKEN ID $tokenID
echo SERVER NAME $serverName
echo $attQuery
echo $agentRegQuery
Find below the output I am receiving:
TOKEN ID 29a6966f-fa0e-4f08-87eb-418722872d80---46407
SERVER NAME asdasd
asdasdf-fa0e-4f08-87eb-418722872d80---46407
asdasdmule/bin/amc_setup -H 29a6966f-fa0e-4f08-87eb-418722872d80---46407
There's a carriage return character at the end of the tokenID variable, probably because /tempdir/tokenfile.txt is in DOS/Windows format (lines end with carriage return+linefeed), not unix (lines end with just linefeed). When you print tokenID by itself, it looks ok, but if you print something else after that on the same line, it winds up overwriting the first part of the line. So when you print $attQuery, it prints this:
29a6966f-fa0e-4f08-87eb-418722872d80---46407[carriage return]
asdasd
...but with the second line printed on top of the first, so it comes out as:
asdasdf-fa0e-4f08-87eb-418722872d80---46407
The solution is to either convert the file to unix format (dos2unix will do this if you have it), or remove the carriage return in your script. You can do it like this:
tokenID=$(docker exec -ti $dockerContainerID /bin/sh -c "cat /tempdir/tokenfile.txt" | tr -d '\r')
I think everything works as it should
echo TOKEN ID $tokenID -> TOKEN ID 29a6966f-fa0e-4f08-87eb-418722872d80---46407
echo SERVER NAME $serverName -> SERVER NAME asdasd
echo $attQuery -> asdasdf-fa0e-4f08-87eb-418722872d80---46407
echo $agentRegQuery -> asdasdmule/bin/amc_setup -H 29a6966f-fa0e-4f08-87eb-418722872d80---46407
Why do you think something is wron here?
Best regards, Georg
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; }