Get golang version from compiled binary - go

Is there a way to get golang version from pkg/ or from compiled binary?
I want to automate removal of $GOPATH/pkg folder only when I change the golang version.

Nevermind, found the answer
[ `strings $pkg_a_file | grep 'go object' | head -n 1 | cut -f 5 -d ' '` != `go version | cut -f 3 -d ' '` ] && \
rm -rf $GOPATH/pkg
strings $pkg_a_file | grep 'go object' | head -n 1 | cut -f 5 -d ' ' part will show something like go1.6.2
pkg_a_file can be something like this:
PKG_OS_ARCH=`go version | cut -d ' ' -f 4 | tr '/' '_'`
pkg_a_file=$GOPATH/pkg/$PKG_OS_ARCH/gitlab.com/kokizzu/gokil/A.a
External reference: http://kokizzu.blogspot.co.id/2016/06/solution-for-golang-slow-compile.html

With 1.9.2, the grep command is
strings $pkg_a_file | grep 'Go cmd/compile'

I found the following method, to check the version of Go lib used to compile the binary
$ strings compiled_binary | grep '^go1'
go1.17.4

Related

Get latest version of Maven with curl

My task is to grab latest version of maven in bash script using curl https://apache.osuosl.org/maven/maven-3/
Output should be: 3.8.5
curl -s "https://apache.osuosl.org/maven/maven-3/" | grep -o "[0-9].[0-9].[0-9]" | tail -1
works for me ty
One way is to
Use grep to only get the lines containing the folder link
Use sed with an regex to get only the version number
Use sort to sort the lines
Use tail -n1 to get the last line
curl -s https://apache.osuosl.org/maven/maven-3/ \
| grep folder \
| gsed -E 's/.*([[:digit:]]\.[[:digit:]]\.[[:digit:]]).*/\1/' \
| sort \
| tail -n1
Output:
3.8.5
The url you're mentioning is an HTML-document and curl is not an HTML-parser, so I'd look into an HTML-parser, like xidel, if I were you:
$ xidel -s "https://apache.osuosl.org/maven/maven-3/" -e '//pre/a[last()]'
3.8.5/
$ xidel -s "https://apache.osuosl.org/maven/maven-3/" -e 'substring-before(//pre/a[last()],"/")'
3.8.5
I would suggest to use the central repository instead one of the apache maven sites because the information on central more maded for machine consumption.
It will be taken into account that the file maven-metadata.xml contains the appropriate information.
There are two options using the <version>..</version> tag and the <latest>..</latest> tag.
MVNVERSION=$(curl https://repo1.maven.org/maven2/org/apache/maven/maven/maven-metadata.xml \
| grep "<version>.*</version>" \
| tr -s " " \
| cut -d ">" -f2 \
| cut -d "<" -f1 \
| sort -V -r \
| head -1)
The second option:
MVNVERSION=$(curl https://repo1.maven.org/maven2/org/apache/maven/maven/maven-metadata.xml \
| grep "<latest>.*</latest>" \
| cut -d ">" -f2 \
| cut -d "<" -f1)

getting "grep: write error: pipe broken" when command executed through application installation

Below is the command which is executed when I install the application(this line written in one of the script of our application).
PASS=strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 10 | tr -d '\n'
every time I get the error "grep: write error: pipe broken".
here are few points to be noted
When I install the application on RHEL 7.X. It runs without an issue.
When I run the command directory on RHEL 8.X. it doesn't give an error.
It throws an error only when installing the application on RHEL 8.x.
Also, I have tried few other ways to generate alphanumaric character like:
X=strings /dev/urandom | grep -o -m15 '[[:alnum:]]'
PASS=echo "$X" | head -n 10 | tr -d '\n'
PASS=strings /dev/urandom | tr -dc A-Za-z0-9 | head -c10
PASS=cat /dev/urandom | tr -dc A-Za-z0-9 | head -c10
X=strings /dev/urandom | head -n 100
PASS=echo "X" | grep -o '[[:alnum:]]' | head -n 10 | tr -d '\n'
PASS=< /dev/urandom tr -dc '[[:alnum:]]' | head -c10
None of this worked on RHEL 8.X while installing application. However all this command works fine when executing directly on terminal.
The command
PASS=`strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 10 | tr -d '\n'`
does not work due to a bug in strings. head exits after having read 10 lines; grep detects that the other end of its output pipe has been closed, issues grep: write error: Broken pipe and also exits; strings ignores that the other end of its output pipe has been closed and blindly continues endlessly.
The commands 3. and 5. which don't use strings correctly generate a 10 character password, although 3. also issues write error: Broken pipe.

How to extract string of command result and use it in a loop

