Day 5: Few more concepts on Shell Scripting

Day 5: Few more concepts on Shell Scripting

Hi Everyone!

Please find below concepts on shell scripting. (continuation)

Shift Command

Generally, arguments will take from $1 to $9. ($0 gives filename).

if we give $10, it won't consider the 10th argument instead go for the first argument.

So you can give them {} braces

or use shift command as explained below.

By using the shift command it is possible to reference individual CLI parameters beyond $9.

Shift command move to one argument ahead.

Ex: If we give a, b, c arguments, it won't consider the first argument but move to the second one.

shift -- move/points to the second argument (If you don't mention any number after shift, it considers as '1')

shift 2 -- move to the third argument

shift 3 -- move to the fourth argument and so on.

Note: So in order to consider the first argument make sure you either use this first value (or)

Store in another variable like first = $1

Otherwise, it will be lost.

An Example script:

Here $@ holds all the arguments. If we shift 3 then it will consider from 4th argument and prints four , five , six

#!/bin/bash
echo "Input: $@"
shift 3
echo "After shift: $@"

Run it:

$ myscript.sh one two three four five six

Input: one two three four five six
After shift: four five six

Shift in while


Types of Operators

Arithmetic Operations:

In the shell script, all variables hold string values even if they are numbers. So, to perform arithmetic operations we use the expr command.

The expr command can only work with integer values. For floating point numbers we use the bc command.

To compute the result we enclose the expression in backticks

Examples: For Addition

#!/bin/sh

take two integers from the user

echo "Enter two integers: " read a b

perform addition

result=expr $a + $b

show result

echo "Result: $result"


Floating:

In the following example we are enclosing the variables in double quotes and using bc to handle floating point numbers.

#!/bin/sh

take two numbers from the user

echo "Enter two numbers: " read a b

perform addition

result=expr "$a + $b" | bc

show result

echo "Result: $result"


Relational Operators:

if(( $a==$b ))

then

echo "a is equal to b."

else

echo "a is not equal to b."

fi


Boolean Operators:


String Operators:

String Operators | Shell Script - GeeksforGeeks

File Test operators:

Unix / Linux - Shell File Test Operators Example (tutorialspoint.com)


Functions

Unix / Linux - Shell Functions (tutorialspoint.com)

Creating Functions:

functionname () {

}

#!/bin/sh

# Define your function here
Hello () {
   echo "Hello Aasifa"
}

# Invoke your function
Hello
$./test.sh
Hello Aasifa

Passing Parameters to the Functions:

#!/bin/sh

# Define your function here
Hello () {
   echo "Hello World $1 $2"
}

# Invoke your function
Hello Aasifa Shaik
$./test.sh
Hello World Aasifa Shaik

Returning Values from the functions:

$? returns the exit value of the last executed command.

#!/bin/sh

# Define your function here
Hello () {
   #echo "Hello World $1 $2"
   return 10
}

# Invoke your function
Hello Aasifa shaik

# Capture value returnd by last command
ret=$?

echo "Return value is $ret"
Return value is 10

Nested Functions:

#!/bin/sh

# Calling one function from another
number_one () {
   echo "This is the first function speaking..."
   number_two
}

number_two () {
   echo "This is now the second function speaking..."
}

# Calling function one.
number_one
This is the first function speaking...
This is now the second function speaking...