<?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: Basanta Bhusan Khadka</title>
    <description>The latest articles on Forem by Basanta Bhusan Khadka (@bhusan).</description>
    <link>https://forem.com/bhusan</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%2F2330815%2F7d05e2b9-309f-4222-a830-cf2b72f8b665.png</url>
      <title>Forem: Basanta Bhusan Khadka</title>
      <link>https://forem.com/bhusan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bhusan"/>
    <language>en</language>
    <item>
      <title>Shell Scripting: Writing and Running Your First Script</title>
      <dc:creator>Basanta Bhusan Khadka</dc:creator>
      <pubDate>Tue, 05 Nov 2024 12:23:05 +0000</pubDate>
      <link>https://forem.com/bhusan/shell-scripting-writing-and-running-your-first-script-5g8e</link>
      <guid>https://forem.com/bhusan/shell-scripting-writing-and-running-your-first-script-5g8e</guid>
      <description>&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Access to a Unix/Linux-based environment (e.g., Ubuntu, macOS, or a Linux VM) and for Windows you could use WSL (Windows Subsystem for Linux) else git bash.&lt;/li&gt;
&lt;li&gt;A text editor (like Vim, Nano, or VS Code).&lt;/li&gt;
&lt;li&gt;Basic knowledge of using the terminal or command line. (you could refer my blog &lt;a href="https://dev.to/bhusan/shell-scripting-basic-commands-to-just-get-you-start-scripting-ih6"&gt;Shell Scripting: Basic Commands To Just Get You Starting&lt;/a&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create a New Shell Script File&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the terminal&lt;/li&gt;
&lt;li&gt;Navigate to the directory you want to save the script&lt;/li&gt;
&lt;li&gt;Use the &lt;em&gt;&lt;strong&gt;touch&lt;/strong&gt;&lt;/em&gt; command to create a new file called hello_world.sh
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Your First Script&lt;/strong&gt;&lt;br&gt;
Open the file in your preferred editor(e.g., Vim):&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Your script should start with shebang but if its &lt;strong&gt;&lt;em&gt;vim&lt;/em&gt;&lt;/strong&gt; press &lt;strong&gt;&lt;em&gt;I&lt;/em&gt;&lt;/strong&gt; to get to insert mode.&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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;&lt;strong&gt;#!/bin/bash&lt;/strong&gt;&lt;/em&gt; line is called a "&lt;em&gt;&lt;strong&gt;shebang&lt;/strong&gt;&lt;/em&gt;" and it tells the system to use Bash to run the script in the terminal.&lt;/p&gt;

&lt;p&gt;Write a simple &lt;em&gt;&lt;strong&gt;echo&lt;/strong&gt;&lt;/em&gt; command to display "Hello, World!" on the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo "Hello, World"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your script should 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
echo "Hello, World"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now time to save file since its &lt;strong&gt;&lt;em&gt;vim&lt;/em&gt;&lt;/strong&gt; at the moment to save it press &lt;strong&gt;&lt;em&gt;Esc&lt;/em&gt;&lt;/strong&gt;, type &lt;strong&gt;&lt;em&gt;:wq&lt;/em&gt;&lt;/strong&gt; and press Enter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Make the Script Executable&lt;/strong&gt;&lt;br&gt;
Before you can run your script you need to give executable permission else it wouldn't be recognized as a program to run. Here's the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod +x hello_world.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command grants execute (+x) permissions to the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Run Your Script&lt;/strong&gt;&lt;br&gt;
Run the script just by typing the command&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;You should see&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, World!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hurry! Congratulations!! you have just written and executed your first shell script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Step 5: Practice More&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Adding on few tasks for your guys who have been following it all till here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Write a script to display “Hello, World!” and current date.&lt;br&gt;
It will help you Understand basics of script writing and command execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write a script to print system information like hostname, kernel version and more.&lt;br&gt;
It will help you get comfortable with basic commands and echo statements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a script to display the contents of a directory.&lt;br&gt;
It will help you learn file operations and directory commands.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This guide will help you to write and run your first script and help you enhance your understanding with the tasks to practice to get you up and running and stay motivated. &lt;/p&gt;

</description>
      <category>devops</category>
      <category>linux</category>
      <category>bash</category>
      <category>shellscripting</category>
    </item>
    <item>
      <title>Shell Scripting: Basic Commands to just get you start scripting</title>
      <dc:creator>Basanta Bhusan Khadka</dc:creator>
      <pubDate>Mon, 04 Nov 2024 10:32:58 +0000</pubDate>
      <link>https://forem.com/bhusan/shell-scripting-basic-commands-to-just-get-you-start-scripting-ih6</link>
      <guid>https://forem.com/bhusan/shell-scripting-basic-commands-to-just-get-you-start-scripting-ih6</guid>
      <description>&lt;p&gt;Shell scripting is a powerful scripting language that operates within a shell environment, allowing users to automate repetitive tasks, manage files and directories, configure systems, and execute a series of commands in a single script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Essential Shell Commands: A Practical Guide&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Mastering shell commands provides the foundation to harness the power of shell scripting. Here’s a comprehensive guide to the key commands you’ll rely on for efficient file management, system monitoring, text processing, and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File and Directory Management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a Directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir new_directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Creates a new directory in the current location.&lt;/p&gt;

&lt;p&gt;Navigate Directories&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd new_directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Changes to the specified directory.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;: Returns to the previous directory.&lt;/p&gt;

&lt;p&gt;List Directory Contents&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Lists files and folders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls -l
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Lists detailed information about files (permissions, size, date).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Lists all files, including hidden ones.&lt;/p&gt;

&lt;p&gt;Copy Files and Directories&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp source_file.txt destination.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Copies a file to a new location.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp -r source_directory destination_directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Recursively copies an entire directory.&lt;/p&gt;

&lt;p&gt;Move or Rename Files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mv oldname.txt newname.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Renames a file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mv file.txt /path/to/destination/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Moves a file to a specified directory.&lt;/p&gt;

&lt;p&gt;Delete Files and Directories&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rm file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Deletes a file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rm -r directory/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Deletes a directory and its contents (use with caution).&lt;/p&gt;

&lt;p&gt;File Content Operations&lt;br&gt;
Display File Contents&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Displays file contents.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;head file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Shows the first 10 lines of a file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tail file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Shows the last 10 lines of a file.&lt;/p&gt;

&lt;p&gt;Search within Files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grep "search_term" file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Searches for specific text within a file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grep -r "search_term" directory/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Searches within a directory recursively.&lt;/p&gt;

&lt;p&gt;Edit a File&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Opens a file in Vim editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nano file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Opens a file in Nano editor.&lt;/p&gt;

&lt;p&gt;File Permissions and Ownership&lt;br&gt;
Set File Permissions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod 755 file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Sets read, write, execute permissions for the owner and read/execute for others.&lt;/p&gt;

&lt;p&gt;Change File Ownership&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chown user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;:group file.txt: Changes file ownership.&lt;/p&gt;

&lt;p&gt;System Information and Monitoring&lt;br&gt;
Print Working Directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pwd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Shows the current directory path.&lt;/p&gt;

&lt;p&gt;Check Disk Usage&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;df -h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Displays disk space usage in a human-readable format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;du -h directory/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Shows the size of a specific directory.&lt;/p&gt;

&lt;p&gt;System Information&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uname -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Provides system information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;top
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Real-time process monitor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ps -ef
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Lists running processes.&lt;/p&gt;

&lt;p&gt;Network and Connectivity Commands&lt;br&gt;
Network Connectivity Check&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ping example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Checks if a host is reachable.&lt;/p&gt;

&lt;p&gt;View Network Configuration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ifconfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Displays network interfaces (use ip a on newer systems).&lt;/p&gt;

&lt;p&gt;Compression and Archiving&lt;br&gt;
Create Archives&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -cvf archive.tar directory/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Creates a tar archive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -czvf archive.tar.gz directory/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Creates a compressed (gzip) tar archive.&lt;/p&gt;

&lt;p&gt;Extract Archives&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -xvf archive.tar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Extracts a tar archive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -xzvf archive.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Extracts a gzip-compressed tar archive.&lt;/p&gt;

&lt;p&gt;Advanced Utilities&lt;br&gt;
Output Redirection&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;command &amp;gt; output.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Redirects output to a file (overwrites).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;command &amp;gt;&amp;gt; output.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Appends output to a file.&lt;/p&gt;

&lt;p&gt;Command Chaining and Piping&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls -l | grep "search_term"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Pipes output of one command into another.&lt;/p&gt;

&lt;p&gt;Set Environment Variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export VAR_NAME="value"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Sets an environment variable.&lt;/p&gt;

&lt;p&gt;Command History&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;history
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;: Displays command history.&lt;/p&gt;

&lt;p&gt;With these essential commands, you’re well-equipped to start scripting effectively in the shell environment. Mastering these commands opens up the potential for automation, efficient file handling, and streamlined system administration through shell scripting.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>linux</category>
      <category>bash</category>
      <category>shellscripting</category>
    </item>
  </channel>
</rss>
