<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Twinkle lahariya</title>
    <description>The latest articles on Forem by Twinkle lahariya (@twinklelahariya).</description>
    <link>https://forem.com/twinklelahariya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F379202%2F9f3c4761-d862-4bf5-a36a-4eb2a4080175.jpeg</url>
      <title>Forem: Twinkle lahariya</title>
      <link>https://forem.com/twinklelahariya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/twinklelahariya"/>
    <language>en</language>
    <item>
      <title>Control program flow with if statement in shell scripting</title>
      <dc:creator>Twinkle lahariya</dc:creator>
      <pubDate>Sat, 09 Apr 2022 15:10:11 +0000</pubDate>
      <link>https://forem.com/twinklelahariya/control-program-flow-with-if-statement-in-shell-scripting-1b91</link>
      <guid>https://forem.com/twinklelahariya/control-program-flow-with-if-statement-in-shell-scripting-1b91</guid>
      <description>&lt;p&gt;In the last post, we learned about tests in shell scripting.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/twinklelahariya" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tXDAroGv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--f1yZ6FOt--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/379202/9f3c4761-d862-4bf5-a36a-4eb2a4080175.jpeg" alt="twinklelahariya"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/twinklelahariya/variables-and-tests-in-shell-scripting-2k81" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Variables and Tests in shell scripting&lt;/h2&gt;
      &lt;h3&gt;Twinkle lahariya ・ Apr 6 '22&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#shellscripting&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#bash&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;In this post, let's see how we can use them with if statement to control the flow of the script.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is &lt;code&gt;if&lt;/code&gt; statement?
&lt;/h3&gt;

&lt;p&gt;If statement allows us to make decisions in our script. It starts with the keyword &lt;code&gt;if&lt;/code&gt; followed by a test expression, &lt;code&gt;then&lt;/code&gt; list of commands followed by the closing &lt;code&gt;fi&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;A basic &lt;code&gt;if&lt;/code&gt; statement says that &lt;strong&gt;if&lt;/strong&gt; the test expression is &lt;strong&gt;true&lt;/strong&gt;, then perform the following commands. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Syntax&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if []
then
    command 1
    command 2
    .
    .
    command n
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand &lt;code&gt;if&lt;/code&gt; with an example where we check if a given number is greater than 10.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

VAR=20
if [ $VAR -gt 10 ]
then
    echo "Variable is greater than 10"
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; ./IfDemo.sh
Variable is greater than 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see what we have done here:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if [ $VAR -gt 10 ]&lt;/code&gt; : &lt;code&gt;if&lt;/code&gt; followed by a test condition &lt;code&gt;$VAR -gt 10&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$VAR -gt 10&lt;/code&gt; : returns true if the var is greater (&lt;code&gt;-gt&lt;/code&gt;) than 10&lt;br&gt;
&lt;code&gt;then&lt;/code&gt; : if the condition is true, then execute the following commands&lt;br&gt;
&lt;code&gt;fi&lt;/code&gt; : end of &lt;code&gt;if&lt;/code&gt; statement&lt;/p&gt;

&lt;p&gt;What if you want to log both the scenarios, i.e. if the number is greater than 10 and then the number is less than 10.&lt;/p&gt;
&lt;h3&gt;
  
  
  If..else statement
&lt;/h3&gt;

&lt;p&gt;If-else statement follows the following form:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Syntax&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if []
then
    command 1
else 
    command 2
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If-else tells that &lt;strong&gt;if&lt;/strong&gt; the test is &lt;strong&gt;true&lt;/strong&gt;, then run &lt;strong&gt;command 1&lt;/strong&gt;, &lt;strong&gt;else&lt;/strong&gt; run &lt;strong&gt;command 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's extend the same example of a number greater than 10 and print a message if the number is not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

VAR=5
if [ $VAR -gt 10 ]
then
    echo "Variable is greater than 10"
else 
    echo "Variable is less than 10"
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; ./IfDemo.sh

Variable is less than 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we want to check the second test condition if the first one fails and handle it differently, then we use &lt;code&gt;if-elif-else&lt;/code&gt; ladder.&lt;/p&gt;

