Tuesday, May 22, 2018

Print chess board in command line

The following bash one-liner will print a chess board in a terminal (the script works for the shells bash and ksh only)


for (( i = 1; i <= 8 ; i++ )); do for (( j = 1 ; j <= 8; j++ )); do tot=`expr $i + $j`; tmp=`expr $tot % 2`; if [ $tmp -eq 0 ]; then echo -e -n "\033[47m  ";  else echo -e -n "\033[49m  ";  fi;   done; echo -e -n "\033[49m"; echo "" ; done; echo -e -n "\033[49m \n"



               
               
               
               
               
               
               
               

Chrome (hidden) useful settings

1. Override software rendering list 
Overrides the built-in software rendering list and enables GPU-acceleration on unsupported system configurations. #ignore-gpu-blacklist

When is this needed?
When Chrome is not using hardware acceleration

To verify if Chrome is using hardware acceleration type in chrome://gpu/. If it says 

Canvas: Software only, hardware acceleration unavailable

Then is clear that Chrome is not using hardware acceleration. To enable hardware acceleration 

Search for "hardware" and tick the checkbox for "Use hardware acceleration..." in chrome://settings
Enable "Override software rendering list" in chrome://flags/


Relaunch Chrome. type in chrome://gpu/. If the message has changed to

Canvas: Hardware accelerated

Then Chrome is using hardware acceleration.

2. Fast tab/window close
Enables fast tab/window closing - runs a tab's onunload js handler independently of the GUI.

3. Smooth Scrolling Windows, Linux, Chrome OS, Android
Animate smoothly when scrolling page content. #smooth-scrolling

4. GPU rasterization
Use GPU to rasterize web content. Requires impl-side painting. #enable-gpu-rasterization
5. GPU rasterization MSAA sample count.
Specify the number of MSAA samples for GPU rasterization. #gpu-rasterization-msaa-sample-count = 2

6. Material Design in the rest of the browser's native UI 

7. LCD text antialiasing 

8. Simple Cache for HTTP 

9. TCP Fast Open Linux, Chrome OS, Android
Enable the option to send extra authentication information in the initial SYN packet for a previously connected client, allowing faster data send start. #enable-tcp-fast-open

10. Zero-copy rasterizer
Raster threads write directly to GPU memory associated with tiles. #enable-zero-copy
11. Enable native notifications. Mac, Linux
Enable support for using the native notification toasts and notification center on platforms where these are available. #enable-native-notifications - The Chrome notifications goes through the OS 
12. Number of raster threads
Specify the number of raster threads. #num-raster-threads = 4
13. Tab audio muting UI control 
When enabled, the audio indicators in the tab strip double as tab audio mute controls. This also adds commands in the tab context menu for quickly muting multiple selected tabs. #enable-tab-audio-muting - This settings is amazing. It lets me mute a tab with embedded videos so that I can continue reading without wasting time looking for the exact location of the sound source.

14. Reduce default 'referer' header granularity.
If a page hasn't set an explicit referrer policy, setting this flag will reduce the amount of information in the 'referer' header for cross-origin requests. #reduced-referrer-granularity

