How to Use Bash Subshells Inside if Statements

How to Use Bash Subshells Inside if Statements

If you have ever used Bash subshells ($(...)), you know how flexible subshells can be. It only takes a few characters to start a subshell to process anything required, inline to another statement. The number of possible use cases is virtually unlimited.

We can also use Bash subshells inside if statements, inline with the statement. Doing so gives the user and developer much additional flexibility when it comes to writing Bash if statements.

If you are not familiar yet (or would like to learn more about) Bash if statements, please see our Bash If Statements: If Elif Else Then Fi article.

In this tutorial you will learn:

  • How to incorporate Bash subshells inside if statements
  • Advanced methods to incorporate Bash subshells inline with other commands
  • Examples demonstrating the use of Bash subshells in if statements

Read more

Bash if Statements: if, elif, else, then, fi

Bash if Statements: if, elif, else, then, fi

If you are just starting to explore the Bash coding language, you will soon find yourself wanting to create conditional statements. Conditional statements, in other words, define ‘if a condition is true or false, then do this or that, and if the opposite is true, do something else’. This is the most basic function of any conditional statement.

This article will introduce you to the five basic if statement clauses. being if, elif, else, then and fi. The first simply opens a if statement, the then introduces the what commands to execute if the statement condition was true section and the else introduces the what commands to execute if the statement condition was false section. Finally, the fi closes the statement. We also have the special elif on which we will see more in a minute. Let’s start with an easy example.

In this tutorial you will learn:

  • How to implement an if statement at the Bash command line
  • How such if statements can also be used inside a Bash scripts
  • Examples showing you the if, elif, else, then and fi clauses in Bash

Read more

How to launch external processes with Python and the subprocess module

How to launch external processes with Python and the subprocess module

In our automation scripts we often need to launch and monitor external programs to accomplish our desired tasks. When working with Python, we can use the subprocess module to perform said operations. This module is part of the programming language standard library. In this tutorial we will take a quick look at it, and we will learn the basics of its usage.

In this tutorial you will learn:

  • How to use the “run” function to spawn an external process
  • How to capture a process standard output and standard error
  • How to check the exist status of a process and raise an exception if it fails
  • How to execute a process into an intermediary shell
  • How to set a timeout for a process
  • How to use the Popen class directly to pipe two processes

Read more

Bash Advanced Variable Idioms for Case Sensitivity Management

Bash Advanced Variable Idioms for Case Sensitivity Management

Whenever we work with textual strings, sooner or later the issue of case comes up. Does a word need to be fully uppercase, fully lowercase, with a capitalized letter at the start of the word or sentence, and so on.

An idiom is a natural language expression of a simple programming task. For example, in the sleep 10 command (which will pause the terminal one is working in for ten seconds), the word sleep is a natural language expression of what is a time based coding construct, developed in the Bash GNU coreutils software package.

There are a number of special variable-bound idioms (i.e. suffixes which can be added to a variable name, indicating what we would like to do with a given variable), which can be used in Bash to more easily do these types of conversions on the fly instead of having to use for example the Sed Stream Editor with a Regular Expression to do the same.

If you are interested in using regular expressions, have a look at our Bash Regexps For Beginners With Examples Advanced Bash Regex With Examples articles!

This makes working with variables that need case modification, or if statement testing a whole lot easier and provides great flexibility. Idioms can be added directly inside the if statements and do not need to employ a subshell with sed.

While the syntax looks slightly complex to start with, once you learn a little mental support trick to remember the right keys, you will be well on your way to use these idioms in your next script or Bash one-liner script at the command line!

In this tutorial you will learn:

  • How to use the ^, ^^, , and ,, Bash variable suffix idioms
  • How to use a regular expression [] range idiom in combination with these
  • How to use the ^ and , idioms directly from within if statements
  • Detailed examples exemplifying the use of ^, ^^, , and ,,

Read more

How to read and create csv files using Python

How to read and create csv files using Python