&lt;h3&gt;
  
  
  If..elif..else statement
&lt;/h3&gt;

&lt;p&gt;Let's check if the number is greater than 10, then print 'greater than'. Else, if the number is equals 10, print 'equals to', else print 'less than'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

VAR=5
if [ $VAR -gt 10 ]
then
    echo "Variable is greater than 10"
elif [ $VAR -eq 10]
    echo "Variable is equals to 10"
else 
    echo "Variable is less than 10"
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; ./IfDemo.sh
Variable is equals to 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is more to if statements. To list some of the scenarios:&lt;/p&gt;

&lt;p&gt;1: Nested if statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [[ Test condition ]]
then
  if [[ Test condition ]]
  then
    command 1
  else
    command 2
  fi
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2: Multiple test conditions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [[ Test condition ]] &amp;amp;&amp;amp; [[ Test condition ]]
then
  command 1
elif [[ Test condition ]] || [[ Test condition ]]
then
  command 2
else
  command 3
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool, I'll go now and try these scenarios out, do let me know if you try them and have any questions about it.&lt;/p&gt;

&lt;p&gt;Up next is loops in shell scripting. Stay tuned.&lt;/p&gt;

&lt;p&gt;Until next time. Love ❤️ &lt;/p&gt;

</description>
      <category>shellscripting</category>
      <category>bash</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Variables and Tests in shell scripting</title>
      <dc:creator>Twinkle lahariya</dc:creator>
      <pubDate>Wed, 06 Apr 2022 10:42:09 +0000</pubDate>
      <link>https://forem.com/twinklelahariya/variables-and-tests-in-shell-scripting-2k81</link>
      <guid>https://forem.com/twinklelahariya/variables-and-tests-in-shell-scripting-2k81</guid>
      <description>&lt;p&gt;In the earlier post, we learned the basics of shell scripting and wrote our first shell script.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/twinklelahariya" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tXDAroGv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--f1yZ6FOt--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/379202/9f3c4761-d862-4bf5-a36a-4eb2a4080175.jpeg" alt="twinklelahariya"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/twinklelahariya/shell-script-basicspart-i-1mib" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Shell Scripting Basics&lt;/h2&gt;
      &lt;h3&gt;Twinkle lahariya ・ Apr 5 '22&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#shellscripting&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;In this post, let's start with variables in a shell script&lt;/p&gt;

&lt;h3&gt;
  
  
  What are variables?
&lt;/h3&gt;

&lt;p&gt;Variables are the storage locations that have a name.&lt;br&gt;
Consider variables as a name-value pair.&lt;br&gt;
&lt;code&gt;VARIABLE_NAME="Value"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Variables' names are case sensitive and the general convention is to have them in all uppercase.&lt;/p&gt;
&lt;h4&gt;
  
  
  Now, how can I use these variables in my script?
&lt;/h4&gt;

&lt;p&gt;Variables can be used by preceding variable names with &lt;code&gt;$&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;$&amp;lt;VARIABLE_NAME&amp;gt;&lt;/code&gt;&lt;br&gt;
Let's look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
SHELL_NAME="bash"
echo "I am using $SHELL_NAME scripting!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;./VariablesDemo.sh
I am using bash scripting!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We can also enclose the name in curly braces&lt;br&gt;
&lt;code&gt;${&amp;lt;VARIABLE_NAME&amp;gt;}&lt;/code&gt;&lt;br&gt;
In that case, the above example will look like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
SHELL_NAME="bash"
echo "I am using ${SHELL_NAME} scripting!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;./VariablesDemo.sh
I am using bash scripting!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Is curly braces mandatory?
&lt;/h4&gt;

&lt;p&gt;No, but in some cases, you need to. Let me show you when&lt;/p&gt;

&lt;p&gt;In case you want to concatenate the variable with another string you have to use curly braces.&lt;/p&gt;

