Using Bash Script to Check if a Command exist

A nifty code snippet that will allow you to check for the availability of a command.


#!/bin/bash

if type "command-name" &> /dev/null; then
echo "Command exist!"
else
echo "Command does not exist! :("
fi

Used this myself for a script, where I wanted to make sure a certain command was available before continuing the script execution.
That being said; it was a bit of a pain to figure out how to do this, while searching the web for answers!
Solution discovered, solution shared.

Creating Digitalocean droplets on Windows using doctl

In this guide, I will be walking through the process of setting up a droplet from your own windows computer, using Digitalocean’s API tool Doctl. This is something that can help you automate certain processes, such as setting up testing environments or servers on demand. Perhaps you and your buddies would like to quickly setup and tear down temporary game servers, without spending all that extra time configuring it? (Might cover this in the future) Whatever may be your reasons, I aim to provide the necessary information to help you get started setting up your own droplet from your own Windows machine.

Download doctl – digitalocean’s API tool

Go to the official github page for doctl.

From here, select the releases tab.

From this page, we will have to choose the windows version according to your own CPU.
If you are running on a computer with an AMD CPU (64bit), select doct-1.8.3-windows-4.0-amd64.zip. If you are running on an intel computer (32 or 64bit), select doctl-1.8.3-windows-4.0-386.zip.

(Optional) Find out which CPU your computer use

If you do not know whether you are running on an AMD or Intel CPU, open up the start menu and type in dxdiag.

The first time you run it, it is going to ask if you would like to check if your drivers are digitally signed. Select yes.

Here you should be able to see whether or not it says AMD or Intel(R) next to Processor. In my own case, I use Intel CPU.

Install and setup doctl

After you have downloaded the appropriate .zip file, extract the contents. I would recommend you rename it and put it under C:/doctl/ for ease of access. Whichever path you choose, make sure you keep using your own path throughout these examples.

 

Next we want to make sure we can access the doctl.exe file from any location via the command line/powershell, by adding it as a system path environment variable. With that done, you are able to run it from any location via the command line, powershell, or cmder.

Getting your API Token from Digitalocean.com

In order to make sure that only you can create droplets from your own account, we use API tokens. An API token is a randomly generated value, generated by digitalocean, that will act as your secure password and ID for interacting with the digitalocean API via doctl.

Start by logging in to your digitalocean account, and go to your API page.

Make sure you are in the Tokens/Keys tab and select the huge Generate New Token button in the middle.

A popup will show up, and ask you for a Token name, as well as whether or not to give it Write access. Leave it as is, and give it a recognizable and nice token name. Select Generate Token.

A new entry should have been added, and below the nice token name, you should see a long string.

Copy the string, either by doing it manually or pressing the invisible Copy button to the right of it. Make sure you store it somewhere secure. 

Now we have our token to use with doctl.

Authenticate yourself in Doctl

Opening up your favorite command prompt, type in (note that adding .exe is optional):

doctl auth init

It will then ask you for your digitalocean access token. Copy and paste it in. Make sure you do not have any trailing white spaces before pressing enter.
Assuming you entered the correct token, it should tell you that validation succeeded.

We are now ready to enter the appropriate commands to create our own droplets from the command line!

Creating a Droplet

Before we can create a droplet, we need to figure out where we want to host it, what type of image (OS) we want to run, and the size of the droplet.

The format we are going to run is:

doctl compute droplet create <name> --region <region-slug> --image <image-slug> --size <size-slug>

Picking a region

Let us begin by figuring out which region we want to host the droplet in. We can get a list of available regions by typing in the following:

doctl compute region list

Here you can choose the slug for the region you wish to use. For this example, I will use London 1, which has the slug name of lon1.

Picking an distribution or application image

We can choose whether or not we want use a clean distribution image, or a ready application image such as docker, GitLab, LEMP, and so on.
You can list the available distribution images by entering:

doctl compute image list-distribution

Here you can choose the slug for the image you wish to use. In the same manner, you can list available application images by running:

doctl compute image list-application

For this example, I will use the CentOS distribution image, which has the slug name centos-7-x64.

Choosing a droplet size

In order to list available sizes, we can run:

doctl compute size list

For this example, I will choose the cheapest one with a slug name s-1vcpu-1gb.

Create droplet

Now that we have our slug for region, image, and size, we can create our image from the command line.

Choose a name for the droplet, and fill the chosen slugs:

doctl compute droplet create <name> --region <region-slug> --image <image-slug> --size <size-slug>

Since I choose a server in London lon1, and a CentOS 7 image centos-7-x64, with a size of s-1vcpu-1gb, the full command would look like this, using the name my-droplet:

