Skip to main content

Posts

Showing posts from April, 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...