Running a nx affected:apps command gives me this output:
> NX NOTE Affected criteria defaulted to --base=master --head=HEAD
> NX Affected apps:
- app-backend
- app-frontend
- app-something
- app-anything
I need to get all the application names and use them again for a command call.
So I started with that
output=$(nx affected:apps)
echo "$output" | grep -E "^\W+app-(\w+)"
This gives me
- app-backend
- app-frontend
- app-something
- app-anything
But I need to get the names only instead to run foo --name={appname} four times.
Also not quite sure how to use it in a loop. Quite new to bash scripting :-(
You may use -o (show matches only) with -P (perl regex moode) in gnu-grep:
nx affected:apps |
grep -oP "^\W+app-\K\w+" |
xargs -I {} docker build -t {} .
If gnu-grep isn't available then use this awk command:
nx affected:apps |
awk -F- '/app-/{print $3}' |
xargs -I {} docker build -t {} .
I don't have nx command here but you can try using xargs:
nx affected:apps | grep '^ -' | cut -d' ' -f4 | xargs -I{} echo docker build -t {} ./dist/{}
Remove echo to actually run the command.
You can use the --plain option:
nx affected:apps --plain
the command should return all the affected apps with space as a divider. You can then store those to a bash array and cycle through them in a for loop, running the command you need:
#!/bin/bash
AFFECTED=($(./node_modules/.bin/nx affected:apps --plain))
for t in ${AFFECTED[#]}; do
echo $t
done

Is it possible to comment inline in a multi-line newline escaped script?

When I have long multi-line piped commands in my scripts I would like to comment on what each line does, but I haven't found a way of doing so.
Given this snippet:
git branch -r --merged \
| grep " $remote" \
| egrep -v "HEAD ->" \
| util.esed -n 's/ \w*\/(.*)/\1/p' \
| egrep -v \
"$(skipped $skip | util.esed -e 's/,/|/g' -e 's/(\w+)/^\1$/g' )" \
| paste -s
Is it possible to insert comments in between the lines? It seems that using the backslash to escape the newline prevents me from adding comments at the end of the line, and I can't add the comment before the backslash, as that would hide the escaping.
Pseudo-code of what I would like the above script to look like
It seems I was unclear (?) of what I wanted by the above section, so to have a clue on what I am looking for, it should be in the similar vein of this:
git branch -r --merged \ # list merged remote branches
| grep " $remote" \ # filter out the ones for $remote
| egrep -v "HEAD ->" \ # remove some garbage
#strip some whitespace:
| util.esed -n 's/ \w*\/(.*)/\1/p' \
# remove the skipped branches:
| egrep -v \
"$(skipped $skip | util.esed -e 's/,/|/g' -e 's/(\w+)/^\1$/g' )" \
| paste -s # something else
It doesn't have to be exactly like this (obviously, it's not valid syntax), but something similar. If it's not possible directly, due to syntactical restrictions, perhaps it's possible to write self-modifying code that will have comments that are removed before executing it?
You can try something like that:
git branch --remote | # some comment
grep origin | # another comment
tr a-z A-Z

curl complex usage with pattern

I'm trying to get 2 files using curl based on some pattern but that doesn't seem to work:
Files:
SystemOut_15.04.01_21.12.36.log
SystemOut_15.04.01_15.54.05.log
curl -f -k -u "login:password" https://myserver/cgi-bin/logviewer/index.cgi?getlogfile=SystemOut_15.04.01_21.12.36.log'&'server=qwerty123.com'&'numlines=100000000'&'appenv=MBL%20-%20PROD'&'directory=/app/WAS/was85/profiles/node/logs/mbl-server1
I know there is -A key but it doesn't work since my file is inside the link.
How can I extract those 2 files using a pattern?
Did that myself. One curl gets the list of logs on the webpage. Another downloads those files.
The code looks like:
for file in $(curl -f -k -u "user:pwd" https://selfservice.pwj.com/cgi-bin/logviewer/index.cgi?listdirectory=/app/smx_client_mob/data/log'&'appenv=MBL%20-%20PROD'&'server=xshembl04pap.she.pwj.com | grep href | sed 's/.*href="//' | sed 's/".*//' | sed 's/javascript:getLog//g' | sed "s/['();]//g" | grep -i 'service' | grep '^[a-zA-Z].*'); do
curl -o $file -f -k -u "user:pwd" https://selfservice.pwj.com/cgi-bin/logviewer/index.cgi?getlogfile="$file"'&'server=xshembl04pap.she.pwj.com'&'numlines=100000000'&'appenv=MBL%20-%20PROD'&'directory=/app/smx_client_mob/data/log; done

Resources