Facebooktwittergoogle_plusredditpinterestlinkedinmail

I am sharing beginner shell script reference.

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 ComparisonDescription
String1 = String2Returns true if the strings are equal
String1 != String2Returns true if the strings are not equal
-n String1Returns true if the string is not null
-z String1Returns true if the string is null
Numeric ComparisonDescription
expr1 -eq expr2Returns true if the expressions are equal
expr1 -ne expr2Returns true if the expressions are not equal
expr1 -gt expr2Returns true if expr1 is greater than expr2
expr1 -ge expr2Returns true if expr1 is greater than or equal to expr2
expr1 -lt expr2Returns true if expr1 is less than expr2
expr1 -le expr2Returns true if expr1 is less than or equal to expr2
! expr1Negates the result of the expression
File ConditionalsDescription
-d fileTrue if the file is a directory
-e fileTrue if the file exists (note that this is not particularly portable, thus -f is generally used)
-f fileTrue if the provided string is a file
-g fileTrue if the group id is set on a file
-r fileTrue if the file is readable
-s fileTrue if the file has a non-zero size
-uTrue if the user id is set on a file
-wTrue if the file is writable
-xTrue 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
Beginner Shell Scripting Reference Roopendra DevOPSScripting,
I am sharing beginner shell script reference. 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 $HELLOConditional Statement 1) if conditional statement The base for the 'if' constructions in bash is this: Sytanx: if ; then ...
I am sharing beginner shell script reference. <h3>Local Variable in Shell Script</h3> We can create local variables in shell script by using the keyword local. <pre>#!/bin/bash HELLO=Hello function helloWorld { local HELLO=World echo $HELLO } echo $HELLO helloWorld echo $HELLO </pre> <h3>Conditional Statement</h3> <h4>1) if conditional statement</h4> The base for the 'if' constructions in bash is this: <strong>Sytanx:</strong> <pre>if [expression]; then code if 'expression' is true. fi</pre> <strong>Example: </strong> <pre>#!/bin/bash if [ "foo" = "foo" ]; then echo when condition is true fi </pre> <h4>2) if .. then ... else conditional statement</h4> <strong>Example:</strong> <pre>#!/bin/bash if [ "bar" = "bar" ]; then echo when condition is true else echo when condition is false fi</pre> <strong>Example:</strong> <pre>#!/bin/bash VAR1="value1" VAR2="value2" if [ "$VAR1" = "$VAR2" ]; then echo when condition is true else echo when condition is false fi </pre> List of String Comparison , Numeric Comparison and File Check Condition used in Shell Scripting. <table class="wiki-content-table"> <tbody> <tr> <th>String Comparison</th> <th>Description</th> </tr> <tr> <td>String1 = String2</td> <td>Returns true if the strings are equal</td> </tr> <tr> <td>String1 != String2</td> <td>Returns true if the strings are not equal</td> </tr> <tr> <td>-n String1</td> <td>Returns true if the string is not null</td> </tr> <tr> <td>-z String1</td> <td>Returns true if the string is null</td> </tr> <tr> <th>Numeric Comparison</th> <th>Description</th> </tr> <tr> <td>expr1 -eq expr2</td> <td>Returns true if the expressions are equal</td> </tr> <tr> <td>expr1 -ne expr2</td> <td>Returns true if the expressions are not equal</td> </tr> <tr> <td>expr1 -gt expr2</td> <td>Returns true if expr1 is greater than expr2</td> </tr> <tr> <td>expr1 -ge expr2</td> <td>Returns true if expr1 is greater than or equal to expr2</td> </tr> <tr> <td>expr1 -lt expr2</td> <td>Returns true if expr1 is less than expr2</td> </tr> <tr> <td>expr1 -le expr2</td> <td>Returns true if expr1 is less than or equal to expr2</td> </tr> <tr> <td>! expr1</td> <td>Negates the result of the expression</td> </tr> <tr> <th>File Conditionals</th> <th>Description</th> </tr> <tr> <td>-d file</td> <td>True if the file is a directory</td> </tr> <tr> <td>-e file</td> <td>True if the file exists (note that this is not particularly portable, thus -f is generally used)</td> </tr> <tr> <td>-f file</td> <td>True if the provided string is a file</td> </tr> <tr> <td>-g file</td> <td>True if the group id is set on a file</td> </tr> <tr> <td>-r file</td> <td>True if the file is readable</td> </tr> <tr> <td>-s file</td> <td>True if the file has a non-zero size</td> </tr> <tr> <td>-u</td> <td>True if the user id is set on a file</td> </tr> <tr> <td>-w</td> <td>True if the file is writable</td> </tr> <tr> <td>-x</td> <td>True if the file is an executable</td> </tr> </tbody> </table> <h3>Conditional Statement: Loops for, while and until</h3> <strong>for</strong>: for loops iterate through a set of values until the list is exhausted: <strong>while</strong>: 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.) <strong>until</strong>: The until loop is almost equal to the while loop, except that the code is executed while the control expression evaluates to false. <h3>For Loop:</h3> Syntax: <pre>for var in do #command to perform some operation based on list item done </pre> <strong>Example: </strong> <pre>#!/bin/bash # Basic for loop TAGLINE='Techieroop Solution for DevOps' for word in $TAGLINE do echo $word done</pre> Output: <pre>Techieroop Solution for DevOps</pre> <h4>Iterate over ranges</h4> <pre>#!/bin/bash # Range in for loop for value in {1..5} do echo $value done </pre> <strong>Output: </strong> <pre>1 2 3 4 5 </pre> <h4>While Loop</h4> While loop is executed as long as given condition is true. <pre>#!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done </pre> <h4>Until Loop</h4> 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. <pre>#!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ]; do echo COUNTER $COUNTER let COUNTER-=1 done </pre>
(Visited 18 times, 28 visits today)
Facebooktwittergoogle_pluslinkedinrss