Shell Programming – If…else, Array, Loops, Function…

Tram Ho

Hello, continue with the Shell programming series, this article we will learn about conditional statements, arrays, loops, functions …

1. Conditional statement

  • If you already know conditional statements in other programming languages, the same is true for Shell. It is different only in syntax.
  • There are 5 conditional statements in Shell programming:
    • if statement
    • if-else statement
    • if..elif..else..fi statement
    • if..then..else..if..then..fi..fi .. (Nested if)
    • switch statement

Syntax

If statement

if-else statement

if..elif..else..fi statement

if..then..else..if..then..fi..fi .. (Nested if)

switch statement

2. Array in Shell

  • In the previous article we learned about shell variables used to store a value, called a scalar variable. Shell also supports another variable called array variable . Variable naming rules also apply to array naming.

Array definition

  • Define: array_name[index]=value
    • array_name: The name of the array
    • index: is the index of the item in the array
    • value: value at that index
  • For example:

  • If we are using KornShell array initialization syntax: set -A array_name value1 value2 ... valuen
  • If we are using BashShell the syntax of initializing arrays: array_name=(value1 ... valuen)

Accessing array values

  • Syntax for accessing the value of an array: ${array_name[index]}
  • Consider the following example, create file test.sh :

  • Example results above:

  • We can also access all items in the array with the following syntax:

  • See the following example:

  • Result:

3. The loop

  • The loop is a powerful programming tool that allows us to execute a script on a continuous basis. We will explore the loops Shell supports for programmers:
    • The while loop
    • The for loop
    • The until loop
    • The select loop
  • It is possible to change the state of the loop using the break, continue statement as in other languages.
  • We can also use nested loops.

While loop

  • First, we will learn about the while loop. About using similar languages ​​like C ++, java …
  • Syntax:

  • Example: Print out integers smaller than 10.

  • Result:

For loop

  • The second loop we will find out is the For loop.
  • Syntax:

  • For example:

  • Result:

Until loop

  • A strange sounding name loop, let’s see how it is used.
  • Until loop allows to execute a script until a condition becomes true. This means that if the condition is false, then the script will be executed, and if the condition is true, then no statement will be executed. Here you can see the until loop is like a do … while loop in C ++, Java …
  • Until loop always executes at least 1 time.
  • Syntax:

  • Example: Displays numbers from 0 to 9

Select loop

  • Again a fairly new name.
  • The Select loop helps create a numbered menu in order, giving the user a choice.
  • Syntax:

  • For example:

  • Result:

4. Functions

  • Using functions to perform repetitive tasks is a great way to generate code reuse. This is an important part of modern object-oriented programming principles.

Creating Functions

  • For example:

Pass Parameters to a Function

  • We can define a function that will accept parameters while calling the function, the parameters will be denoted by $ 1, $ 2 …

Returning values ​​from Functions

  • To return a value from a function we use the return statement
  • Example: Returns the value 10

Note: The function must be defined before the location to call that function.

5. Shell Substitution

  • Shell performs the replacement when encountering special characters. For better understanding see the example below

  • The -e option allows executing backslash escapes Value of a is 10 . And this is the result without -e Value of a is 10n .
  • Some escapes are used in the echo command:
  • : backslash
  • a : alert
  • b : backspace
  • c : suppress trailing newline
  • f : form feed
  • n : newline
  • r : carriage return
  • t : horizontal tab
  • v : vertical tab
  • You can use the -E option to disable backslash, -n to disable newline.
  • Command substitution : The mechanism by which the shell executes a given script and then replaces its output where it is called.
  • Syntax: command . Use backquote, not single quote.
  • Consider the following example:

  • Result:

  • Variable Substitution : allows manipulating the value of a variable based on its state.
  • $ {var}: Replace the value of var
  • $ {var: -word}: If var is null or unset, word is substituted for var. The value of var does not change.
  • $ {var: = word}: If var is null or unset, var is set to the value of word.
  • $ {var:? message}: If var is null or unset, the message is printed as a standard error. This checks for variables that are set correctly.
  • $ {var: + word}: If var is set, word is substituted for var. The value of var does not change.
  • For example:

  • Result:

summary

  • At this point, we have the basic knowledge for Shell programming, depending on each problem we will apply.
  • Read more about Shell here
Share the news now

Source : Viblo