CSV is the acronym of “Comma Separated Values”. A csv file is a just plain text document used to represent and exchange tabular data. Each row in a csv file represents an “entity”, and each column represents an attribute of it. Columns are usually separated by a comma but other characters can be used as field separator instead of it. In this tutorial we will see how to read and create csv files using Python and specifically the csv module, which is part of the
language standard library.

In this tutorial you will learn:

  • How to read csv rows as a list of strings
  • How to read a csv as a list of dictionaries
  • How to create a csv using Python
  • How to create a csv starting from a list of dictionaries

Read more

Customizing emacs for development

Introduction

First and foremost, a word of warning: while the previous articles were centered around the beginner, this article is for more advanced users, that already “speak” a programming language or two, and want to customize their editor towards it being ideal for the task. So you are expected to be more or less proficient in the ways of emacs, to be able to use it for day to day tasks and have the ability and desire to learn something new. After all, it will be for your own reward, and your programming tasks will become more efficient. Our approach will be to take a few popular languages, show you how to configure emacs for the perfect development environment, then move on to the next language. Emacs configuration is made in a Lisp dialect called Elisp, but don’t worry if you don’t know it yet, we’ll tell you what you need.

The languages

First, some background. This article is about emacs, not about any derivative like mg or jed that might or might not offer the desired functionality. That’s because many derivatives were born from the need of creating a smaller emacs, since the original is pretty big, admittedly. So in the process of removing functionality there might just be just some functionality that’s getting removed and we probably will need here. In short, emacs-only. Second, the files. In our examples, besides customizing the ~/.emacs file, we will create a directory named ~/.emacs.d/ where we will place our modes. Just as emacs knows what kind of syntax highlighting, indentation, etc. to use for several types of text, like HTML, TeX, C source code, and others, via modes, we can add/modify modes to our liking, to this is what we’ll do. Practically speaking, a mode is a file with a .el extension (from Elisp) that will be dropped in ~/.emacs.d, then ~/.emacs will be altered for the editor to “know” about the new extension. You’ll see in a few moments, depending on how fast you read.

Read more

Gnu r plot example

Introduction to GNU R on Linux Operating System

Introduction

This article will deal mainly with the installation of R on Linux, but also will provide a simple example on how to use R for plotting. This is the first article of the series of R articles so subscribe to our RSS feed for regular updates. Everyone, who is interested in using R for their work or is simply interested in this software is invited to follow this series of articles. The main objective of these articles is to provide a quick reference to R with illustrative examples.

What is GNU R?

R is an open source programming language (software package) and environment used mainly for statistical data analysis. It is licensed under the GNU General Public License (GPL). R is a very intuitive programming language. You can do in a few lines of R code a lot, mainly because there is a large number of packages available for R, which means a large number of preprogrammed functions for you to use. You can get R packages through Comprehensive R Archive Network (CRAN).

R’s strengths are: graphical visualization of data such as plots, data analysis, statistical data fits.

R’s weaknesses are: complex structured data storage, querying data, dealing with large data sets, which do not fit in the computer’s memory.

Installing GNU R on Linux/Unix.

Package Management System

Debian / Ubuntu / Mint

On Debian like Linux systems such as Debian, Ubuntu or Linux Mint you can install R from standard repositories. This is a preferred way of getting R installed on your system. The command bellow will download and install R along with all its prerequisites:

$ sudo apt-get install r-base

Read more

C program compilation process

C development on Linux – Introduction – I.

Introduction

What you’re just reading is the beginning of series of articles dedicated to development on Linux systems. However, with minor modifications (if any), you will be able to use this knowledge you will get by reading our series on any other system that uses the same tools (OpenIndiana, BSD…). This first article will deal gradually with the intricacies of writing C code on Linux. You are expected to have basic programming knowledge, either on Linux/Unix systems or on other platforms. The idea is that you should know the basics of programming, like what a variable is or how to define a structure. Even though, you will get this information from this article, we won’t insist very much on beginner-level concepts. A word of warning: we won’t include everything there is to tell about C, because that would take lots of space and of course, we don’t actually know everything about C.

Why C?