15. Speculative Prefetch
"Speculative Prefetch" fetches likely resources early to improve load times, based on a local database (see chrome://predictors). "Learning" means that only the database construction is enabled, "Prefetching" that learning and prefetching are enabled. #enable-resource-prefetch

16. Off-main-thread fetch for Web Workers
If enabled, the resource fetches from worker threads will not be blocked by the busy main thread. #enable-off-main-thread-fetch

17. Enable browser side navigation
Use browser scrollbars instead of the scrollbars from the window manager of the OS 

18. chrome://net-internals/#events
Net-internals is a browser's network diagnosis center. It includes information and tools to help you see the events has been captured, solve recurring problems, and solve network problems. If you encounter a DNS error page, you may have visited this page to refresh the cache.

19. chrome://components/
This command displays all components used in Chrome browser. Here we can see the version of commonly used Flash components and check whether there are updates.

20. chrome://about/
If the user is still confused about how to find these commands, enter the "chrome://about" command, which will include all the commands supported by the Chrome browser, divided into the Chrome URLs and the commands for Debug purposes.

21. chrome://media-engagement/
This page will show the frequency of visit to the video streaming sites. It also logs visits to sites you do not want to be logged. So that's some motivation to stay off the sites with amorous content.


ttf-mscorefonts-installer error from update notifier

Error message at login:

Failure to download extra data files

The following packages requested additional data downloads after package installation, but the data could not be downloaded or could not be processed.

ttf-mscorefonts-installer


Solution: 1. Purge ttf-mscorefonts-installer

$ sudo apt-get remove --purge ttf-mscorefonts-installer

2. Remove the partial folder containing partial download in /var/lib/

$ cd /var/lib/update-notifier/package-data-downloads
$ sudo rm -rf partial

3. Install ttf-mscorefonts-installer and make sure to select “YES” to the EULA
$ sudo apt-get install ttf-mscorefonts-installer 


Monday, January 29, 2018

Removing audio noise from a video

The basic steps are to extract the audio, remove noise, and then re-insert the noise-free audio back
into the video wrapper.

Step I: Extract the noisy audio

1. Open Audacity

2. Open the video file in Audacity

3. Select a region of pure background noise (which should be periods of silence). Then use the Audacity menu to select Effects -> Noise reduction



4. Select Get Noise Profile (Important: don’t click ok)



5. Then go back to the waveform and select the entire waveform

6. Again select Effects -> Noise reduction



7. But this time just click ok

8. Audacity will work on the file to reduce the noise. Check the region selected before to see if noise is removed/reduced

9. Repeat the steps #3 to #7 as many times as necessary till all noise is gone

10. Now Export the file to mp3. Make to change the extension as Audacity uses “aiff” even when the file type is selected to be mp3.


Step II: Remove audio from video file

1. Go to the folder containing the video file.

2. Strip audio from the video file and store it in a temporary video file called temp.mp4 using the following command

$ ffmpeg -i video.mp4 -an -vcodec copy temp.mp4


temp.mp4 will have no audio

Step III: Add noise-free audio back to the video file

1. Copy the noise-reduced audio file from Audacity to this folder. Let’s say the audio file is called audio.mp3
2. Add this audio file to the temporary audio-free video file temp.mp4


$ ffmpeg -i temp.mp4 -i audio.mp3 -vcodec copy -acodec copy final.mp4


final.mp4 will contain the audio with no noise.




Wednesday, January 24, 2018

dpkg: error: duplicate file trigger interest for filename `/usr/lib/gio/modules' and package `libglib2.0-0:i386'

The dpkg trigger file has duplicates. We would need to manually remove all of them.

$ cd /var/lib/dpkg/triggers/
$ sudo vim File

/usr/share/octave/packages octave
/usr/lib/libreoffice/share/extensions libreoffice-common
/usr/lib/x86_64-linux-gnu/gio/modules libglib2.0-0
/usr/lib/gio/modules libglib2.0-0
/usr/share/glib-2.0/schemas libglib2.0-0
/usr/lib/i386-linux-gnu/gio/modules libglib2.0-0:i386
/usr/lib/gio/modules libglib2.0-0:i386
/usr/share/glib-2.0/schemas libglib2.0-0:i386
/etc/dbus-1/system.d dbus/noawait
/usr/share/dbus-1/system-services dbus/noawait
/usr/lib/vlc/plugins vlc-nox/noawait

Notice the duplicates in bold. Remove them manually.
If there are many duplicates then a quick way to see the duplicates is to run

$ cat File | cut -d" " -f 2 | uniq -c

      1 octave
      1 libreoffice-common
      3 libglib2.0-0
      3 libglib2.0-0:i386
      2 dbus/noawait
      1 vlc-nox/noawait
      2 libgdk-pixbuf2.0-0
      1 udev/noawait
      1 cups/noawait
      2 systemd/noawait

The number in front of each package gives the duplicate count.

After removing duplicates run

$ sudo apt-get install -f

to install missing dependencies


Monday, October 2, 2017

OK GOOGLE on Samsung Galaxy S7 doesn’t work

To make Ok Google detection work on Galaxy S7 (Galaxy series phones) we need to perform a couple of steps.


1. As long as Samsung S voice is enabled, it interferes with Ok Google detection.


So disable S Voice first.


Goto settings -> Apps -> S Voice -> Disable. If needed Force Stop the app.


2. Make sure S voice is no longer the default app for voice commands. Otherwise the phone will start it again instead of Google assistant.


Goto settings -> Apps -> Default apps -> Device assistance app


Make sure that the device assistance app is Google.


3. Make sure “Ok Google” is turned ON.


Tap the google search bar to go to the Google Now app. Tap the big G at the top left corner and then the three bars at the top left corner and choose settings -> Voice -> OK Google detection. Make sure that Ok Google detection is turned on and Trusted voice is turned on too. Train voice model.


4. Restart the phone. This is a crucial step.

Saturday, May 20, 2017

Common questions on climate change

1.  Are renewable energy systems even viable or are they just an Utopian dream of impractical scientists? And even if they are viable wouldn’t it cost an arm and a leg to make them viable?


Among the multitude of scientific journal articles, which answers both these questions separately, one article answers both these questions succinctly in a single paper.


In 2011 Jacobson et al. proposed a 100% WWS (wind, water, solar) energy system which can supplant the current fossil fuel industry. They found that production cost of WWS is exactly same as current fossil fuel-derived energy systems and that includes costs of production, storing, and transmission (with transmission losses). Their conclusion is that “ … barriers to a 100% conversion to WWS power worldwide are primarily social and political, not technological or even economic.”


2. Which countries invest the most in renewables?


The current standing (May, 2017) is:


3. What is geothermal energy?


Geothermal Energy is a viable a source of renewable energy. A NatGeo documentary Steam Drilling (2012) explores the steamy world of geothermal energy production, its challenges, and its tremendous potential as a renewable energy source.


The only problem with geothermal energy is its location dependence. Only a few places on Earth are fortunate enough to have access to geothermal energy reserves. Krafla Power Station in Iceland is one such place. Till now it remains unique in its production of geothermal energy.


4. Many liberals claim that 97% of climate scientists agree that climate change is real and that it is man-made while conservatives say that it is a made-up number and the actual consensus is much lower. Who is right and where did this number come from?

The 97% comes from a series of surveys and meta-analysis done by climate scientists on the level of consensus in their own community. See this short video to get a quick update on how the number is arrived at or read the article at skepticalscience. However, the most convincing argument always come from a paper published in a reputed a peer-reviewed journal.  And that argument can be found in the recent article by Cook J et al 2016 Consensus on consensus: a synthesis of consensus estimates on human-caused global warming Environ. Res. Lett. 11 048002 IOPscience.

Thursday, December 15, 2016

Command-line password generator for Linux

Linux has its own cryptographic generator which can easily be harnessed by the common user to generate extremely strong passwords. This password generator can be found at /dev/random. /dev/random generates pseudorandom numbers based on the available entropy of the system (see wiki page). A derivate of /dev/random called /dev/urandom does the same thing but it is more accessible while being slightly (theoretically) less secure. So what stops us from grabbing outout from /dev/urandom using cat? An output of the content of /dev/urandom will demonstrate that it is practically gibberish with very few usable random characters. The output of

cat /dev/urandom 

should convince the user.  However, it is possible to trim out the unwanted characters and only keep alphabets, numbers, and special characters with a simple filtering by the tr command.


cat /dev/urandom | head -c 10 | tr -dc "A-Za-z0-9_!"

This command will produce the desired output but it will generate very small passwords, which are truly random but which do not meet the length criteria in most cases. Therefore, this procedure needs to be repeated a large number of times until the required length of random passwords can be generated. It is better to write a bash script which will automate this procedure.

The following bash script generates random passwords and allows the user to control the length and number of random passwords that are generated. It also allows special characters to be included or excluded with a switch (argument). Since I am always in a hurry and don't like passing too many arguments, I wrote the script so that it can accept arguments in a shorthand manner in addition to the longhand manner (Unix style) of passing arguments. Shorthand way: ./randompass.sh 10 2 1 will generate 2 random passwords of length 10 and they will all include special characters.


~$ ./randompass.sh 10 2 1

ST5%l36HpG
4Tvq%Ia1UZ

Or I could use the longhand (Unix-style) way of passing the arguments: ./randompass.sh -l 10 -n 2 -s 1


~$ ./randompass.sh -l 10 -n 2 -s 1

IofK1_qA%l
%5aw_YY1XZ


The result will be the same. I have included the full code below which is well commented. I also provide explanation and cite sources below the code.


#!/bin/bash

# generate random passwords using urandom CSPRNG.  

# randompass.sh v5.2 by Jones

# ---- SET INITIAL VALUES ----
length=16
num=4
special=1
usage="
Usage: ./randompass.sh [options ...]
  -l password length. DEFAULT=16
  -n number of passwords to generate. DEFAULT=4
  -s special characters. DEFAULT=1. Set to 0 to disable selecting _!%
  -h help
  
Example 1: ./randompass.sh -l 8 -n 2 -s 0
  
OR

./randompass.sh [password length] [number of passwords to generate] [include special characters] 

Example 2: ./randompass.sh 8 2 0   "

if [[ "$1" =~ ^[0-9]+$ ]]; then  
 # ---- SET INITIAL VALUES ----
 length=$1
 num=$2
 special=$3

 # Second and third argument might be empty which means $num and $special
 # will be empty at this point too. Set the values of $num and $special 
 #to the default values in case they are empty
 [ "$2" == "" ] && num=4
 [ "$3" == "" ] && special=1
else
 # If user uses arguments then use getopts.
 while getopts l:n:s:h option
 do
  case "${option}"
  in
          l) length=${OPTARG};;
          n) num=${OPTARG};;
          s) special=${OPTARG};;
          h) echo "$usage" ;;
  esac
 done
