Set VSTS output variable to be result from bash command - azure-devops

I'm running a task in VSTS which performs some operations on a variable from a previous step and I then need to output the result to be used in future tasks. I have the following in a command line task running on a linux build host but am having no luck when trying to use the result later with $(podName3).
COMMAND="$(echo '$(pods)' | grep -oh -P '[^ ]*' | grep schema)"
##vso[task.setvariable variable=podName3]"$COMMAND"
I have tried several variations on this to no avail and need some direction as this has stumped me for too long now

Seems the syntax is incorrect.
Just try to below format:
COMMAND="$(echo '$pods' | grep -oh -P '[^ ]*' | grep schema)"
echo "##vso[task.setvariable variable=podName3]$COMMAND"
Or add a powershell task and run below commands to set the variable:
$COMMAND="$(echo '$env:pods' | grep -oh -P '[^ ]*' | grep schema)"
Write-Host "##vso[task.setvariable variable=podName3]$COMMAND"
More information please see Define and modify your variables in a script

I created a command line tool & an Azure DevOps task for this: https://marketplace.visualstudio.com/items?itemName=riezebosch.setvar
It just lets you pipe the output of a command into the tool and output it as the magic variable string. It's written in Go and cross compiled so works on all major platforms and all different shells.
Your example:
echo '$pods' | grep -oh -P '[^ ]*' | grep schema | setvar -name podName3
You only need to include the setvar task prior to this script task in order to get the tool on the agent.

Adding a PSA to this post, looks like they changed the way the variables are accessed now - you have to access variables like this: $(variable)
COMMAND=$(echo '$pods' | grep -oh -P '[^ ]*' | grep schema)
echo "##vso[task.setvariable variable=podName3]$(COMMAND)"

Related

Check number of active meetings in Big Blue Button from command line

I want to check how many active meetings there are on the BBB server at any one time from the command line. I have tried
$ bbb-conf --network
but not getting anywhere. I have also checked the number of active connections to port 80 and 443
$ netstat -anp | grep :443 | grep ESTABLISHED | wc -l
but I'm not sure if I can trust that figure.
I know I can use the isMeetingRunning call from the API but I'm just looking for command line.
Any ideas would be appreciated
The following bash script, which can be run from command line on the same machine as the BigBlueButton server, will process the response to the BBB API getMeetings call.
#!/bin/bash
APICallName="getMeetings"
APIQueryString=""
X=$( bbb-conf --secret | fgrep URL: )
APIEndPoint=${X##* }
Y=$( bbb-conf --secret | fgrep Secret: )
Secret=${Y##* }
S=$APICallName$APIQueryString$Secret
Checksum=$( echo -n $S | sha1sum | cut -f 1 -d ' ' )
if [[ "$APIQueryString" == "" ]]
then
URL="${APIEndPoint}api/$APICallName?checksum=$Checksum"
else
URL="${APIEndPoint}api/$APICallName?$APIQueryString&checksum=$Checksum"
fi
wget -q -O - "$URL" | grep -o '<meetingID>' | wc -w
Tested on a live BBB machine.
Note:
The APICallName and APIQueryString can be modified to provide interface to other BBB API calls. See https://docs.bigbluebutton.org/dev/api.html
The command-line sha1sum will output a different result if a newline is appended to its input. This is the reason echo -n is used instead of echo.
In the last line, the script processes the XML output from the API call in a very naïve way, simply counting the number of occurences of the <meetingID> tag. More elaborate processing would probably require parsing the XML.

How to pipe into grep using backticks in Linux shell script?

I am trying to execute, what I thought would be, a simple shell command within a script. When I execute this from the command prompt, it works well:
$ sudo cat /etc/sysconfig/network-scripts/ifcfg-en0 | grep "IPADDR"
IPADDR=192.168.1.10
However, if I put this into a shell script:
#!/usr/bin/sh
my_command=`sudo cat /etc/sysconfig/network-scripts/ifcfg-en0 | grep "IPADDR"`
${my_command}
echo $?
I get this error:
$ sudo ./myscript.sh
./myscript.sh: line 3: IPADDR=192.168.1.10: command not found
So, how can I successfully execute this line within my shell script?
Thanks!
The problem in your case is that you are executing the result of the command...
This line executes the code as it's between "``" that are special characters for executing the given string as a command:
my_command=`sudo cat /etc/sysconfig/network-scripts/ifcfg-en0 | grep "IPADDR"`
as a result, $my_command is "IPADDR=192.168.1.10"
Then you are trying to execute it for the second time:
${my_command}
Thats why you are getting this error. There is no such a command as "IPADDR=192.168.1.10".
Just use $my_command as a result that contains your desired grepped part and skip the ${my_command} line:
#!/usr/bin/sh
my_command=`sudo cat /etc/sysconfig/network-scripts/ifcfg-en0 | grep "IPADDR"`
echo $my_command

using here document and pipeline of sh at the same time

I'm using here document of sh to run some commands. now I want to parse the output of those commands using awk. However, everytime I execute it, I get the output of the command append with something like this "% No such child process"
This is how my script looks like.
#!/bin/sh
com = "sudo -u username /path/of/file -l"
$com <<EOF | awk '{print $0}'
Commands.
.
.
.
EOF
How am I going to use heredoc and pipeline without appending that unwanted string?
Thanks
Your variable assignment is wrong in a couple of ways. First, you aren't actually assigning a variable; you're trying to run a command named com whose arguments are = and a string "sudo ...". Spaces must not be used on either side of the =:
com="sudo ..."
Second, command lines should not be stored in a variable; the shell's parser can only make that work they way you intend for very simple commands. Type the command out in full, or use a shell function.
com () {
sudo -u username /path/to/file -l
}
com <<EOF | awk '{print $0}'
...
EOF
There's no problem, check :
$ cat <<EOF | awk '{print $1}'
a b c
1 2 3
EOF
a
1

Simultaneous pipe to grep and redirect to stdout

In Linux bash, I am trying to run a command and grep for an argument:
command | grep
However, I need to redirect the result of the commad to the stdout and simultaneously pipe it to grep (I need to see both the grep result and the command result in stdout).
I googled a bit and tried some variations, such as:
command | tee /dev/tty | grep
But, no luck.
I don't want to use sth like
command
command | grep
as it is ugly :)
Thanks,
Try
command | tee >(grep whatever)
Note that there's no space between these two symbols: >(.

works on the command line but not in a shell script

I have this line
samtools view -h file | awk '$3=="chr$a" || /^#/' | samtools view -S - -b -o output
the dash between the -S and the -b is supposed to indicate to the program that it is from STDIN. I can run it from a perl script on the command line but as soon as I try to move it into a shell script it just creates the file without outputting any data. Any ideas would be greatly appreciated.
In a shell script the $a inside single quotes will not be expanded:
for a in {1..22} do
samtools view -h AD3.sorted.bam | awk '$3=="chr$a" || /^#/' | samtools view -S - -b -o chr$a.bam
done
If you haven't already, have a look at the samtools FAQ. This has examples for doing similar things to what you want to do with your pipeline.
Its been a while since I used samtools, but I would've written your command like this:
samtools view -h file | awk '$3=="chr$a" || /^#/' | samtools view -S -b - > output.bam
Also you mentioned you had moved the command to a shell script. Is the shell script doing anything else? If it still doesn't work, I would post that for us to look at.