Online Tutorials
While Loop - Shell Scripting
- Details
- Category: Shell Scripting Tutorials
While loop will work when the condition is true. If the condition is true for infinite times then the loop will run for infinite times.
Code:
open a editor and write the below mentioned code,
#!/bin/bash
a=10
while [[ $a -lt 20 ]]
do
echo "$a"
a=$((a + 1))
done
To execute:
sh while.sh
If Condition - Shell Scripting
- Details
- Category: Shell Scripting Tutorials
If the condition is true then the set of action/commands will be executed else it will check for another condition if that condition is true then it will execute those set of actions else it will execute the else part
Code:
open a editor and write the below mentioned code
#!/bin/bash
a=10
if [ $a -eq 20 ];then
echo "20"
elif [ $a -eq 10 ];then
echo "10"
else
echo "else"
fi
To execute:
sh if.sh
or
bash if.sh
or
chmod 755 ./if.sh
chmod command is used to change permission of a file.
./if.sh
Conditional Expression:
String1 == string2 true if both are equal
String1 != string2 true if both are not equal
-lt true if number is lesser than
-le true if number is lesser than or equal to
-gt true if the number is greater than
-ge true if the number is greater than or equal to
-eq true if the numbers are equal
-ne true if the numbers are not equal
-z string : true is the string is empty
-f file : true if the file exists
-d directory : true if the directory exists
-n string : true if the string is non zero
code:
open a editor and write the below mentioned code
#!/bin/bash
if [ -z "$testing" ];then
echo "String is null"
fi
if [ -n "$a" ];then
echo "String is not null"
fi
if [ -f "logs/test.log" ];then
echo "file exist"
fi
if [ -d "logs/" ];then
echo "directory exist"
fi
if [ -s "a" ];then
echo " file is not empty"
else
echo "file is empty"
fi
Arithmetic Expansion - Shell Scripting
- Details
- Category: Shell Scripting Tutorials
We have to use arthimetic expansion for doing any arithmetic calculations in shell script.
$(( expression ))
echo $(( 1 + 1 )) - will add 2 value
echo $(( 2 * 2 )) - multiply 2 value
echo $(( 2 / 2 )) - divide the one value by another
echo $(( 5 % 2 )) - modulo of 5 by 2
Note: shell does not support floating point so all the value will be rounded to the nearest number automatically
expr Command:
expr command is also used for calculating the arithmetic expressions
add=`expr 1 + 1`
mul=`expr 2 \* 6`
Note: in order to suppress * as regular expression \ is used before the *
div=`expr 6 / 2 `
modulo=`expr 7 % 2`
Arithmetic Evaluation - Shell Scripting
- Details
- Category: Shell Scripting Tutorials
id++ id-- : variable post-increment and post-decrement
echo $((a++))
++id --id
variable pre-increment and pre-decrement
echo $((++a))
- + unary minus and plus
! ~ logical and bitwise negation
** exponentiation
* / % multiplication, division, remainder
+ - addition, subtraction
<= >= < > comparison
== != equality and inequality
&& logical AND
|| logical OR
Command Substitution - Shell Scripting
- Details
- Category: Shell Scripting Tutorials
Command substitution is used to execute a command and return the result to the shell script. This result will be stored in a variable
path=`printenv|grep PATH`
` is used to substitute a command. The command inside ` will be executed in a sub shell and the result will be returned back to the parent shell. This result is now stored in the corresponding variable.
path and PATH and two different variables.
| is called pipe. The output of one command is given as the input to another command.
printenv command will be executed first and the output will be given as the input for the grep command.
$(command) – is another way of command substitution
path=$(printenv |grep PATH)