Shell Scripting Tutorial for Beginners
Shell Scripting Tutorial for Beginners
I am sharing few beginner shell scripting reference that we need in day-to-day tasks.
Local Variable in Shell Script
We can create local variables in shell script by using the keyword local.
#!/bin/bash HELLO=Hello function helloWorld { local HELLO=World echo $HELLO } echo $HELLO helloWorld echo $HELLO
Conditional Statement
1) if conditional statement
The base for the ‘if’ constructions in bash is this:
Sytanx:
if [expression]; then code if 'expression' is true. fi
Example:
#!/bin/bash if [ "foo" = "foo" ]; then echo when condition is true fi
2) if .. then … else conditional statement
Example:
#!/bin/bash if [ "bar" = "bar" ]; then echo when condition is true else echo when condition is false fi
Example:
#!/bin/bash VAR1="value1" VAR2="value2" if [ "$VAR1" = "$VAR2" ]; then echo when condition is true else echo when condition is false fi
List of String Comparison , Numeric Comparison and File Check Condition used in Shell Scripting.
String Comparison | Description |
---|---|
String1 = String2 | Returns true if the strings are equal |
String1 != String2 | Returns true if the strings are not equal |
-n String1 | Returns true if the string is not null |
-z String1 | Returns true if the string is null |
Numeric Comparison | Description |
expr1 -eq expr2 | Returns true if the expressions are equal |
expr1 -ne expr2 | Returns true if the expressions are not equal |
expr1 -gt expr2 | Returns true if expr1 is greater than expr2 |
expr1 -ge expr2 | Returns true if expr1 is greater than or equal to expr2 |
expr1 -lt expr2 | Returns true if expr1 is less than expr2 |
expr1 -le expr2 | Returns true if expr1 is less than or equal to expr2 |
! expr1 | Negates the result of the expression |
File Conditionals | Description |
-d file | True if the file is a directory |
-e file | True if the file exists (note that this is not particularly portable, thus -f is generally used) |
-f file | True if the provided string is a file |
-g file | True if the group id is set on a file |
-r file | True if the file is readable |
-s file | True if the file has a non-zero size |
-u | True if the user id is set on a file |
-w | True if the file is writable |
-x | True if the file is an executable |
Conditional Statement: Loops for, while and until
for: for loops iterate through a set of values until the list is exhausted:
while: The while executes a piece of code if the control expression is true, and only stops when it is false (or a explicit break is found within the executed code.)
until: The until loop is almost equal to the while loop, except that the code is executed while the control expression evaluates to false.
For Loop:
Syntax:
for var in do #command to perform some operation based on list item done
Example:
#!/bin/bash # Basic for loop TAGLINE='Techieroop Solution for DevOps' for word in $TAGLINE do echo $word done
Output:
Techieroop Solution for DevOps
Iterate over ranges
#!/bin/bash # Range in for loop for value in {1..5} do echo $value done
Output:
1 2 3 4 5
While Loop
While loop is executed as long as given condition is true.
#!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done
Until Loop
The until loop is fairly similar to the while loop. The difference is that it will execute the commands within it until the test becomes true.
#!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ]; do echo COUNTER $COUNTER let COUNTER-=1 done