&lt;p&gt;Say you have to write "I am scripting on my machine" and you have a variable "$SCRIPT="script" in that case you will write:&lt;br&gt;
&lt;code&gt;echo "I am ${SCRIPT}ing on my machine"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you write &lt;code&gt;$SCRIPTing&lt;/code&gt;, in that case, the whole string &lt;code&gt;SCRIPTing&lt;/code&gt; will become the name and since we do not have a variable with that name, nothing will be put in that place.&lt;/p&gt;
&lt;h4&gt;
  
  
  Assigning output of a command to a variable
&lt;/h4&gt;

&lt;p&gt;To assign the output of a command to a variable, just use &lt;code&gt;$(command)&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

SHELL_NAME="bash"
USER_NAME=$(whoami)
echo "$USER_NAME is using ${SHELL_NAME} scripting!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; ./VariablesExample.sh
twinklekailashlahariya is using bash scripting!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Variable naming conventions
&lt;/h4&gt;
&lt;h5&gt;
  
  
  &lt;em&gt;don'ts&lt;/em&gt;
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Don't start with a number&lt;/li&gt;
&lt;li&gt;Don't use special characters. Only &lt;code&gt;_&lt;/code&gt; is allowed and can be at any location&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;otherwise, go crazy and name that variable of yours as you want.&lt;/p&gt;
&lt;h3&gt;
  
  
  Tests in a shell script
&lt;/h3&gt;

&lt;p&gt;Scripts are made to replace the effort to sit and scribble a series of commands. What if we want to perform certain actions based on conditional scenarios? If we are typing in the commands manually, we might check the status of the previous command and write the next command accordingly, right?&lt;/p&gt;

&lt;p&gt;In shell scripts, we can achieve the same with tests.&lt;br&gt;
To test a condition, simply place the condition within square brackets.&lt;br&gt;
&lt;em&gt;Syntax&lt;/em&gt; = &lt;code&gt;[condition-to-test]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the test passes, the expression exits with exit status 0, i.e. true, else it returns 1, i.e. false.&lt;/p&gt;

&lt;p&gt;Let's look at this with an example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

FILENAME=/etc/myFile.txt
if [ -f "$FILENAME" ]
then echo "File exist"
else echo "File does not exist"
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; ./TestsExample.sh
File does not exist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Let's see what we have done here:&lt;br&gt;
&lt;code&gt;FILENAME=/etc/myFile.txt&lt;/code&gt; : Defined a variable with a file path.&lt;br&gt;
&lt;code&gt;if [ -f "#FILENAME" ]&lt;/code&gt; : An if condition that checks if the file exists at the given path.&lt;br&gt;
&lt;code&gt;[ -f "#FILENAME" ]&lt;/code&gt; : Our test condition where &lt;code&gt;-f&lt;/code&gt; is a flag that returns true if file exists and is a regular file.&lt;br&gt;
&lt;code&gt;then echo "File exist"&lt;/code&gt; : if the file exists, the control flow goes into this condition and prints "File exist" message.&lt;br&gt;
&lt;code&gt;else echo "File does not exist"&lt;/code&gt; : otherwise the control falls into this block and prints "File does not exist" message.&lt;/p&gt;

