Objective

The objective is to use command line and bash shell script to obtain Sunrise & Sunset time information for any given location.

Operating System and Software Versions

  • Operating System: - Linux distribution agnostic.

Requirements

Installed lynx tool and access to shell command line. Your location code obtained from https://weather.codes/search/.

Difficulty

EASY

Conventions

Objective

Learn how to use the bash getopts builtin to parse a script options

Operating System and Software Versions

  • Operating System: - All Linux distributions

Requirements

  • No special requirements, just access to a bash shell

Difficulty

MEDIUM

Conventions

Introduction

The following post is a little bit different from my other posts as it does not solve any of your Linux configuration problems. Apologies there! This post is an attempt to kill multiple birds with one stone, hence expose children to a command line, avoid web flash and ads driven multiplication practice tests online and most importantly gain some multiplication skills along the way.

My daughter reached the age where she is required to perform multiplication calculation at school. So together we have developed a below Bash script to take her 1x1 to 10x10 multiplication tables practice to the next level. She approved all the output colours with a "cool" mark, and to my surprise, she was very quickly able to correct my forgotten closing quotation marks during the development thus significantly improving development efficiency:-)

Objective

Install ZSH with Oh My ZSH and learn the basic features.

Distributions

ZSH is available in the repositories of nearly every distribution.

Requirements

A working Linux install with root privileges.

Difficulty

Easy

Conventions

  • # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - requires given linux commands to be executed as a regular non-privileged user

Introduction

Bash isn't bad. It gets the job done just fine, but have you ever considered what it'd be like if Bash had some extra features to make it more convenient to work with? That's more-or-less what ZSH is.

It includes all of the features that you'd expect from Bash, but it also has some really nice additions to make your life easier. Actually, you'll be amazed at how much easier they make working in the command line.

Objective

Learn to use redirections, pipes and tee in the Bash shell

Operating System and Software Versions

  • Operating System: - Linux distribution agnostic

Requirements

  • Access to a Bash shell

Difficulty

EASY

Conventions

Introduction

Python will let you know when you get your syntax wrong. It'll immediately fail and not allow your program to run.

What about when your code has a different type of problem? Those are called exceptions, and they tend to be harder to catch. It's up to you to recognize situations where hey might come up and catch them to prevent your program from crashing altogether.

Imagine a scenario where you need user input. Do you want your program to crash every time a user mistypes something or enters something erroneous? That's far from ideal. Since you know there could be a problem there, you can tell Python to look out for one, and recover gracefully.

Introduction

Python is a powerful scripting language. So, why not use it to script Linux? The os module is Python's answer to handling many Linux file operations.

The os module allows Python to perform many of the file and folder operations that you'd typically carry out in the Linux command line. It enable you to begin swapping out Bash for Python, which makes for a much cleaner and friendlier scripting experience.

Loading OS

The os module is a Python module like any other. In any script where you want to use it, you can use an import statement to pull it in.
import os

getcwd()

The getcwd() method returns the current working directory in the form of a string. You don't need to pass it anything. It's roughly the equivalent of pwd.
print(os.getcwd())

Introduction

You've worked with both functions and methods, but there's a different type of function, the anonymous function. Anonymous functions are just functions without names, and in Python, they are handled with the lambda operator.

Anonymous functions allow you to use the behavior of a function without having to create a function. This is especially good when you only need to use a function once or twice. It might sound like an unusual occurrence, but it happens fairly frequently when a function or method takes a function as a parameter.

Introduction

Jut about any program of a decent size needs to be able to read and write from files. At very least, it needs to be able to generate logs. Python is also tightly integrated into Linux system administration and scripting. Again, reading and writing are important for managing a system.

Introduction

Whenever you need some additional functionality in Python, you turn to the import keyword to pull in extras from Python modules. You've used common ones like the math module plenty of times.

Now, you will learn how to create your own Python modules and packages to compartmentalize your code. Modules are sort of like classes in that they make your code modular. While classes make code modular within a program and serve as the blueprints for objects, modules make all of your code modular across all programs and are utilities to be used just as they are.

Through the use of modules, you can create your own toolbox with all sorts of parts and pieces that you commonly use. Modules can include anything from variables and constants to functions and even classes. Because of this versatility, you can set yourself up to have everything that you need at the beginning of any project.

Introduction

Polymorphism is yet another important aspect of Object Oriented Programming. No Warcraft fans, it doesn't have anything to do with turning people into sheep. That'd be much cooler. Instead, Polymorphism allows methods and functions to use classes with similar functionality in the same way.

Polymorphism cuts down on the amount of code that you need to write by eliminating redundancy in a logical and meaningful way. It relies on you, the programmer, to be clever in your design and harness similarities in objects.

Introduction

Inheritance is yet another key concept in Object Oriented Programming, and it plays a vital role in building classes. It allows a class to be based off on an existing one.

When you first started writing Python classes, you were told to just put "Object" in the parenthesis of the class definition and not think too much about it. Well, now's the time to start thinking about it.

"Object" is actually the base class that all Python classes inherit from. It defines a basic set of functionality that all Python classes should have. By inheriting from it when you create a new class, you ensure that that class has that basic functionality.

In short, inheritance is a nice way of categorizing classes and making sure that you don't needlessly repeat yourself.

Introduction

Encapsulation is one of the fundamental aspects of Object Oriented Programming. It allows programmers better control of how data flows in their programs, and it protects that data. Encapsulation also makes objects into more self-sufficient and independently functioning pieces.

The concept of encapsulation builds on what you did in the last two guides with classes and constructors. Constructors usually are usually used in close conjunction with encapsulation and actually aid in making encapsulation work seamlessly.

Introduction

By now, you should be familiar with the way basic classes work in Python. If classes were just what you've seen, they'd be fairly rigid and not all that useful.

Thankfully, classes are much more than just that. They are designed to be much more adaptable and can take in information to shape the way they look initially. Not every car starts off exactly the same, and neither should classes. After all, how awful would it be if every car was an orange 71' Ford Pinto? That's not a good situation.

Writing A Class

Start off by setting up a class like the one in the last guide. This class will evolve over the course of this guide. It will move from being a rigid, photocopy-like, situation to a template that can generate multiple unique objects within the outline of the class.

Write the first line of the class, defining it as a class and naming it. This guide is going to stick with the car analogy from before. Don't forget to pass your class object so that it extends the base object class.

Introduction

Classes are the cornerstone of Object Oriented Programming. They are the blueprints used to create objects. And, as the name suggests, all of Object Oriented Programming centers around the use of objects to build programs.

You don't write objects, not really. They are created, or instantiated, in a program using a class as their basis. So, you design objects by writing classes. That means that the most important part of understanding Object Oriented Programming is understanding what classes are and how they work.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: