Linux Terminal Check stdin, stdout and stderr

When we want to check whether current terminal is attached to stdin, stdout and stderr streams or not, we can use the below codes. I tried with piping, and redirection so both works well.

As seen in below 6 usages, stdin is not listed in output of commands except first one. Because stdin is used by piping or redirection.

$  ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; echo hello)

stdin
stdout
stderr
hello
$ echo hello | ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat)

stdout
stderr
hello
$ ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat) <<<"hello"

stdout
stderr
hello
Continue reading

Linux Bash: Language Basics

I will not write all story here but people were using Teleprinter (Teletypewriter, Teletype, TTY) from 1830s and 1840s. As seen in the following pictures, it was an electromechanical device used for communicating text over telegraph lines etc. There wasn't any monitor, output was written in a paper. Some models were creating punched tape for data storage (either from typed input or from data received from a remote source) and read back such tape for local printing or transmission.

Nowadays, we use computers with monitor and operating system but Linux still uses TTY concept under the hood 🙂

You can find some diagrams below relation with Terminal Emulator and TTY

Teletypewriter(Teletype, Teleprinter or TTY)
A restored 1930s Teletype is now a Linux TTY
A Teletype Model 32 ASR used for Telex service
Video terminals like the DEC VT-100 (1978) made teletypes obsolete as computer I/O devices
Terminal Emulator (Pseudo type writer or PTY)
Each Terminal Emulator window attached to separate TTY as shown in the above screenshot.
Continue reading

How to install Java

Downloading and Installing Java from OpenJDK

if you want to download OpenJDK manually, download it from https://jdk.java.net/

But you can download JDK programmatically as the following. Java SE 7, 8, 9, 10, 11, 12, 13 are included below script. So, run below script in your terminal.

# Java SE 7
wget https://download.java.net/openjdk/jdk7u75/ri/openjdk-7u75-b13-linux-x64-18_dec_2014.tar.gz

# Java SE 8
wget https://download.java.net/openjdk/jdk8u40/ri/openjdk-8u40-b25-linux-x64-10_feb_2015.tar.gz

# Java SE 9
wget https://download.java.net/java/GA/jdk9/9.0.4/binaries/openjdk-9.0.4_linux-x64_bin.tar.gz

# Java SE 10
wget https://download.java.net/java/GA/jdk10/10.0.2/19aef61b38124481863b1413dce1855f/13/openjdk-10.0.2_linux-x64_bin.tar.gz

# Java SE 11
wget https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_linux-x64_bin.tar.gz

# Java SE 12
wget https://download.java.net/java/GA/jdk12.0.2/e482c34c86bd4bf8b56c0b35558996b9/10/GPL/openjdk-12.0.2_linux-x64_bin.tar.gz

# Java SE 13
wget https://download.java.net/java/GA/jdk13/5b8a42f3905b406298b72d750b6919f6/33/GPL/openjdk-13_linux-x64_bin.tar.gz


mkdir /usr/lib/jvm


# Extract all downloaded jdk files
tar -xvzf openjdk-7u75-b13-linux-x64-18_dec_2014.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-8u40-b25-linux-x64-10_feb_2015.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-9.0.4_linux-x64_bin.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-10.0.2_linux-x64_bin.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-11.0.2_linux-x64_bin.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-12.0.2_linux-x64_bin.tar.gz -C /usr/lib/jvm

tar -xvzf openjdk-13_linux-x64_bin.tar.gz -C /usr/lib/jvm



# Remove downloaded jdk files
rm openjdk*


# Install Java and Java Compiler to Environment
update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-se-7u75-ri/bin/java 1

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-se-7u75-ri/bin/javac 1

update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-se-8u40-ri/bin/java 2

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-se-8u40-ri/bin/javac 2

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-9.0.4/bin/java 3

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-9.0.4/bin/javac 3

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-10.0.2/bin/java 4

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-10.0.2/bin/javac 4

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-11.0.2/bin/java 5

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-11.0.2/bin/javac 5

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12.0.2/bin/java 6

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-12.0.2/bin/javac 6

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-13/bin/java 7

update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-13/bin/javac 7



# Verify Java and Java Compiler Installation
# check if java command is pointing to correct path in system
update-alternatives --display java
update-alternatives --display javac

# List all environment variables
update-alternatives --get-selections
# or
update-alternatives --get-selections | grep java
# or
update-alternatives --get-selections | grep ^java

Check Java Version

# Verify if Java is running
java -version

# Verify if Java Compiler is running
javac -version

GitHub clone all repositories

After you join an organization, you will need to clone all git repositories in your machine probably. 🙂

So first follow this post https://kenanhancer.com/2018/11/08/github-connect-with-your-ssh-public-key/ to create a ssh key in your machine and put in github then you can clone all repositories for any user.

if you have any authentication issue, then create an access token from github, copy and paste instead of {acceess-token} in the following code.

curl -u {access-token}:x-oauth-basic -s https://api.github.com/users/kenanhancer/repos?per_page=2 | grep ssh_url | awk -F '"' '{print $4}' | xargs -n 1 -P 4 git clone

In order to learn github API endpoint urls, use the following code.

curl https://api.github.com/

To learn more details about official GitHub REST API v3, use the following link.

https://developer.github.com/v3/

Github Connect with your SSH Public Key

If you are using git for your projects, you need to run some git commands such as clone, pull or push. So, let's say that you want to clone one git project and if it is asking your username and password, you can create a SSH Public Key for your pc, after that you can paste it your github account so that you will never need to specify username and password.

You can follow below screenshot to create a SSH Public Key. After you create it, copy created SSH Public Key from terminal and paste it to GitHub SSH Keygen window like below.

Follow below screenshots to add your SSH Public Key in your GitHub account.

Uninstall Vagrant on Ubuntu

Uninstall just vagrant

This will remove just the vagrant package itself.

sudo apt-get remove vagrant

Uninstall vagrant and its dependencies

To remove the vagrant package and any other dependant package which are no longer needed from Ubuntu.

sudo apt-get remove --auto-remove vagrant

Purging vagrant (your config/data too)

If you also want to delete your local/config files for vagrant then this will work.

sudo apt-get purge vagrant

To delete configuration and/or data files of vagrant and it's dependencies from Ubuntu then execute.

sudo apt-get purge --auto-remove vagrant