How to use local jdk in SDKMAN! script - bash

My script changes to a jdk installed with sdkman but not to a local one. How can I change into my local oracle 8 v261 jdk in a script with sdkman?
The script
#!/bin/bash
. /usr/local/sdkman/bin/sdkman-init.sh
sdk ls java
for i in {"8_261-oracle", "9.0.4-open"}
do
sdk u java $i
done
gives as output
[...]
| | 9.0.4 | open | installed | 9.0.4-open
| >>> | 8.0.265 | open | installed | 8.0.265-open
| | 8.0.232 | open | local only | 8.0.232-open
[...]
Unclassified | | 8_261 | none | local only | 8_261-oracle
================================================================================
Use the Identifier for installation:
$ sdk install java 11.0.3.hs-adpt
================================================================================
Stop! java 8_261-oracle, is not installed.
Using java version 9.0.4-open in this shell.
I was inspired by How to use SDKMAN! to install packages from within scripts.

You could parse the output of sdkman to retrieve the list of installed sdks as follows:
#!/bin/bash
sdks=`sdk list java | grep installed | awk -F"|" '{print $6}'`
for sdk in ${sdks[#]}; do
sdk use java $sdk
### YOUR CODE HERE
done
I have omitted the . /usr/local/sdkman/bin/sdkman-init.sh part because I suggest to set it up in bashrc/zshrc as suggested in sdkman doc.
An example from my ~/.zshrc:
#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="$YOUR_PATH/.sdkman"
[[ -s "$YOUR_PATH/.sdkman/bin/sdkman-init.sh" ]] && source "$YOUR_PATH/.sdkman/bin/sdkman-init.sh"

Related

Getting latest version of Android from sdkmanager automatically

I was trying to make a bash script to automatically install flutter and android sdk without Android Studio.
I managed to get the latest version of build-tools using the following sequence
btversion=`sdkmanager --list | tac | sed "/build-tools/q" | tac | sed -n 1p`
btversion=${btversion% *}
btversion=${btversion:2}
I am trying to do something similar to install platforms;android-%version%, except doing something like this lead me to a dead end:
sdkmanager --list | tac | sed "/platforms;android-[0-9]+/g"
The [0-9]+ filter is necessary as packages such as platforms;android-TiramisuPrivacySandbox show up in the list.
I would like to know what would be a better filter. For reference, this is how the output of sdkmanager --list sort of looks like
platforms;android-27 | 3 | Android SDK Platform 27
platforms;android-28 | 6 | Android SDK Platform 28
platforms;android-29 | 5 | Android SDK Platform 29
platforms;android-30 | 3 | Android SDK Platform 30
platforms;android-31 | 1 | Android SDK Platform 31
platforms;android-32 | 1 | Android SDK Platform 32
platforms;android-33 | 2 | Android SDK Platform 33
It would be like
sdkmanager --list | egrep -i 'platforms;android-[0-9]{2}' | tac | head -n 1 | sed 's/\(platforms;android-[0-9]\+\).*/\1/'
or
sdkmanager --list | egrep -i 'platforms;android-[0-9]{2}' | sed 's/\(platforms;android-[0-9]\+\).*/\1/' | tail -1

How to set up OSX dev machines to install clean at a specific time

Does anyone know an easy way to set up OSX dev machines to install xcode and set up a clean dev environment every morning at 5am?
I was looking at using something like boxen, but the problem I was facing is they all need Xcode installed to work.
You can actually automate a lot of the pre-requites for Boxen with scripts, such as automating the Xcode installation step with a script something like this:
#!/bin/sh
# Get and install Xcode CLI tools
OSX_VERS=$(sw_vers -productVersion | awk -F "." '{print $2}')
# on 10.9+, we can leverage SUS to get the latest CLI tools
if [ "$OSX_VERS" -ge 9 ]; then
# create the placeholder file that's checked by CLI updates' .dist code
# in Apple's SUS catalog
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
# find the CLI Tools update
PROD=$(softwareupdate -l | grep "\*.*Command Line" | head -n 1 | awk -F"*" '{print $2}' | sed -e 's/^ *//' | tr -d '\n')
# install it
softwareupdate -i "$PROD" -v
# on 10.7/10.8, we instead download from public download URLs, which can be found in
# the dvtdownloadableindex:
# https://devimages.apple.com.edgekey.net/downloads/xcode/simulators/index-3905972D-B609-49CE-8D06-51ADC78E07BC.dvtdownloadableindex
else
[ "$OSX_VERS" -eq 7 ] && DMGURL=http://devimages.apple.com/downloads/xcode/command_line_tools_for_xcode_os_x_lion_april_2013.dmg
[ "$OSX_VERS" -eq 8 ] && DMGURL=http://devimages.apple.com/downloads/xcode/command_line_tools_for_osx_mountain_lion_april_2014.dmg
TOOLS=clitools.dmg
curl "$DMGURL" -o "$TOOLS"
TMPMOUNT=`/usr/bin/mktemp -d /tmp/clitools.XXXX`
hdiutil attach "$TOOLS" -mountpoint "$TMPMOUNT"
installer -pkg "$(find $TMPMOUNT -name '*.mpkg')" -target /
hdiutil detach "$TMPMOUNT"
rm -rf "$TMPMOUNT"
rm "$TOOLS"
exit
fi

Automatically Download Latest WhatsApp APK using Shell Scripting

I'm trying to create a cron job that downloads the latest version of WhatsApp's APK from their website using a bash script and make it available through my site.
So far, I'm able to obtain the version number from the site using the following (user-agent part omitted):
wget -q -O - "$#" whatsapp.com/android | grep -oP '(?<=Version )([\d.]+)'
And I can download the APK using the following command:
wget http://www.whatsapp.com/android/current/WhatsApp.apk
That part is fine. What I can't figure out is how to download the APK only if it's newer than the existing APK on the server. How should the script be?
Since I'm not a command-line pro, I guess there's a better way to achieve this than my current approach, so if you have any suggestions, I'd appreciate it very much.
Seems like you need to manage the version yourself.
I would store the apk files with a version number in the filename, e.g WhatsApp_<version-number>_.apk. So the script that downloads the newer file can be as following:
# Get the local version
oldVer=$(ls -v1 | grep -v latest | tail -n 1 | awk -F "_" '{print $2}')
# Get the server version
newVer=$(wget -q -O - "$#" whatsapp.com/android | grep -oP '(?<=Version )([\d.]+)')
# Check if the server version is newer
newestVer=$(echo -e "$oldVer\n$newVer" | sort -n | tail -n 1)
#Download the newer versino
[ "$newVer" = "$newestVer" ] && [ "$oldVer" != "$newVer" ] && wget -O WhatsApp_${newVer}_.apk http://www.whatsapp.com/android/current/WhatsApp.apk || echo "The newest version already downloaded"
#Delete all files that not is a new version
find ! -name "*$newVer*" ! -type d -exec rm -f {} \;
# set the link to the latest
ln -sf $(ls -v1 | grep -v latest| tail -n1) latest

tree command on osx bash

I'm following a screen cast on a ruby gem called pry. At 8:10, the .tree command is used, which I believe is a Unix command.
It does not appear to be working on my system:
[24] pry(main)> .tree
\Error: there was a problem executing system command: tree
and I have traced the issue to here, in which pry references a shell command:
Pry::CommandSet.new do
command(/\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => ".<shell command>") do |cmd|
if cmd =~ /^cd\s+(.+)/i
dest = $1
begin
Dir.chdir File.expand_path(dest)
rescue Errno::ENOENT
output.puts "No such directory: #{dest}"
end
else
if !system(cmd)
output.puts "Error: there was a problem executing system command: #{cmd}"
end
end
end
from the context of bash I tried using the command tree with no luck:
projects/sms(apps2)$ tree
-bash: tree: command not found
~/projects/sms(apps2)$ .tree
-bash: .tree: command not found
This looks incredibly useful, how can I get this command?
Using homebrew:
brew install tree
Using macports:
sudo port install tree
Using the source:
Follow these directions. (Caveat; you should use the flags/etc. that make sense.)
<rant>All systems should come with tree; I use it a lot. And we can post directory structures as text, not pics.</rant>
For a simple approach you can also add the following alias to your ~/.bashrc or ~/.zshrc file:
alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
This results in the following:
$ tree
.
|____.git
| |____config
| |____objects
| | |____pack
| | |____info
| |____HEAD
| |____info
| | |____exclude
| |____description
| |____hooks
| | |____commit-msg.sample
| | |____pre-rebase.sample
| | |____pre-commit.sample
| | |____applypatch-msg.sample
| | |____pre-receive.sample
| | |____prepare-commit-msg.sample
| | |____post-update.sample
| | |____pre-applypatch.sample
| | |____pre-push.sample
| | |____update.sample
| |____refs
| | |____heads
| | |____tags
Found this solution here:
http://osxdaily.com/2016/09/09/view-folder-tree-terminal-mac-os-tree-equivalent/
Use brew install tree command on the terminal if you're using Homebrew on your Mac.
Not exactly the same, but gives you a list of all directories and files in those directories using:
find .
You can also specify list directories only
find -type d
or if you want to see files only
find -type f
you can also specify depth
find -type d -maxdepth 2
Add this to your shell startup file(~/.bashrc or ~/.zshrc):
tree() {
find $1 -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
}
After restarting your shell, you can use the tree command like so:
% tree Downloads
Downloads
|____ideaIU-2020.1.3-no-jbr.tar.gz
|____Firicico.ttf

Graphviz and ascii output

Is it possible to draw ASCII diagram using Graphviz?
Something like that:
digraph
{
this -> is
this -> a
a -> test
}
Gives undesired result.
Instead, I would like to get similar ASCII representation:
this
/ \
is a
|
test
How to draw ascii diagrams from dot-files format?
If you are not perl averse, graph-easy (and the associated Graph::Easy package) can do exactly that:
http://search.cpan.org/~tels/Graph-Easy/
http://search.cpan.org/~tels/Graph-Easy/bin/graph-easy
On Mac you can install this with Homebrew and cpan:
brew install cpanminus
cpan Graph::Easy
It's easy to invoke after installation:
cat dotfile.dot | /opt/local/libexec/perl5.12/sitebin/graph-easy
Here is equivalent commands for linux:
First install cpanminus
sudo apt install cpanminus
After you can install GraphEasy
sudo cpanm Graph::Easy
Here is a sample usage
cat input.dot | graph-easy --from=dot --as_ascii
By now, in ubuntu, you can install and use graph-easy directly:
> sudo apt install libgraph-easy-perl
[...]
> graph-easy dotfile.dot
+----+ +------+
| is | <-- | this |
+----+ +------+
|
|
v
+------+
| a |
+------+
|
|
v
+------+
| test |
+------+
Another option to use Graph::Easy's ASCII functionality is directly from your browser through this small service that I hosted:
https://dot-to-ascii.ggerganov.com
Using graph-easy via docker. You can install whalebrew and use it to run graph-easy without installing too much dependancies on your local machine other than whalebrew and docker.
on MacOS with homebrew install docker
$ brew install docker
$ docker -v # check if docker is running
Install whalebrew - https://github.com/whalebrew/whalebrew (check installation alternatives)
$ brew install whalebrew
Install graph-easy via whalebrew
$ whalebrew install tsub/graph-easy
Now run it via
$ echo '[a]->[b]' | graph-easy
+---+ +---+
| a | --> | b |
+---+ +---+

Resources