Tuesday, April 4, 2017

Unix - Shell Substitution

What is Substitution?

The shell performs substitution when it encounters an expression that contains one or more special characters.

Example

Here, the printing value of the variable is substituted by its value. Same time, "\n" is substituted by a new line −
#!/bin/sh

Unix - Shell Quoting Mechanisms

In this chapter, we will discuss in detail about the Shell quoting mechanisms. We will start by discussing the metacharacters.

The Metacharacters

Unix Shell provides various metacharacters which have special meaning while using them in any Shell Script and causes termination of a word unless quoted.

Unix - Shell Input/Output Redirections

In this chapter, we will discuss in detail about the Shell input/output redirections. Most Unix system commands take input from your terminal and send the resulting output back to your terminal. A command normally reads its input from the standard input, which happens to be your terminal by default. Similarly, a command normally writes its output to standard output, which is again your terminal by default.

Unix - Shell Functions

In this chapter, we will discuss in detail about the shell functions. Functions enable you to break down the overall functionality of a script into smaller, logical subsections, which can then be called upon to perform their individual tasks when needed.
Using functions to perform repetitive tasks is an excellent way to create code reuse. This is an important part of modern object-oriented programming principles.

Unix - Shell Manpage Help

All the Unix commands come with a number of optional and mandatory options. It is very common to forget the complete syntax of these commands.
Because no one can possibly remember every Unix command and all its options, we have online help available to mitigate this right from when Unix was at its development stage.

Unix - Regular Expressions with SED

In this chapter, we will discuss in detail about regular expressions with SED in Unix.
A regular expression is a string that can be used to describe several sequences of characters. Regular expressions are used by several different Unix commands, including ed, sed, awk, grep, and to a more limited extent, vi.

Unix - File System Basics

A file system is a logical collection of files on a partition or disk. A partition is a container for information and can span an entire hard drive if desired.
Your hard drive can have various partitions which usually contain only one file system, such as one file system housing the /file system or another containing the /home file system.

Unix - User Administration

In this chapter, we will discuss in detail about user administration in Unix.
There are three types of accounts on a Unix system −

Root account

This is also called superuser and would have complete and unfettered control of the system. A superuser can run any commands without any restriction. This user should be assumed as a system administrator.

Unix - System Performance

In this chapter, we will discuss in detail about the system performance in Unix.
We will introduce you to a few free tools that are available to monitor and manage performance on Unix systems. These tools also provide guidelines on how to diagnose and fix performance problems in the Unix environment.

Unix - System Logging

In this chapter, we will discuss in detail about system logging in Unix.
Unix systems have a very flexible and powerful logging system, which enables you to record almost anything you can imagine and then manipulate the logs to retrieve the information you require.
Many versions of Unix provide a general-purpose logging facility called syslog. Individual programs that need to have information logged, send the information to syslog.

Unix - Signals and Traps

In this chapter, we will discuss in detail about Signals and Traps in Unix.
Signals are software interrupts sent to a program to indicate that an important event has occurred. The events can vary from user requests to illegal memory access errors. Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the usual flow of control.
The following table lists out common signals you might encounter and want to use in your programs −

Unix Questions and Answers

Unix Questions and Answers has been designed with a special intention of helping students and professionals preparing for various Certification Exams and Job Interviews. This section provides a useful collection of sample Interview Questions and Multiple Choice Questions (MCQs) and their answers with appropriate explanations.

Unix - Useful Commands

This quick guide lists commands, including a syntax and a brief description. For more detail, use −
$man command

Files and Directories

These commands allow you to create directories and handle files.

Unix - Quick Guide

Unix - Getting Started

What is Unix ?

The Unix operating system is a set of programs that act as a link between the computer and the user.
The computer programs that allocate the system resources and coordinate all the details of the computer's internals is called the operating system or the kernel.
Users communicate with the kernel through a program known as the shell. The shell is a command line interpreter; it translates commands entered by the user and converts them into a language that is understood by the kernel.

Unix - Shell Builtin Mathematical Functions

The most of the part of this tutorial covered Bourne Shell but this page list down all the mathematical builti-in functions available in Korn Shell.
The Korn shell provides access to the standard set of mathematical functions. They are called using C function call syntax.

Unix, Linux System Call

Unix, Linux Commands




Advertisements

List of Unix, Linux Commands
[a] [b] [c] [d] [e] [f] [g] [h] [i] [j] [k] [l] [m]
[n] [o] [p] [q] [r] [s] [t] [u] [v] [w] [x] [y] [z]
  accept
accton
acpid
addftinfo
addpart
addr2line

Unix - Shell Useful Resources

The following resources contain additional information on Unix. Please use them to get more in-depth knowledge on this topic.

Useful Links on Unix

  • Bell Labs − The Creation of the UNIX Operating System. Gives overview and history of UNIX operating system.