fi

# ---- CHECK ARG VALIDITY ----

# check if the first argument is an integer. If it is not then throw error. 
#=~ matches REGEXP ^[0-9]+$ which is one or more occurences of the numbers 0-9
if [[ !("$length" =~ ^[0-9]+$ )]]; then
 echo "Enter numbers only for the password length"
 echo "$usage"
 exit
fi

# check if the second argument is an integer. If it is not then throw error.
if [[ !("$num" =~ ^[0-9]+$) ]]; then
 echo "Enter numbers only for the number of passwords to generate"
 echo "$usage"
 exit
fi

# check if the third argument is an integer. If it is not then throw error. 
if [[ !("$special" =~ ^[0-1]+$) ]]; then
 echo "Enter only 0 or 1 for the third argument."
 echo "$usage"
 exit
fi


# ---- GENERATE THE PASSWORDS ----
if [[ $special == "1" ]]; then # if special chars needed: DEFAULT
 i="0"
# echo "first loop for 1"
 while [ $i -lt $num ] 
 # Keep looping till next password, which satisfies the required criteria, 
 # is successfully generated. May require many more passess for stricter 
 # requirements, usually smaller length passwords.
 do 
 pass=`tr -dc "A-Za-z0-9_!%" < /dev/urandom | head -c ${length} | xargs`

 if [ `echo $pass | grep "%" | grep "[0-9]" ` ]; then
   echo $pass
   i=$[$i+1]
 fi
 done
