Saturday, June 29, 2019

Bash - Another thing for the lazy: umount by name

Hi, just another script. You won't believe how many errors you can get before this simple thing works. Sometimes I wonder if my brain does it just for kicks, to see what funny message we can get this time. Or maybe it is too lazy to bother with heuristics and just goes over all the imaginable options. Surprisingly, the second thing feels like an easier one.

Or maybe it does both: 

#!/bin/bash

if [ -z $1 ]
  then printf "\nPlease provide drive name (label)\n\n"
    exit
fi

drivelabel = $1

cat /etc/mtab | grep ${drivelabel} | cut -d " " -f1 | xargs umount

Friday, June 7, 2019

Bash - Bulk file update with find and sed

Doing bulk file update (mind escapes when using complicated patterns):

#!/bin/bash

if [ -z $1 ] || [ -z $2 ]  || [ -z $3 ] || [ -z $4 ]; then
        printf "\nPlease enter:\n
        1. file command-compatible filename pattern
        2. backup suffix for affected files
        3. pattern to replace
        4. replacement\n\n
        For instance, $0 *.txt .xml-backup blue red\n
        This should replace blue with red in all txt files\n\n"
        exit
fi


filename_pattern=$1
backup_suffix=$2
pattern_to_replace=$3
replace_with=$4

find . -type f -name ${filename_pattern} -exec sed -i${backup_suffix} "s|${pattern_to_replace}|$replace_with|g" {} \+

Bash - Cut a fragment of a log by some criteria (like timestamp)

Here is how to cut out a piece of a log between two unique strings (like timestamps):

#!/bin/bash

printf "\nResults are returned *inclusive*\n\n"

if [ -z $1 ] || [ -z $2 ] || [ -z $3 ]; then
        printf "\nPlease enter SUBSTRSTART SUBSTREND and LOGPATH\nFor instance, $0 11:59:17,206 12:02:14,606 test.log\n\n"
        exit
fi

substrstart=$1
substrend=$2
logpath=$3

sed -n "/${substrstart}/,/${substrend}/p" ${logpath} | tee log_excerpt-$(date +%F_%H-%M-%S) | cat

Bash - Getting network data for the port (localhost only)

Getting network data:

#!/bin/bash

printf "\nCurrently for localhost only\n\n"

if [ -z $1 ]; then
    printf "Please enter port and [host]\n\n"
    exit
fi

port=$1
host=localhost

printf '========= lsof -i ============\n\n'

lsof -i :${port}

printf '========= netstat -lnt | grep $port ============\n\n'

netstat -lnt | grep ${port}

printf '========= nc -z $host $port  ============\n\n'

nc -z ${host} ${port}


Bash - kill everything with a matching name

Just a small script for killing matching processes by name:

#!/bin/bash

if [ -z $1 ]
then
    printf "keyword is empty\n\n"
    exit
fi

let k=0
let min=1

# mind the double brakets in do part
for i in $(ps -e | grep "$1"); do ((k++)); done

if [ "${k}" -lt "${min}" ]
then
    printf "no such process to kill\n\n"
    exit
fi


for i in $(ps -e | grep $1 | cut -s -f1 -d\t); do kill -9 ${i}; done

Thursday, June 6, 2019

Password generator with bash

Looks like the best way to handle your passwords strongly depends on which password breaking approach is the most effective at the moment, and apparently those ways are changing as quickly as technology develops. Which is why it's unlikely that there will ever be a perfect way to avoid all the risks, but we can design an optimal one.

It occurred to me that first we need to select between two strategies: storing generated password or memorizing them with mnemonics. I don't think it would be a good idea to store memorizable data cause it's too easy to break the pattern, or to try to remember a generated one. As I already can do mnemonics, so I decided to write my own password generator (I like having my own collection of tools).

Solution is based on LINK. Stuff in use: cat, tr, fold, head, /dev/urandom, echo, printf, for each and parameters testing

Wednesday, May 29, 2019

Configuration - setting up Docker for Ubuntu 19.04

A friend of mine installed fresh Ubuntu 19.04 for which there is no stable docker yet. So we had to do it a slightly more complicated way.  

NOTE: this configuration does not help with running docker over VPN
NOTE: Some of the commands below may require sudo
NOTE: It is assumed that you configure docker for user without sudo, cause using it with sudo does not quite make sense

Self-education - Using learning matrix to plan and assess your progress

When you are learning something new and you are doing it by yourself, you may want to avoid the usual problems associated with learning, such as losing control and being unable to assess the results. I would like you to consider using learning matrix -- something I designed for myself when I need a really quick start with a new project at work.

In my experience, this approach may be useful when you cannot study full-time and have to do it alongside other activities. Or maybe you are creating a competency matrix or need an assessment tool.

The principle behind this learning matrix is simple. You list all the major areas vertically, and you put levels of qualification horizontally, and what you get is a 2D matrix that describes the area you would like to assess or master. The cells of this matrix contain criteria a learner should meet in order to be able to say, for instance, 'I have level A in functional programming' or 'I have level B in yoga'.

A word of advice. Do not try to make it too big, make sure it is feasible to learn everything you have planned. If you are not sure about some criterion, mark that criterion appropriately, to prevent it from mixing together with the clear ones. If possible, try to make your criteria SMART (simple/specific, measurable, attainable, reasonable, time-bound).

The example below is for starting with a project, but you can apply it to an academic subject or learning a skill as well. Please note that criteria add up (which means that you cannot get level B without successfully passing level A).


Tuesday, May 28, 2019

Coding challenge - Repeated string

When I popped out for some food today it dawned on me that there are basically two type of tests. First try to find out if you're good at drilling and memorising, and second one is looking for an ability to create your own solution. Sadly enough, first type claims to the the second type, but never vice versa.

My version of code is below and original task can be found here: LINK


Monday, May 27, 2019