Discuss Unix

Unix is a computer Operating System which is capable of handling activities from multiple users at the same time. The development of Unix started around 1969 at AT&T Bell Labs by Ken Thompson and Dennis Ritchie. This tutorial gives a very good understanding on Unix.

Tcl - Overview

Tcl is shortened form of Tool Command Language. John Ousterhout of the University of California, Berkeley, designed it. It is a combination of a scripting language and its own interpreter that gets embedded to the application, we develop with it.

Tcl - Environment Setup

Try it Option Online

We have set up the Tcl/Tk Programming environment online, so that you can compile and execute all the available examples online. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it online.

Tcl - Special Variables

In Tcl, we classify some of the variables as special variables and they have a predefined usage/functionality. The list of specials variables is listed below.
S.No. Special Variable & Description
1 argc
Refers to a number of command-line arguments.
2 argv
Refers to the list containing the command-line arguments.

Tcl - Basic Syntax

Tcl is quite simple to learn and let's start creating our first Tcl program!

First Tcl Program

Let us write a simple Tcl program. All Tcl files will have an extension, i.e., .tcl. So, put the following source code in a test.tcl file.
#!/usr/bin/tclsh

Tcl - Commands

As you know, Tcl is a Tool command language, commands are the most vital part of the language. Tcl commands are built in-to the language with each having its own predefined function. These commands form the reserved words of the language and cannot be used for other variable naming. The advantage with these Tcl commands is that, you can define your own implementation for any of these commands to replace the original built-in functionality.

Tcl - Data Types

The primitive data-type of Tcl is string and often we can find quotes on Tcl as string only language. These primitive data-types in turn create composite data-types for list and associative array. In Tcl, data-types can represent not only the simple Tcl objects, but also can represent complex objects such as handles, graphic objects (mostly widgets), and I/O channels. Let's look into the details about each of the above.

Tcl - Variables

In Tcl, there is no concept of variable declaration. Once, a new variable name is encountered, Tcl will define a new variable.

Variable Naming

The name of variables can contain any characters and length. You can even have white spaces by enclosing the variable in curly braces, but it is not preferred.
The set command is used for assigning value to a variable. The syntax for set command is,
set variableName value

Tcl - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Tcl language is rich in built-in operators and provides the following types of operators −
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary Operator

Tcl - Decisions

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the programming languages −

Tcl - Loops

There may be a situation, where you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Tcl - Arrays

An array is a systematic arrangement of a group of elements using indices. The syntax for the conventional array is shown below.
set ArrayName(Index) value
An example for creating simple array is shown below.
#!/usr/bin/tclsh

Tcl - Strings

The primitive data-type of Tcl is string and often we can find quotes on Tcl as string only language. These strings can contain alphanumeric character, just numbers, Boolean, or even binary data. Tcl uses 16 bit unicode characters and alphanumeric characters can contain letters including non-Latin characters, number or punctuation.

Tcl - Lists

List is one of the basic data-type available in Tcl. It is used for representing an ordered collection of items. It can include different types of items in the same list. Further, a list can contain another list.
An important thing that needs to be noted is that these lists are represented as strings completely and processed to form individual items when required. So, avoid large lists and in such cases; use array.

Tcl - Dictionary

A dictionary is an arrangement for mapping values to keys. The syntax for the conventional dictionary is shown below −
dict set dictname key value
# or 
dict create dictname key1 value1 key2 value2 .. keyn valuen
Some examples for creating a dictionary are shown below −
#!/usr/bin/tclsh

Tcl - Procedures

Procedures are nothing but code blocks with series of commands that provide a specific reusable functionality. It is used to avoid same code being repeated in multiple locations. Procedures are equivalent to the functions used in many programming languages and are made available in Tcl with the help of proc command.

Tcl - Packages

Packages are used for creating reusable units of code. A package consists of a collection of files that provide specific functionality. This collection of files is identified by a package name and can have multiple versions of same files. The package can be a collection of Tcl scripts, binary library, or a combination of both.
Package uses the concept of namespace to avoid collision of variable names and procedure names. Check out more in our next 'namespace' tutorial.

Tcl - Namespaces

Namespace is a container for set of identifiers that is used to group variables and procedures. Namespaces are available from Tcl version 8.0. Before the introduction of the namespaces, there was single global scope. Now with namespaces, we have additional partitions of global scope.

Tcl - File I/O

Tcl supports file handling with the help of the built in commands open, read, puts, gets, and close.
A file represents a sequence of bytes, does not matter if it is a text file or binary file.

Opening Files

Tcl uses the open command to open files in Tcl. The syntax for opening a file is as follows −
open fileName accessMode

Tcl - Error Handling

Error handling in Tcl is provided with the help of error and catch commands. The syntax for each of these commands is shown below.

