How to get version from sbt-dynver on command line? - bash

In order to generate dynamically the version in my sbt project I am using the sbt-dynver plugin. But in order to integrate the build system, I would like to obtain the version string from a bash script, something like:
DYNVER=`sbt dynver`
But the previous command does not return anything.

I managed to get what I wanted by adding the 'show' command to sbt and parsing the output value, as follows:
VERSION=`sbt "show dynver" | grep -oE "\w{7}-\w{8}-\w{4}"`
echo $VERSION
4bbbb2a-20171022-1508

The simplest solution is to ignore other SBT output by taking last line.
Also it's better to print out version instead of dynver as it affects overrides from your version.sbt file.
VERSION=$(sbt 'print version' | tail -n 1)
echo $VERSION
0.1.7-2-6853afe4

Related

Bash: How to edit a file onto/into itself - without using a secondary file

Note: I am particularly looking for a coding hack, not for an alternative solution. I am aware that awk, sed etc. can do this inline edit just fine.
$ echo '1' > test
$ cat test > test
$ cat test
$
Is there a way, to somehow make the second command output the original contents (1 in this case)? Again, I am looking for a hack which will work without visibly having to use a secondary file (using a secondary file in the background is fine). Another question on this forum solely focused on alternative solutions which is not what I am looking for.
You can store the content in a shell variable rather than a file.
var=$(<test)
printf "%s\n" "$var" > test
Note that this might only work for text files, not binary files. If you need to deal with them you can use encoding/decoding commands in the pipeline.
You can't do it without storing the data somewhere. When you redirect output to a file, the shell truncates the file immediately. If you use a pipeline, the commands in the pipeline run concurrently with the shell, and it's unpredictable which will run first -- the shell truncating the file or the command that tries to read from it.
With thanks to the comment made by #Cyrus to the original question
$ sudo apt install moreutils
$ echo '1' > test
$ cat test | sponge test
$ cat test
1
It does require installing an extra package and pre-checking for the binary using something like where sponge to check if it is installed.
if you happen to use macos, if the file isn't too gargantuan, you can always follow these steps :
perform the edits
pipe it to the clipboard (or "pasteboard" in mac lingo)
paste it back to original file name
|
{... edits to file1 ...} | pbcopy; pbpaste > file1

How to extract only version from "go version" command using sed or other bash command in bash shell script running on Debian 10

The command go version currently prints go version go1.13.6 linux/amd64. I installed from the go website rather than Debian packages as the version is old. Therefore traditional ways to extract the version number like dpkg -s cannot be used.
I've explored sed commands to extract only the number (1.13.6) like this other question on this site which is similar I grant you, however after reading various sources online about whats possible with sed and my limited knowledge I've been unable to work out how to tell sed to find the starting point, yet alone make it future proof for new versions which may be slight alterations of this number format. I've tried to explore ways to say "find the 3rd to last number" so that I can then work backwards. Or, "find the 2nd word 'go'".
Current efforts have been purely theoretical, as I can't find where to begin, I've not included any attempts.
Can it be done?
$ v=`go version | { read _ _ v _; echo ${v#go}; }`
$ echo $v
1.13.6
Further reading:
Compound commands.
The read comand.
Parameter expansion.
Command substitution.

reading vm properties from a shell script XshowSettings

I am trying to capture the output of java -XshowSettings | grep file.encoding but it is not working. I am trying to read a property of java -XshowSettings from a Unix shell script. Normally, it is easy to ready a property by using e.g. printenv | grep JAVA_HOME but in case of java -XshowSetting, grep does not work.
So, I want something like this java -XshowSetting | grep file.encoding, but it does not work. Any idea?
This will solve your problem
java -XshowSettings 2>&1 | grep file.encoding
You need to combine the standard output and standard error to capture the results. Read more here.

date no such file or directory

I'm trying to script something that isn't outputting quite correctly with the date command. Here's the contents of what I have thus far:
#!/bin/bash
# Get RPM manifest
# Output written to /tmp
NOW=$(date +%D)
rpm -qa --qf="%{NAME}.%{ARCH}\n" | sort > /tmp/$HOSTNAME.RPM_Manifest.$NOW.txt
When I run this script, I get this message:
[root#linmachine1 ~]# sh /usr/local/bin/rpm_manifest.sh
/usr/local/bin/rpm_manifest.sh: line 7: /tmp/linmachine1.RPM_Manifest.03/01/17.txt: No such file or directory
I suspect the problem is in how the date formatting within the NOW variable I'm defining may be the culprit. I've tried with and without quotes and get the same thing. Looking at the man pages, I didn't see a way to change the default behavior such that the forward slashes would be replaced by dots, as I believe this is where the problem lies.
EDIT: Thanks for all of your responses. I'm not real sure why this was downvoted though. I asked a legitimate question. What gives?
Yes, you shouldn't have slashes in a file name.
Use:
now=$(date "+%d.%m.%Y")
rpm -qa --qf="%{NAME}.%{ARCH}\n" | sort > "/tmp/$HOSTNAME.RPM_Manifest.$now.txt"
instead, or replace the . with whatever you prefer

Access Perl environment (shell) variables in Windows - $ENV not working

The issue
I found out how to use windows environment variables (e.g., %AppData%, %HomePath%, %SystemRoot%, etc.) in this SO post:
Getting the path of %AppData% in perl script
Here is the code snippet that was chosen as a working, correct answer:
#!/usr/bin/perl
use warnings;
use strict;
my $localConfPath = $ENV{localappdata};
my $appdata = $ENV{appdata};
print $localConfPath; #will print the app path - C:\users\xxx\AppData\local
print $appdata; #prints - C:\users\xxx\AppData\Roaming
However, this is not working on my machine in my code for some reason. My scripts work without the shebang (#!) line so I tried the script both with and without it, to no avail.
My set-up
I'm using the Perl that comes with GitBash, if that makes a difference.
What I've tried
I tried a simple Perl command line execution:
perl -e 'print %ENV{AppData}';
This didn't work. I also tried the following alternatives:
perl -e 'print %ENV{APPDATA}';
perl -e 'print %ENV{appdata}';
That also didn't work. Here's the error I get (the same for all 3 versions):
syntax error at -e line 1, near "%ENV{AppData"
Execution of -e aborted due to compilation errors.
I even tried to use the code from the SO post I mentioned in it's own file. That code doesn't work either. With the code from the post I get this error:
$ perl /c/Users/User1/Desktop/ehCode_testingWindowsEnvironmentVariables_01.pl
Use of uninitialized value in print at /c/Users/User1/Desktop/ehCode_testingWindowsEnvironmentVariables_01.pl line 7.
Use of uninitialized value in print at /c/Users/User1/Desktop/ehCode_testingWindowsEnvironmentVariables_01.pl line 8.
The lines in question then are these:
print $localConfPath; #will print the app path - C:\users\xxx\AppData\local
print $appdata; #prints - C:\users\xxx\AppData\Roaming
I don't see why they shouldn't work.
I've checked Perl Monks, Perl Maven, Stack Overflow, and other popular Perl resources, to no avail. Even Active State did not have the answer.
When you access individual items of a hash, you need to use the scalar sigil, $ as opposed to the hash sigil, %:
perl -e 'print $ENV{APPDATA}'

Resources