else  # if special chars are NOT needed
 i="0"
# echo "second loop for 0"
 while [ $i -lt $num ]
 do 
 pass=`tr -dc "A-Za-z0-9" < /dev/urandom | head -c ${length} | xargs`

 if [ `echo $pass | grep "[0-9]" ` ]; then
   echo $pass
   i=$[$i+1]
 fi
 done
fi



 

Parsing the command-line

The block of code which parses the command-line options using getopts is found at lifewire. The post at that link provides a clear explanation of what the block of code while getopts l:n:s:h option does. So I will only mention that the while loop parses through the command-line arguments and if it finds any matches to the options that the script expects, then it assigns the value passed to the option to the corresponding variables, defined in the script.

Checking validity of arguments passed

Since the script can only accept numeric arguments, their values need to be checked for non-numeric entries. If found an error message is displayed. To check if the values are numeric we can treat the value as a string and verify that every character lies between 0-9. This ensures that there are no non-numeric characters in that string which in turn implies that the string is a number. "$num" =~ ^[0-9]+$  does exactly that. =~ operator is used for REGEX matching of lvalue $num to rvalue ^[0-9]+$ (which is a REGEX that matches every character to characters between 0-9). A similar test is applied for the special variable which acts as a switch to include or exclude special characters. However, it only takes values 0 or 1. Hence its REGEX is ^0-1+$. For more information on regular expressions see this quick reference.




Sunday, November 27, 2016

Turning normal HDTV into Smart HDTV

One word ChromeCast. That will do it. There is no need to pay a premium for a Smart TV when Chromecast can outperform any of them for a fraction of the cost. Also, there are reports of some Smart TVs stealing user browsing behavior for commercial purposes. The worst was the claim that some TVs secretly listen to the conversation of the people in the room. I cannot verify the truthfulness of these reports but all these are viable possibilities if a malicious app has been secretly installed in the Smart TV. So I would rather play it safe and use Chromecast (since Google has all my data anyways). So we need to buy the Chromecast device and install the Chromecast app on the phone and that will transform the non-smart TV into an entertainment hub. However, just installing the Chromecast app on the phone does not satisfy a power user. Therefore, I chose to explore the possibilities of "Home entertainment".

