Bash Script to Get Operating System + Version as string - bash

I want to make a bash script to take the system os and the version as a simple string.
Possible ways to get these info is from
/etc/issue
cat /etc/*-release
lsb_release -a
and probably some others which i dont know. The problem is that i want the bash script to work on Ubuntu 12,13,14 and CentOS. Some of the above does not work in these systems. For example the lsb_release does not work on CentOS and sometimes the /etc/issue is empty so i'm little confused about it.
As for the string i want to get it in this way (and save it to var). I will give examples.
If OS is Ubuntu 12.x i want to take it as ubuntu12
If OS is Ubuntu 13.x i want to take it as ubuntu13
If OS is CentOS 7.x i want to take it as centos7
Is that easy?
THANK YOU

Here is a bash solution. I tested on Ubuntu, but not on CentOS (I only have RHEL available now). But you can test the CentOS part and modify as needed.
#!/usr/bin/env bash
RELEASE=unknown
version=$( lsb_release -r | grep -oP "[0-9]+" | head -1 )
if lsb_release -d | grep -q "CentOS"; then
RELEASE=centos$version
elif lsb_release -d | grep -q "Ubuntu"; then
RELEASE=ubuntu$version
fi
echo $RELEASE
Or, without lsb_release on CentOS:
#!/usr/bin/env bash
RELEASE=unknown
if [ -f /etc/redhat-release ]; then
version=$( cat /etc/redhat-release | grep -oP "[0-9]+" | head -1 )
RELEASE=centos$version
elif [ -n $(which lsb_release 2> /dev/null) ] && lsb_release -d | grep -q "Ubuntu"; then
version=$( lsb_release -d | grep -oP "[0-9]+" | head -1 )
RELEASE=ubuntu$version
fi
echo $RELEASE
In any case, there's more than one way to skin this cat.

Related

extracting the java version from multiple linux servers

My below script is not giving the correct java version from the remote server, instead prints the version of the source server:
for i in 'cat serverlist.txt'
do
ssh $i `java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'|sed 's/"//g'`
done >>sample.txt
cat sample.txt
expected result would like below:
eg: 1.8.181 (each server would be having a different version, that shud be printed)
You shouldn't be using back ticks in remote command - single quotes are probably what you're looking for:
ssh $i 'java -version 2>&1 >/dev/null | grep "version" | cut -d" " -f 3-'
I am able to get my results correctly after trying the below:
ssh $server >sample.txt 2>&1 java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'|sed 's/"//g' >>s1.txt exit; cat s1.txt

Installing latest docker compose on Ubuntu

I use the following to install the most recent docker compose for my ubuntu server:
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
How to do I make this more version agnostic. For instance, so that I do not have to go in and keep changing the version -which in this case is 1.21.2. How do I change the command so it gets the most latest stable release?
How do I change the command so it gets the most latest stable release?
You could try following:
curl -L https://github.com/docker/compose/releases/download/`curl -Ls -o /dev/null -w %{url_effective} https://github.com/docker/compose/releases/latest | awk -F / '{print $NF}'`/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
This is same as your script only replacing actual version (1.21.2 in your case) with latest tag over several steps:
First we get redirection url for latest stable:
curl -Ls -o /dev/null -w %{url_effective} https://github.com/docker/compose/releases/latest
currently it resolves to https://github.com/docker/compose/releases/tag/1.21.2
Then we get version tag out of redirection url:
| awk -F / '{print $NF}'
currently resolving to 1.21.2
Finally we execute it in place of version number using your original curl statement. Note that this can break if latest tag is not properly redirected and ads some extra complexity, but automates version pulling as requested.
Accepted answer isn't the latest stable version according to https://docs.docker.com/compose/release-notes/ (returns v2 instead of the latest v1 which I was looking for)
This is the monstrosity I went with
rm -Rf /usr/local/bin/docker-compose && version=$(curl -s https://docs.docker.com/compose/release-notes/ | grep "Docker Compose release notes" | grep "Estimated reading time" | sed 's/.*id=//g' | sed 's/<.*$//g' | sed 's/.*>//g') && curl -L https://github.com/docker/compose/releases/download/${version}/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose

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

bash/shell: easiest way to get kernel name components on command line

On my Fedora machine I sometimes need to find out certain components of the kernel name, e.g.
VERSION=3.18.9-200.fc21
VERSION_ARCH=3.18.9-200.fc21.x86_64
SHORT_VERSION=3.18
DIST_VERSION=fc21
EXTRAVERSION = -200.fc21.x86_64
I know uname -a/-r/-m but these give me not all the components I need.
Of course I can just disassemble uname -r e.g.
KERNEL_VERSION_ARCH=$(uname -r)
KERNEL_VERSION=$(uname -r | cut -d '.' -f 1-4)
KERNEL_SHORT_VERSION=$(uname -r | cut -d '.' -f 1-2)
KERNEL_DIST_VERSION=$(uname -r | cut -d '.' -f 4)
EXTRAVERSION="-$(uname -r | cut -d '-' -f 2)"
But this seems very cumbersome and not future-safe to me.
Question: is there an elegant way (i.e. more readable and distribution aware) to get all kernel version/name components I need?
Nice would be s.th. like
kernel-ver -f "%M.%m.%p-%e.%a"
3.19.4-200.fc21.x86_64
kernel-ver -f "%M.%m"
3.19
kernel-ver -f "%d"
fc21
Of course the uname -r part would need a bit sed/awk/grep magic. But there are some other options you can try:
cat /etc/os-release
cat /etc/lsb-release
Since it's fedora you can try: cat /etc/fedora-release
lsb_release -a is also worth a try.
cat /proc/version, but that nearly the same output as uname -a
In the files /etc/*-release the format is already VARIABLE=value, so you could source the file directly and access the variables later:
$ source /etc/os-release
$ echo $ID
fedora
To sum this up a command that should work on every system that combines the above ideas:
cat /etc/*_ver* /etc/*-rel* 2>/dev/null

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

Resources