Skip to main content

Posts

Showing posts from 2017

Unix - Shell 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 a = 10 echo - e "Value of a is $a \n" You will receive the following result. Here the -e option enables the interpretation of backslash escapes. Value of a is 10 Following is the result without -e option − Value of a is 10\n Here are following escape sequences which can be used in echo command − S.No. Escape & Description 1 \\ backslash 2 \a alert (BEL) 3 \b backspace 4 \c suppress trailing newline 5 \f form feed 6 \n new line 7 \r carriage return 8 \t horizontal tab 9 \v vertical tab You can use the -E option to disable the interpretation of the backslash escapes (default). You can use the -n option to disab...

Unix - Shell Decision Making, Shell Loop Types, Shell Loop Control.

Unix Shell supports conditional statements which are used to perform different actions based on different conditions. We will now understand two decision-making statements here − The if...else statement The case...esac statement The if...else statements If else statements are useful decision-making statements which can be used to select an option from a given set of options. Unix Shell supports following forms of if…else statement − if...fi statement if...else...fi statement if...elif...else...fi statement Most of the if statements check relations using relational operators discussed in the previous chapter. The case...esac Statement You can use multiple if...elif statements to perform a multiway branch. However, this is not always the best solution, especially when all of the branches depend on the value of a single variable. Unix Shell supports case...esac statement which handles exactly this situation, and it does so more efficiently than repeated ...

Unix - Shell Arrays

Shell supports a different type of variable called an array variable . This can hold multiple values at the same time. Arrays provide a method of grouping a set of variables. Instead of creating a new name for each variable that is required, you can use a single array variable that stores all the other variables. All the naming rules discussed for Shell Variables would be applicable while naming arrays. Defining Array Values The difference between an array variable and a scalar variable can be explained as follows. Suppose you are trying to represent the names of various students as a set of variables. Each of the individual variables is a scalar variable as follows − NAME01 = "Anurag" NAME02 = "Erra" NAME03 = "Ronny" NAME04 = "Joe" NAME05 = "Virat" We can use a single array to store all the above mentioned names. Following is the simplest method of creating an array variable. This helps assign a value to one of...

Unix - Shell Variables

A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables. Variable Names The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _). By convention, Unix shell variables will have their names in UPPERCASE. The following examples are valid variable names − _ALI TOKEN_A VAR_1 VAR_2 Following are the examples of invalid variable names − 2_VAR -VARIABLE VAR1-VAR2 VAR_A! The reason you cannot use other characters such as ! , * , or - is that these characters have a special meaning for the shell. Defining Variables Variables are defined as follows − variable_name=variable_value For example − NAME = "Zara Ali" The above example defines the variable NAME and assigns the value "Zara Ali" to it. Variables of this type are called scalar variables . A scalar variable can hold only one value at a time. Shell enables yo...

UNIX- Shells

A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output. Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions. Shell Prompt The prompt, $ , which is called the command prompt , is issued by the shell. While the prompt is displayed, you can type a command. Shell reads your input after you press Enter . It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words. Following is a simple example of the date command, which displays the current date and time − $date Sat Mar 25 08:30:19 MST 2017 You can customize your comman...

Unix - System Performance & System Logging

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 has following major resource types that need to be monitored and tuned − CPU Memory Disk space Communications lines I/O Time Network Time Applications programs Performance Components The following table lists out five major components which take up the system time − S.No. Component & Description 1 User State CPU The actual amount of time the CPU spends running the users’ program in the user state. It includes the time spent executing library calls, but does not include the time spent in the kernel on its behalf 2 System State CPU This is the amount of time the CPU spends in the system state on behalf of this program. All I/O routines require kernel services. The programmer can affect this value by blocking I/O transf...

Unix - User Administration

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. System accounts System accounts are those needed for the operation of system-specific components for example mail accounts and the sshd accounts. These accounts are usually needed for some specific function on your system, and any modifications to them could adversely affect the system. User accounts User accounts provide interactive access to the system for users and groups of users. General users are typically assigned to these accounts and usually have limited access to critical system files and directories. Unix supports a concept of Group Account which logically groups a number of accounts. Every account would be a part of another group account. A Unix group plays important role in han...

Unix - Network Communication Utilities

 When you work in a distributed environment, you need to communicate with remote users and you also need to access remote Unix machines. There are several Unix utilities that help users compute in a networked, distributed environment. This chapter lists a few of them. The ping Utility The ping command sends an echo request to a host available on the network. Using this command, you can check if your remote host is responding well or not. The ping command is useful for the following − Tracking and isolating hardware and software problems. Determining the status of the network and various foreign hosts. Testing, measuring, and managing networks. Syntax Following is the simple syntax to use the ping command − $ping hostname or ip-address The above command starts printing a response after every second. To come out of the command, you can terminate it by pressing CNTRL + C keys. Example Following is an example to check the availability of a host availab...

Unix - Pipes and Filters

To make a pipe, put a vertical bar ( | ) on the command line between two commands. When a program takes its input from another program, it performs some operation on that input, and writes the result to the standard output. It is referred to as a filter . The grep Command The grep command searches a file or files for lines that have a certain pattern. The syntax is − $grep pattern file(s) The name "grep" comes from the ed (a Unix line editor) command g/re/p which means “globally search for a regular expression and print all lines containing it”. A regular expression is either some plain text (a word, for example) and/or special characters used for pattern matching. The simplest use of grep is to look for a pattern consisting of a single word. It can be used in a pipe so that only those lines of the input files containing a given string are sent to the standard output. If you don't give grep a filename to read, it reads its standard input; that...

Unix Basic Utilities - Printing, Email

Printing Files Before you print a file on a Unix system, you may want to reformat it to adjust the margins, highlight some words, and so on. Most files can also be printed without reformatting, but the raw printout may not be that appealing. Many versions of Unix include two powerful text formatters, nroff and troff . The pr Command The pr command does minor formatting of files on the terminal screen or for a printer. For example, if you have a long list of names in a file, you can format it onscreen into two or more columns. Following is the syntax for the pr command − pr option(s) filename(s) The pr changes the format of the file only on the screen or on the printed copy; it doesn't modify the original file. Following table lists some pr options − S.No. Option & Description 1 -k Produces k columns of output 2 -d Double-spaces the output (not on all pr versions) 3 -h "header" Takes the next item as a report...

Unix - Environment

 An important Unix concept is the environment , which is defined by environment variables. Some are set by the system, others by you, yet others by the shell, or any program that loads another program. A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. For example, first we set a variable TEST and then we access its value using the echo command − $TEST = "Unix Programming" $echo $TEST It produces the following result. Unix Programming Note that the environment variables are set without using the $ sign but while accessing them we use the $ sign as prefix. These variables retain their values until we come out of the shell. When you log in to the system, the shell undergoes a phase called initialization to set up the environment. This is usually a two-step process that involves the shell reading the following files − /etc/profile profile The process...