1. Streaming movies from phone - Install Chromecast app. This is simple and most straighforward way to use it.

2. Streaming movies from the computer - Install Videostream app in Chrome and play the files straight from the local hard drive. However, one drawback of the free version is that it does not support adding a subtitle file. So the only options are to either pay for it or encode the subtitle files into the video files themselves. I will choose the latter option. We will need to create a mkv container and then embed the video and the subtitle files in that container. Videostream supports mkv files so it will stream the video to the TV along with the embedded subtitles.

The embedding can be done with a single command

mkvmerge -o output.mkv video.mp4 eng.srt

However, if there is a lag in the subtitle file and it needs to be resynced with the video file then a couple of additional steps will be required. I wrote an article about it here.

Saturday, August 13, 2016

Setting up Crafty chess engine on Linux/Ubuntu

Crafty installation is pretty simple. The command

sudo apt-get install crafty

will install crafty at /usr/games/crafty and the book files at /usr/share/crafty/books.bin.

If the book files are not installed they can be manually installed by

sudo apt-get install crafty-books-medtosmall

Sometimes the book are not automatically installed in the home folder and it will result in the following error when crafty is run from the command line.

Machine is NUMA, 1 nodes (6 cpus/node)

unable to open book file [./book.bin].
book is disabled
unable to open book file [./books.bin].
ERROR, unable to open game history file, exiting

This implies that we need to import the book.bin and books.bin which contain a vast array of opening moves into the local ~/.crafty directory. For that, we need to run the setup_crafty.sh. However, it needs to be made executable by the normal user first. As root go to the directory /usr/share/doc/crafty and do the following,

su
cd /usr/share/doc/crafty 
sudo chmod 777 setup_crafty.sh 

Then logout from root, go back to the same directory /usr/share/doc/crafty and run as user:

./setup_crafty.sh

Don't run the last command as root otherwise the .crafty directory will be created in the /root directory! It will spit out a few lines of info. Copy the lines

export CRAFTY_LOG_PATH=/home/<USERNAME>/.crafty
export CRAFTY_RC_PATH=/home/<USERNAME>/.crafty
export CRAFTY_TB_PATH=/home/<USERNAME>/.crafty
export CRAFTY_BOOK_PATH=/home/<USERNAME>/.crafty

to .bashrc file and then

source .bashrc

After that source


source /home/<USERNAME>/.crafty/env_settings

Run Crafty. It should be able to see the book files now.




Friday, September 4, 2015

Generate email filters in Gmail from list of emails

I have a lot of students from different classes who email me regularly. It is helpful to tell who belongs to which class. So I created a filter in Gmail which will sort the student's email according to their email ID. This scenario is also applicable to industry where we want to sort emails using filters. Most of the time people are able to do it by hand. However, for me it was challenging to do it by hand since I had 400 students. Therefore I fell back to Bash tools to automate the task.

The first thing is to extract all the email IDs from a CSV file and save it as a list.

cat file.csv | cut -d"," -f10

The 10th column had the email IDs in the CSV file. The fields are separated by commas in CSV files. Therefore, I have used -d"," as the delimiter in the cut command.

"rwrwfa"
"ebbra"
"rksda"
"ru57a"

But they have the " which I need to get rid off. Easy use tr.

cat file.csv | cut -d"," -f12 | sed 's/"//'

rwrwfa"
ebbra"
rksda"
ru57a"

The reason why I didn't get rid off the last " is because I am going to replace that with @aaa.edu since these email IDs do no have the @aaa.edu in them.

cat file.csv | cut -d"," -f12 | sed 's/"//' | sed 's/"/@aau.edu'

"rwrwfa@aaa.edu"
"ebbra@aaa.edu"
"rksda@aaa.edu"
"ru57a@aaa.edu"

Save this list to a file tempfile1.

cat file.csv | cut -d"," -f12 | sed 's/"//' | sed 's/"/@aau.edu' > tempfile1

The reason why I can use . (dot) inside sed is because I used  ' ' instead of " "  so .(dot) is interpreted as . (dot) and not as a wildcard. 








Print chess board in command line

The following bash one-liner will print a chess board in a terminal (the script works for the shells bash and ksh only) for (( i = 1; i ...