Shell Scripts

Read Only Varibales

Shell provides a way to mark variables as read-only that could make its value cannot be changed. The execution of overriding a read-only variable, will show a message stating a variable name is a readonly.

Figure 1.5 – Readonly Variable

Conditional Statements

Conditions are expressions that evaluate to a boolean expression (true or false). To check conditions, we can use ifif-elseif-elif-else and nested conditionals. The structure of conditional statements is as follows:

  • if...then...fi statements
  • if...then...else...fi statements
  • if..elif..else..fi
  • if..then..else..if..then..fi..fi.. (Nested Conditionals)

There are known operator that could be used companion operator. Some of these operators are AND, OR, greater than, less than. This operators are represented in the following special expression

  • AND -a
  • OR -o
  • Greaer than -gt
  • Less than -lt
  • Equal -eq

Figure 1.6 – Conditional Statements

Looping and skipping

It is used to execute statements a specific number of times and perform the logic inside the loop. There are different types of loop statements. Some of the loop statements are the follow

  • For Loop
  • While Loop

For Loop – Numeric/Arithmetic Value

The application of a for loop applied for numeric as well as string values. We being to discuss for loop stating the application for numeric as well as string. The application of for loop statement considering a numeric valueas follow

Figure 1.7 – For Loop for Numeric Values

For Loop – String

This is a loop application for a string values The loop will iterate with the statement set of string, and access each of the string… the application is as folllow

Figure 1.8 – For Loop for String Values

While Loop

While loops check for a condition and loop until the condition remains true. We need to provide a counter statement that increments the counter to control loop execution. Let see (( i += 1 )) is the counter statement that increments the value of i

Figure 1.9 – Argument for Shell Script

Passing argument to scripts

It is possible to pass argument to a shell script that could let to take the argument and use it in the shell script. the application of argument is make using a symbol $@ which is staring the position of parameter starting from one.

Figure 2.0 – Argument for Shell Script

Scroll to Top