&lt;p&gt;Like &lt;code&gt;-f&lt;/code&gt;, we have more such test conditions, some of the popular ones are listed below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-n STRING&lt;/code&gt;    the length of STRING is nonzero&lt;br&gt;
&lt;code&gt;-z STRING&lt;/code&gt;    the length of STRING is zero&lt;br&gt;
&lt;code&gt;STRING1 = STRING2&lt;/code&gt;    the strings are equal&lt;br&gt;
&lt;code&gt;STRING1 != STRING2&lt;/code&gt;   the strings are not equal&lt;br&gt;
&lt;code&gt;INTEGER1 -eq INTEGER2&lt;/code&gt;    INTEGER1 is equal to INTEGER2&lt;br&gt;
&lt;code&gt;INTEGER1 -ge INTEGER2&lt;/code&gt;    INTEGER1 is greater than or equal to INTEGER2&lt;br&gt;
&lt;code&gt;INTEGER1 -gt INTEGER2&lt;/code&gt;    INTEGER1 is greater than INTEGER2&lt;br&gt;
&lt;code&gt;INTEGER1 -le INTEGER2&lt;/code&gt;    INTEGER1 is less than or equal to INTEGER2&lt;br&gt;
&lt;code&gt;INTEGER1 -lt INTEGER2&lt;/code&gt;    INTEGER1 is less than INTEGER2&lt;br&gt;
&lt;code&gt;INTEGER1 -ne INTEGER2&lt;/code&gt;    INTEGER1 is not equal to INTEGER2&lt;br&gt;
&lt;code&gt;FILE1 -ef FILE2&lt;/code&gt;  FILE1 and FILE2 have the same device and inode numbers&lt;br&gt;
&lt;code&gt;FILE1 -nt FILE2&lt;/code&gt;  FILE1 is newer (modification date) than FILE2&lt;br&gt;
&lt;code&gt;FILE1 -ot FILE2&lt;/code&gt;  FILE1 is older than FILE2&lt;br&gt;
&lt;code&gt;-b FILE&lt;/code&gt;  FILE exists and is block special&lt;br&gt;
&lt;code&gt;-c FILE&lt;/code&gt;  FILE exists and is character special&lt;br&gt;
&lt;code&gt;-d FILE&lt;/code&gt;  FILE exists and is a directory&lt;br&gt;
&lt;code&gt;-e FILE&lt;/code&gt;  FILE exists&lt;br&gt;
&lt;code&gt;-g FILE&lt;/code&gt;  FILE exists and is set-group-ID&lt;br&gt;
&lt;code&gt;-G FILE&lt;/code&gt;  FILE exists and is owned by the effective group ID&lt;br&gt;
&lt;code&gt;-h FILE&lt;/code&gt;  FILE exists and is a symbolic link (same as -L)&lt;br&gt;
&lt;code&gt;-k FILE&lt;/code&gt;  FILE exists and has its sticky bit set&lt;br&gt;
&lt;code&gt;-L FILE&lt;/code&gt;  FILE exists and is a symbolic link (same as -h)&lt;br&gt;
&lt;code&gt;-O FILE&lt;/code&gt;  FILE exists and is owned by the effective user ID&lt;br&gt;
&lt;code&gt;-p FILE&lt;/code&gt;  FILE exists and is a named pipe&lt;br&gt;
&lt;code&gt;-r FILE&lt;/code&gt;  FILE exists and read permission is granted&lt;br&gt;
&lt;code&gt;-s FILE&lt;/code&gt;  FILE exists and has a size greater than zero&lt;br&gt;
&lt;code&gt;-S FILE&lt;/code&gt;  FILE exists and is a socket&lt;br&gt;
&lt;code&gt;-w FILE&lt;/code&gt;  FILE exists and write permission is granted&lt;br&gt;
&lt;code&gt;-x FILE&lt;/code&gt;  FILE exists and execute (or search) permission is granted&lt;/p&gt;

&lt;p&gt;We will look into more examples of these flags in our next post when we look into conditions and loops.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/twinklelahariya" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tXDAroGv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--f1yZ6FOt--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/379202/9f3c4761-d862-4bf5-a36a-4eb2a4080175.jpeg" alt="twinklelahariya"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/twinklelahariya/control-program-flow-with-if-statement-in-shell-scripting-1b91" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Control program flow with if statement in shell scripting&lt;/h2&gt;
      &lt;h3&gt;Twinkle lahariya ・ Apr 9 '22&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#shellscripting&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#bash&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;Until next time... Peace ✌️ &lt;/p&gt;

</description>
      <category>shellscripting</category>
      <category>bash</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Shell Scripting Basics</title>
      <dc:creator>Twinkle lahariya</dc:creator>
      <pubDate>Tue, 05 Apr 2022 15:26:30 +0000</pubDate>
      <link>https://forem.com/twinklelahariya/shell-script-basicspart-i-1mib</link>
      <guid>https://forem.com/twinklelahariya/shell-script-basicspart-i-1mib</guid>
      <description>&lt;h3&gt;
  
  
  What is a Script?
