Introduction to named pipes on Bash shell

Introduction to named pipes on Bash shell

On Linux and Unix-based operating systems, pipes are very useful since they are a simple way to achieve IPC (inter-process communication). When we connect two processes in a pipeline, the output of the first one is used as the input of the second one. To build a so called “anonymous” pipe, all we have to do is to use the | operator. Anonymous, or unnamed pipes last just as long as the processes they connect. There is, however, another type of pipe we can use: a FIFO, or named pipe. In this article we will see how named pipes work and in what they are different from the standard pipes.

In this tutorial you will learn:

  • What is a named pipe
  • How to create a named pipe
  • How to recognize a named pipe
  • How named pipes work
  • How to delete a named pipe

Read more

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

Process List Management and Automatic Process Termination

Process List Management and Automatic Process Termination

As sever optimal use/maximization continues to grow, it becomes more and more important to manage processes well. One aspect of this is automatic process termination. When a process has gone rogue, and is consuming too much resources, it can be terminated automatically.

This is especially suited to servers which have a lot of temporary or disposable processes. It is also well suited for testing servers which are running many test trials and where such test trials prove to be unstable or cause the software under testing to behave erratically (for example by using too much memory)

In this tutorial you will learn:

  • How to manage processes in an automated fashion
  • Which resources you may want to monitor, and why
  • Example code showing how automatic process termination can work for memory hogging issues

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

Timing bash script execution

Time Your Bash Scripts and Procedures From Inside the Code

In general, one may use the time Bash utility (see man time for more information) to run a program, and obtain runtime duration and system resource usage summaries. But how can one time particular sections of code, directly from within the Bash source code?

Using some easy variable assignments and calculations, it is possible to achieve accurate timing metrics for Bash script executions.

In this tutorial you will learn:

  • How to time Bash scripts using variable assignments and calculations
  • How to use overlapping timers to time specific sections of your scripts
  • Examples which exemplify how specific sections of code can be timed

Read more

How to create modify and delete users account on Linux

How to create modify and delete users account on Linux

Users account management is one of the fundamental task of every Linux system administrator. In this article we will learn how to create a new user account, how to modify it and how to delete it from the command line using the useradd, usermod and userdel utilities, which are part of the base system.

In this tutorial you will learn:

  • What is the use of the /etc/login.defs file
  • How to create a user account with various options using the useradd command
  • How to modify a user account using the usermod command
  • How to delete a user account using the userdel command

Read more

How to Correctly Grep for Text in Bash Scripts

How to Correctly Grep for Text in Bash Scripts

grep is a versatile Linux utility, which can take a few years to master well. Even seasoned Linux engineers may make the mistake of assuming a given input text file will have a certain format. grep can also be used, directly in combination with if based searches to scan for the presence of a string within a given text file. Discover how to correctly grep for text independent of character sets, how to use the -q option to text for string presence, and more!

In this tutorial you will learn:

  • How to do correct character set-independent text searches with grep
  • How to use advanced grep statements from within scripts or terminal oneliner commands
  • How to test for string presence using the -q option to grep
  • Examples highlighting grep usage for these use cases

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

String concatenation in Bash

String concatenation in Bash

This tutorial will explain the Bash string concatenation by using examples. When it comes to bash scripting or programming in general, the concatenation refers to joining two or more string together to produce single unified output. Using Bash shell and bash scripting the string concatenation can be achieved in number for ways.

In this tutorial you will learn:

  • How to concatenate strings using echo command
  • How to concatenate strings in loop
  • How to concatenate string with command output

Read more

Big Data Manipulation for Fun and Profit Part 3

Big Data Manipulation for Fun and Profit Part 3

There have been two previous article in this series, which you may want to read first if you have not read them yet; Big Data Manipulation for Fun and Profit Part 1 and Big Data Manipulation for Fun and Profit Part 2.

In this series, we discuss various ideas and practical approaches for handling big data, or more specifically handling, transforming, mangling, munging, parsing, wrangling, transforming and manipulating the data at the Linux command line.

This third article in the series will continue explore Bash tools which can help us when processing and manipulating text-based (or in some cases binary) big data. As mentioned in the previous articles, data transformation in general is an semi-endless topic as there are hundreds of tools for each particular text format. Remember that at times using Bash tools may not be the best solution, as an off-the-shelf tool may do a better job. That said, this series is specifically for all those (many) other times when no tool is available to get your data in the format of your choice.

Finally, if you want to learn more about why big data manipulation can be both fun and profitable… please read Part 1 first.

In this tutorial you will learn:

  • Additional big data wrangling / parsing / handling / manipulation / transformation techniques
  • What Bash tools are available to assist you, specifically for text based applications
  • Various examples, showing different methods and approaches

Read more

How to list only work days using shell command line on Linux

The following article will explain a simple procedure on how to list work days ( business days ) on a Linux command line. Please note that the below procedure does not take into account a public holidays for your relevant country as it simply only shows word days while excluding weekends.

For this we will be using ncal command. Let’s start the complete workout by displaying a calendar for a current month start:

$ ncal -h
    August 2016       
Mo  1  8 15 22 29   
Tu  2  9 16 23 30   
We  3 10 17 24 31   
Th  4 11 18 25      
Fr  5 12 19 26      
Sa  6 13 20 27      
Su  7 14 21 28 

Read more

Useful Bash Command Line Tips and Tricks Examples - Part 6

Useful Bash Command Line Tips and Tricks Examples – Part 6

In this article we have a look at obtaining some basic hardware, system and operating system configuration information directly from the command prompt in your terminal. Having this information, and knowing where to find it, often helps when configuring a system, researching optimal hardware and software solutions, and generally being in control of the Linux system you own or manage.

We will focus mostly on every day information, the kind that is required on a regular basis. Going more in-depth often requires a few online searches to first define alignment better, and then often requires additional skills to interpret the results or output shown.

For example, we will not be looking at the output of vmstat, though we will discover how to find out what CPU is in your system without opening the hood. We will also look at some basic resource limit variables and surrounding concerns. The target audience for this article is thus beginner to medium advanced.

This article is part of the Useful Bash Command Line Tips and Tricks Series.

In this tutorial you will learn:

  • Useful Bash command line tips, tricks and methods
  • How to interact with the Bash command line in an advanced manner
  • How to sharpen your Bash skills overall and become a more proficient Bash user

Read more