doctl compute droplet create my-droplet --region lon1 --image centos-7-x64 --size s-1vcpu-1gb

Done! 

You should now have your own running server. You should be receiving a password in your email associated with your digitalocean account in order to login.
If you go back to your digitalocean dashboard, you should see the new droplet under your resources.

 

Feel free to drop any C&Cs or questions in the comment below.

How to set system path environment variable in Windows 10

You may have encountered the term “system environment variables” from time to time. More specifically, system path environment variable. This is often talked about in context of running executable files via the command line / powershell window.

In this example, I will demonstrate how you would install and run cmder from the command line, but you are of course free to do this for any application containing executable files.
Cmder is a terminal emulator for Windows, and I highly recommend you try it out if you are a developer that is comfortable using a terminal.

Our task is done when we are able to open Cmder.exe via the command line, regardless of current directory path.
Without adding the system path environment variable, you would not be able to run Cmder.exe without navigating to the folder containing the file.

How to setup a system path environment variable

  1. Download the application/tool you wish to setup a system path environment variable for. E.g. cmder.
  2. Unzip and place the folder somewhere. In this example, I will place it in C:\cmder\Make sure the folder containing the executable file(s) you wish to run is the folder path you use hereforth. It is common for applications to have a \app\bin\ folder containing the executable files. However in our example, the executable file is in the \cmder\ folder. 
  3. Open Edit the system environment variables page on Windows, using the Start Menu, and by starting to type it out.
  4. Select Environment Variables…
  5. Double-click the Path variable.
  6. Select New and enter the path to the folder containing the binary files. For example using cmder, it would be C:\cmder

  7. Select OK and exit.
  8. Open up command prompt or powershell (if you already got one open, make sure to close and re-open it).
  9. Type in Cmder.exe and hit enter. Cmder should open regardless if there is, or isn’t any Cmder.exe file in the current command line directory.
  10. Bask in the glory of success.

The internet sensation 'Success Baby' is all grown up

 

Feel free to drop by a comment if you are having difficulties, including the steps taken.

Easy way to download music playlists from YouTube

I’ve recently found a playlist I wanted to “git pull” from YouTube, but realize it would take too long to use tools such as Keepvid, or any other MP3 fetchers online. Then I recalled that there is this neat little tool called youtube-dl, and decided to learn how to use it in order to download MP3s from YouTube.
To my big surprise, it also allows us to download entire playlists.

 

As a result, I’ve spent some time to write down a short step by step guide for Windows, that should help us download entire youtube playlists, as well as individual MP3s from youtube videos.

 

I expect to append a similar guide for Linux in the same gist file, once I get the time.

Java SE8 Practice Question: ArrayList’s add and subList method

Practice Question, Oracle Certified Associate: Java SE8 Programming (1Z0-808)

Sometimes even the actual function of the easiest of methods (no pun intended) can elude us.
How well do you know the behavior of java.util.ArrayList’s add and subList method?

Q: What will the output be after code execution?

import java.util.ArrayList;
import java.util.List;

public class Demo {
	public static void main(String[] args) {
		List s1 = new ArrayList();
		s1.add("x");
		s1.add("y");
		s1.add(1, "z");
		List s2 = new ArrayList(s1.subList(2, 2));
		s1.addAll(s2);
		System.out.println(s1);
	}
}

Choose one answer:

  • a) [x, z, y]
  • b) [x, y, z]
  • c) [x, y, z, y]
  • d) [x, z, y, z]
  • e) None of the above

 

 

 

Click here to view answer

The answer is a).

Why?
List is an Interface that ArrayList have implemented. What add(int index, E element) does, is to insert an element into the given index. The existing element on the given index, along with the concurrent elements, will be pushed one step up.

import java.util.ArrayList;
import java.util.List;

public class Demo {
	public static void main(String[] args) {
		List s1 = new ArrayList(); // []
		s1.add("x"); // s1: [x]
		s1.add("y"); // s1: [x, y]
		s1.add(1, "z"); // s1: [x, z, y]
		List s2 = new ArrayList(s1.subList(2, 2)); // s1.sublist(2, 2) is an empty array.
		s1.addAll(s2); // nothing is added to s1.
		System.out.println(s1); // [x, z, y]
	}
}

The s1.sublist(2, 2) has no range. It returns an empty array.

The way subList(int fromIndex, int toIndex) works, is by giving it a range. A range from 2 to 2 is an empty range. The difference is 0, so no letters will be added.
For example, a range from 0 to 2 would return x, z from the example above, making it [x, z, y, x, z]. Because we have a range from and included 0, up to but not included 2.
Modify the existing code and take a look!