&lt;/h3&gt;

&lt;p&gt;A Script is a command line program that contains a series of instructions. &lt;br&gt;
Anything that you can type in a command line, you can put in a script.&lt;/p&gt;

&lt;p&gt;Let's greet the world with a script.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#!/bin/bash echo "Hello World"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;we will look into every component of this script a bit, but before that, let's first see how a script is interpreted and executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interpreting a Script.
&lt;/h3&gt;

&lt;p&gt;Any script is interpreted by an interpreter that executes the command one after the other. In the case of shell script, the shell accesses the interpreter and executes the commands. Functions that shell scripts support include loops, variables, if/then/else statements, arrays and shortcuts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Now the question is, why do we need scripts?
&lt;/h3&gt;

&lt;p&gt;If you find yourself doing a set of tasks over and over again, you might want to automate that task. Scripting is a great way to automate tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Components of a Script
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash 
echo "Hello World"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;A script starts with&lt;br&gt;
&lt;code&gt;#!&lt;/code&gt; : Shebang, which represents the beginning of the script followed by the path of the interpreter. &lt;br&gt;
&lt;code&gt;/bin/bash&lt;/code&gt; : This is the path to the interpreter used to execute commands written in the script. &lt;br&gt;
Here are some more examples of shell scripts, each using a different shell program as the interpreter: &lt;br&gt;
&lt;code&gt;#!/bin/zsh&lt;/code&gt; : This script uses zsh as the interpreter. &lt;code&gt;#!/bin/csh&lt;/code&gt; : This script uses csh as the interpreter. &lt;code&gt;#!/usr/bin/python&lt;/code&gt; : This script uses python as the interpreter.&lt;/p&gt;
&lt;h4&gt;
  
  
  Is the Shebang mandatory?
&lt;/h4&gt;

&lt;p&gt;No, the shebang is an optional command. If we do not specify shebang, then the script runs in the current shell. &lt;/p&gt;

&lt;p&gt;Is this a problem? &lt;br&gt;
&lt;em&gt;No, if you are lucky.&lt;/em&gt;&lt;br&gt;
Different shells have slightly different syntax. It's safe to be explicit and specify the exact interpreter to be used with the script. &lt;/p&gt;

&lt;p&gt;After, the first line of the script follows the executable instructions. In our case, it is: &lt;br&gt;
&lt;code&gt;echo "Hello World"&lt;/code&gt; : Displays hello would to the console. &lt;/p&gt;

&lt;p&gt;Now, save the file with the .sh extension in the location that the shell can access.&lt;/p&gt;
&lt;h3&gt;
  
  
  Run a shell script
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Make the script executable with command &lt;code&gt;chmod +X &amp;lt;fileName&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run the script using &lt;code&gt;./&amp;lt;fileName&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In order to run our HelloWorld program:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;save the file with HelloWorld.sh&lt;/li&gt;
&lt;li&gt;run the command &lt;code&gt;chmod +X HelloWorld.sh&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;execute the script &lt;code&gt;./HelloWorld.sh&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;output :&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; ./HelloWorld.sh
Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  What happens when we execute a script?
&lt;/h3&gt;

&lt;p&gt;When we execute a script that has a shebang, what happens it that the interpreter executes and the path used to call the script is passed as an argument to the interpreter. &lt;br&gt;
And then every command following the Shebang gets executed line by line. &lt;/p&gt;

&lt;p&gt;Voila! We just ran our first shell script. &lt;/p&gt;

&lt;p&gt;Up next is variables and tests in a shell script..&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/twinklelahariya" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tXDAroGv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--f1yZ6FOt--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/379202/9f3c4761-d862-4bf5-a36a-4eb2a4080175.jpeg" alt="twinklelahariya"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/twinklelahariya/variables-and-tests-in-shell-scripting-2k81" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Variables and Tests in shell scripting&lt;/h2&gt;
      &lt;h3&gt;Twinkle lahariya ・ Apr 6 '22&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#shellscripting&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#bash&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;Until next time, love ❤️&lt;/p&gt;

</description>
      <category>shellscripting</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
