Command Substitution in bash

Text between backticks is executed and replaced by the output of the command (minus the trailing newline characters, and beware that shell behaviors vary when there are NUL characters in the output). That is called command substitution because it is substituted with the output of the command.

A=`cat /etc/p2ass2wd2 | head -n1`
echo "$A"
A=$(cat /etc/p2ass2wd2 | head -n1)
echo "$A"

References
https://unix.stackexchange.com/questions/48392/understanding-backtick
https://unix.stackexchange.com/questions/147420/what-is-in-a-command

Get IP Address using bash

Displaying private IP addresses

ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'

address=$(ip addr show wlp3s0 | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/')

Displaying the public IP address

If you want to know the public IP address of a Linux server, you can send an HTTP request to one of the following web servers.

  • http://ifconfig.me
  • http://www.icanhazip.com
  • http://ipecho.net/plain
  • http://indent.me
  • http://bot.whatismyipaddress.com
  • https://diagnostic.opendns.com/myip
  • http://checkip.amazonaws.com
curl http://checkip.amazonaws.com
wget -qO- http://checkip.amazonaws.com

References
https://unix.stackexchange.com/questions/119269/how-to-get-ip-address-using-shell-script
https://www.linuxtrainingacademy.com/determine-public-ip-address-command-line-curl/

Staging the changes in git

git status

git add -A stages all changes

git add . stages new files and modifications, without deletions

git add -u stages modifications and deletions, without new files

git add -A is equivalent to git add .; git add -u

git add -A is equivalent to git add --all

git add -u is equivalent to git add --update

References
https://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add
https://git-scm.com/docs/git-stage
https://git-scm.com/docs/git-add

Signing Commits in Git using GPG Key

gpg --list-secret-keys --keyid-format LONG

From the list of GPG keys, copy the GPG key ID you’d like to use. In this example, the GPG key ID is 3AA5C34371567BD2

$ gpg --list-secret-keys --keyid-format LONG
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid                          Hubot 
ssb   4096R/42B317FD4BA89E7A 2016-03-10
git config --global user.signingkey 3AA5C34371567BD2

or

git commit -S -m your commit message

References
https://help.github.com/en/articles/telling-git-about-your-signing-key
https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work
https://help.github.com/en/articles/signing-commits

Subjects in RxJava

  • PublishSubject
  • BehaviorSubject
  • AsyncSubject
  • ReplaySubject
  • UnicastSubject
  • SingleSubject

A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by re-emitting them, and it can also emit new items.

Publish Subject

It emits all the subsequent items of the source Observable at the time of subscription.

Here, if a student entered late into the classroom, he just wants to listen from that point of time when he entered the classroom.

PublishSubject<Integer> source = PublishSubject.create();

// It will get 1, 2, 3, 4 and onComplete
source.subscribe(getFirstObserver()); 

source.onNext(1);
source.onNext(2);
source.onNext(3);

// It will get 4 and onComplete for second observer also.
source.subscribe(getSecondObserver());

source.onNext(4);
source.onComplete();

Replay Subject

It emits all the items of the source Observable, regardless of when the subscriber subscribes.

Here, if a student entered late into the classroom, he wants to listen from the beginning.

ReplaySubject<Integer> source = ReplaySubject.create();
// It will get 1, 2, 3, 4
source.subscribe(getFirstObserver());
source.onNext(1);
source.onNext(2);
source.onNext(3);
source.onNext(4);
source.onComplete();
// It will also get 1, 2, 3, 4 as we have used replay Subject
source.subscribe(getSecondObserver());

Behavior Subject

It emits the most recently emitted item and all the subsequent items of the source Observable when an observer subscribes to it.

Here, if a student entered late into the classroom, he wants to listen the most recent things(not from the beginning) being taught by the professor so that he gets the idea of the context.

BehaviorSubject<Integer> source = BehaviorSubject.create();
// It will get 1, 2, 3, 4 and onComplete
source.subscribe(getFirstObserver());
source.onNext(1);
source.onNext(2);
source.onNext(3);
// It will get 3(last emitted)and 4(subsequent item) and onComplete
source.subscribe(getSecondObserver());
source.onNext(4);
source.onComplete();

Async Subject

It only emits the last value of the source Observable(and only the last value) only after that source Observable completes.

Here, if a student entered at any point of time into the classroom, and he wants to listen only about the last thing(and only the last thing) being taught, after class is over.

AsyncSubject<Integer> source = AsyncSubject.create();
// It will get only 4 and onComplete
source.subscribe(getFirstObserver());
source.onNext(1);
source.onNext(2);
source.onNext(3);
// It will also get only get 4 and onComplete
source.subscribe(getSecondObserver());
source.onNext(4);
source.onComplete();

References
http://reactivex.io/documentation/subject.html
https://blog.mindorks.com/understanding-rxjava-subject-publish-replay-behavior-and-async-subject-224d663d452f
https://www.journaldev.com/22573/rxjava-subject

Create Observable using empty method in RxJava

This type of source signals completion immediately upon subscription.

Observable<String> empty = Observable.empty();

empty.subscribe(new DefaultObserver<String>() {
    @Override
    public void onNext(String s) {
        System.out.println("This should never be printed!");
    }

    @Override
    public void onError(Throwable e) {
        System.out.println("Or this!");
    }

    @Override
    public void onComplete() {
        System.out.println("Done will be printed.");
    }
});

References
https://github.com/ReactiveX/RxJava/wiki/Creating-Observables#empty