Some of you might argue that C is not the best beginner-level language ever. Agreed, but again, you’re expected to have some basic programming knowledge, for one. Second, C and Unix, and Linux afterwards, are intimately tied together that it only seemed natural to start our development series with C. From the kernel, of which a substantial part of it is written in C, to lots of everyday user-land applications, C is used massively on your Linux system. For example, GTK is based on C, so if you’re using Gnome or XFCE applications, you’re using C-based applications. C is an old, well-established programming language, vital tool in many parts in the IT world, from embedded systems to mainframes. Therefore, it is only fair to assume that C skills will not only enrich your CV, but they will also help you to solve many issues on your Linux system, that is only if you take this seriously and practice a lot by reading and writing C code.

Read more

C development on Linux – Types, variables, operators – III.

Introduction

As promised, starting with this part of our C development article, we will get started with learning, without further introduction. I couldn’t find no better way to start other than this, because types, operators and variables are an essential part of C and you will use them all the time when writing your own programs. For example, you can write a simple C program without defining your own functions, but it’s harder to do that without some variables, unless you wanna stick to “Hello, world!”. A variable is nothing more than a location in memory holding a value that can be altered (hence the name). But before you declare a variable you must know what kind of value you want it to hold, and here you will use types. And in order to operate on those variables, you’ll need…operators, of course. I intend to make this course as concise as possible, so I recommend attention and as usual, practice.

Read more

A quick GNU R tutorial to basic operations, functions and data structures

Introduction

In the last two articles we have learned how to install and run GNU R on the Linux operating system. The purpose of this article is to provide a quick reference tutorial to GNU R that contains introduction to the main objects of the R programming language . We will learn about basic operations in R, functions and variables. Moreover, we will introduce R data structures, objects and classes.

Basic Operations in R

Let us start with a simple mathematical example. Enter, for instance, addition of seven and three into your R console and press enter, as a result we obtain:

> 7+3
[1] 10

To explain in more detail what just happened and what is the terminology we use when running R, we say that the R interpreter printed an object returned by an expression entered into the R console. We should also mention that R interprets any number as a vector. Therefore, “[1]” near our result means that the index of the first value displayed in the given row is one. This can be further clarified by defining a longer vector using the c() function. For example:

Read more

while loop interactive C

C development on Linux – Flow Control – IV.

Introduction

You have already been exposed to a small part of what flow control is in our previous part, namely the section about relational operators. As you start writing more complex programs, you will feel the need to control the order in which your program executes various parts.
Flow control is present in most programming languages in one form or another, and what you’re about to read here is essential to writing C programs.

if/else/else if

This part of flow control is probably the most intuitive and simpler, although you can easily fall to the dark side and start writing unintelligible code with ifs. The idea is simple: if (condition_is_true) do_something; else do_something_else; . So it’s all about logic, binary logic that is, in which an expression can have two values: true or false. If you used C or Java, you’re used with the bool datatype. A bool variable can be only true or only false at a given moment. But C, although it doesn’t have the bool datatype, makes it easy to deal with binary logic, as you will see.

Read more

example scatter plot GNU R

A quick GNU R tutorial to statistical models and graphics

Introduction

In this quick GNU R tutorial to statistical models and graphics we will provide a simple linear regression example and learn how to perform such basic statistical analysis of data. This analysis will be accompanied by graphical examples, which will take us closer to producing plots and charts with GNU R. If you are not familiar with using R at all please have a look at the prerequisite tutorial: A quick GNU R tutorial to basic operations, functions and data structures.

Models and Formulas in R

We understand a model in statistics as a concise description of data. Such presentation of data is usually exhibited with a mathematical formula. R has its own way to represent relationships between variables. For instance, the following relationship y=c0+c1x1+c2x2+…+cnxn+r is in R written as

y~x1+x2+...+xn,

which is a formula object.

Linear regression example

Let us now provide a linear regression example for GNU R, which consists of two parts. In the first part of this example we will study a relationship between the financial index returns denominated in the US dollar and such returns denominated in the Canadian dollar. Additionally in the second part of the example we add one more variable to our analysis, which are returns of the index denominated in Euro.

Read more