Error syntax

error message info code
In the above error command syntax, message is the error message, info is set in the global variable errorInfo and code is set in the global variable errorCode.

Tcl - Built-in Functions

Tcl provides a number of built-in functions (procedures) for various operations. This includes −
  • Functions for list handling.
  • Functions for string handling.
  • Functions for array handling.
  • Functions for dictionary handling.
  • Functions for File I/O handling.
  • Functions for creating namespaces and packages.
  • Functions for Math operations.
  • Functions for System operations.

Tcl - Regular Expressions

The "regexp" command is used to match a regular expression in Tcl. A regular expression is a sequence of characters that contains a search pattern. It consists of multiple rules and the following table explains these rules and corresponding use.

Tk - Overview

Tk refers to Toolkit and it provides cross platform GUI widgets, which helps you in building a Graphical User Interface. It was developed as an extension to Tcl scripting language by John Ousterhout. Tk remained in development independently from Tcl with version being different to each other, before, it was made in sync with Tcl in v8.0.

Tk - Environment

Generally, all Mac and Linux mac come with Tk pre-installed. In case, it's not available or you need the latest version, then you may need to install it. Windows don't come with Tcl/Tk and you may need to use its specific binary to install it.

Tk - Special Variables

In Tk, we classify some of the variables as special variables and they have a predefined usage/functionality. The list of special variables is listed below.
S.No. Special Variable & Description
1 tk_library
Used for setting the location of standard Tk libraries.

Tk - Widgets Overview

The basic component of a Tk-based application is called a widget. A component is also sometimes called a window, since, in Tk, "window" and "widget" are often used interchangeably. Tk is a package that provides a rich set of graphical components for creating graphical applications with Tcl.

Tk - Basic Widgets

Basic widgets are common widgets available in almost all Tk applications. The list of available basic widgets is given below −
S.No. Widgets & Description
1 Label Widget for displaying single line of text.
2 Button Widget that is clickable and triggers an action.

Tk - Layout Widgets

Layout widgets are used to handle layouts for the Tk application. Frame widget is used group other widgets and place, pack, and grid are layout manager to give you total control over your adding to windows. The list of available layout widgets are as shown below −

Tk - Selection Widgets

Selection widgets are used to select different options in a Tk application. The list of available selection widgets are as shown below.
S.No. Widgets & Description
1 Radiobutton Widget that has a set of on/off buttons and labels, one of which may be selected.

Tk - Canvas Widgets

Canvas is used for providing drawing areas. The syntax for canvas widget is shown below −
canvas canvasName options

Options

The options available for the canvas widget are listed below in the following table −
S.No. Syntax & Description
1 -background color
Used to set background color for widget.

Tk - Mega Widgets

Mega widgets include many complex widgets which is often required in some large scale Tk applications. The list of available mega widgets are as shown below −
S.No. Widget & Description
1 Dialog Widget for displaying dialog boxes.
2 Spinbox Widget that allows users to choose numbers.

Tk - Fonts

There are a number of widgets that supports displaying text. Most of these provides the option of font attribute. The syntax for creating a font is shown below −
font create fontName options

Tk - Images

The image widget is used to create and manipulate images. The syntax for creating image is as follows −
image create type name options
In the above syntax type is photo or bitmap and name is the image identifier.

Tk - Events

Events in its simplest form is handled with the help of commands. A simple example for event handling is event handling with button and is shown below −
#!/usr/bin/wish

proc myEvent { } {
   puts "Event triggered"
}
pack [button .myButton1  -text "Button 1"   -command myEvent]
When we run the above program, we will get the following output −

Tk - Windows Manager

Window manager is used to handle the top level window. It helps in controlling the size, position, and other attributes of the window. In Tk, . is used to refer the main window. The syntax for window command is shown below −
wm option window arguments
The list of options available for Tk wm command is shown in the following table −

Tk - Geometry Manager

The geometry manager is used to manage the geometry of the window and other frames. We can use it to handle the position and size of the window and frames. The layout widgets are used for this purpose.

Tcl-Tk - Quick Guide

Tcl - Overview

Tcl is shortened form of Tool Command Language. John Ousterhout of the University of California, Berkeley, designed it. It is a combination of a scripting language and its own interpreter that gets embedded to the application, we develop with it.

Tcl-Tk - Useful Resources

The following resources contain additional information on Tcl-Tk. Please use them to get more in-depth knowledge on this topic.

Discuss Tcl-Tk

Tcl is a general purpose multi-paradigm system programming language. It is a scripting language that aims at providing the ability for applications to communicate with each other.
On the other hand, Tk is a cross platform widget toolkit used for building GUI in many languages.
This tutorial covers various topics ranging from the basics of the Tcl/Tk to its scope in various applications.