Scripting 101: The missed part of your Computer Science Education
Published: 2023-04-23 12:01:11

<h1 id="heading-shell-we-begin">Shell We Begin?</h1>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1682249090806/2ee7f578-b164-4560-ae24-b4e9d2e12216.jpeg" alt class="image--center mx-auto" /></p>
<p>Computers are pretty good at doing the same thing over and over again as computer scientists we know that. But sometimes we forget that we can also do things better with them. While we have access to numerous tools that can enhance our productivity, our proficiency is often limited to a handful of tricks. In the face of challenges, we resort to copy-pasting commands as a quick fix. However, it's essential to expand our skill set and delve deeper into the realm of scripting. With a better understanding of scripting, we can overcome obstacles and accomplish more than ever before.</p>
<h3 id="heading-this-series-is-an-attempt-to-change-how-you-work"><strong>This series is an attempt to change how you work</strong></h3>
<p>Throughout this series, we will be exploring six to seven distinct topics that span the entire spectrum of scripting. The blogs will cover everything from the fundamentals of scripting to advanced techniques such as data wrangling and MetaProgramming</p>
<p>Each blog in this series will include exercises and external resources to help you apply the concepts covered. While the command line might feel difficult and confusing at first, it's designed to simplify your life, not make it more complex. So, don't be afraid to dive in and experiment. Practice consistently and use the provided resources to develop your skills.</p>
<h1 id="heading-shell-what-is-it">Shell, What is it?</h1>
<p>If you read the first line of the blog carefully it says <strong>Shell</strong> <strong>we</strong> <strong>begin</strong> instead of Shall we begin? The shell here is nothing but a command-line interface that provides a way for users to interact with the operating system.</p>
<h3 id="heading-but"><strong>but</strong></h3>
<p>We already have GUI and everything programmed why bother with the command line and shell scripting?</p>
<p>Yes, that's true for most things in fact 80 percent of things but Graphical interfaces and voice commands are limited in what they can do, restricting users to a set of programmed actions. To fully utilize the capabilities of a computer, one must turn to the old-school textual interface: The Shell. While shells may differ in specifics, their core function remains the same: running programs, providing input, and inspecting output in a structured way. Shells exist on almost every platform, providing users with a powerful tool for computer interaction.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1682240066506/d508832d-dd2a-4ed2-8a0b-c507931c2429.png" alt class="image--center mx-auto" /></p>
<p>You can also think of the shell as a mediator between you and your operating system, allowing you to perform tasks that may not be possible through the graphical user interface. Think of it as a command-line interface to interact with the kernel of your operating system. Whether you're on Windows, Linux, or macOS, the shell provides a powerful toolset for interacting with your system and getting things done.</p>
<h2 id="heading-interacting-with-shell">Interacting with Shell</h2>
<p>Upon launching your terminal, it is common to encounter a prompt that may resemble the following.</p>
<pre><code class="lang-bash">vaibhav:~$
</code></pre>
<ul>
<li><p>This prompt provides information about the machine you're using (such as the name "<strong><em>vaibhav</em></strong>") and displays your current working directory (often indicated by "<strong><em>~</em></strong>" to represent the home directory).</p>
</li>
<li><p>The "<strong><em>$</em></strong>" symbol indicates that you are currently logged in as a non-root user. From this prompt, you can enter commands for the shell to execute, such as running a program.</p>
</li>
</ul>
<pre><code class="lang-bash">vaibhav:~$ whoami
</code></pre>
<p>If you execute the "whoami" command in the Bash shell and you are logged in as a user named "vaibhav", the output will be</p>
<pre><code class="lang-bash">vaibhav:~$ whoami
vaibhav
</code></pre>
<p>Let's try some more commands</p>
<pre><code class="lang-bash">vaibhav:~$  <span class="hljs-built_in">echo</span> this
this
</code></pre>
<p>When we ran the command "echo this" in the shell, we instructed the shell to execute the "echo" program with the argument "this". The "echo" program simply prints out its arguments to the terminal. To execute a command, the shell parses the input by splitting it into individual words based on whitespace. The first word is assumed to be the name of the program to be run, and any subsequent words are treated as arguments to that program.</p>
<p>But how does he shell know how to find the <code>this</code> or <code>echo</code> programs? Well, the shell is a programming environment, just like Python or Ruby, and so it has variables, conditionals, loops, and functions (next lecture!). When you run commands in your shell, you are really writing a small bit of code that your shell interprets. If the shell is asked to execute a command that doesn’t match one of its programming keywords, it consults an <em>environment variable</em> called <code>$PATH</code> that lists which directories the shell should search for programs when it is given a command:</p>
<pre><code class="lang-bash">vaibhav:~$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$PATH</span>
/usr/<span class="hljs-built_in">local</span>/sbin:/usr/<span class="hljs-built_in">local</span>/bin:/usr/sbin:/usr/bin:/sbin:/bin
</code></pre>
<p>When we use the "echo" command in the shell, the shell looks for an executable program file with that name by searching through a list of directories specified in the $PATH environment variable. If it finds an executable file with the name "echo" in one of the directories listed in $PATH, it runs that file.</p>
<p>To determine which file the shell is executing for a particular command, we can use the "which" command. This will show us the path to the executable file that would be executed if we ran the command. Alternatively, we can bypass the search through $PATH and execute a specific file by providing the full path to that file.</p>
<h2 id="heading-navigating-the-shell">Navigating the shell</h2>
<p>A path in the shell is like a roadmap that shows you where your files and folders live. It's basically a list of directories separated by slashes on Linux and macOS or backslashes on Windows. On Linux and macOS, the path / is the big cheese of all directories - everything else is under it. But on Windows, each disk partition has its own top dog (like C:).</p>
<p>For the purposes of this class, we'll assume you're using a Linux file system. If a path starts with /, it's an absolute path. Any other path is relative to your current location. Speaking of location, you can use the "pwd" command to see where you are, and the "cd" command to change your current directory.</p>
<p>In a path, the dot "." refers to your current directory, kind of like how "you are here" on a mall map. And ".." refers to the directory one level up, like how you'd use the escalator to go up a floor.</p>
<pre><code class="lang-bash">vaibhav:~$ <span class="hljs-built_in">pwd</span>
/home/vaibhav
vaibhav:~$ <span class="hljs-built_in">cd</span> /home
vaibhav:/home$ <span class="hljs-built_in">pwd</span>
/home
vaibhav:/home$ <span class="hljs-built_in">cd</span> ..
vaibhav:/$ <span class="hljs-built_in">pwd</span>
/
vaibhav:/$ <span class="hljs-built_in">cd</span> ./home
vaibhav:/home$ <span class="hljs-built_in">pwd</span>
/home
vaibhav:/home$ <span class="hljs-built_in">cd</span> vaibhav
vaibhav:~$ <span class="hljs-built_in">pwd</span>
/home/vaibhav
vaibhav:~$ ../../bin/<span class="hljs-built_in">echo</span> hello
hello
</code></pre>
<p>In general, when we run a program, it will operate in the current directory unless we tell it otherwise. For example, it will usually search for files there, and create new files there if it needs to.</p>
<p>To see what lives in a given directory, we use the <code>ls</code> command:</p>
<pre><code class="lang-bash">vaibhav:~$ ls
vaibhav:~$ <span class="hljs-built_in">cd</span> ..
vaibhav:/home$ ls
vaibhav
vaibhav:/home$ <span class="hljs-built_in">cd</span> ..
vaibhav:/$ ls
bin
boot
dev
etc
home
...
</code></pre>
<h2 id="heading-man-command-the-saviour">Man Command the Saviour</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1682248926358/af319d20-74de-415e-9292-97cf893e3695.jpeg" alt class="image--center mx-auto" /></p>
<p><code>MAN</code> command is used to display the manual pages for other commands and topics. The manual pages, or "man pages" for short, provide detailed documentation and information about various commands, system calls, library functions, and other topics related to the Unix operating system.</p>
<p>When you type <code>man</code> followed by the name of a command or topic, the <code>man</code> command searches for and displays the corresponding manual page. For example, to view the manual page for the <code>ls</code> command, you would enter:</p>
<pre><code class="lang-bash">man ls
</code></pre>
<p>This would display the manual page for the <code>ls</code> command, which includes information about its usage, options, and examples.</p>
<h2 id="heading-some-commands-for-you-to-explore">Some commands for you to explore</h2>
<ol>
<li><p><code>mv</code> - (to rename/move a file)</p>
</li>
<li><p><code>cp</code> - (to copy a file)</p>
</li>
<li><p><code>mkdir</code> - (to make a new directory)</p>
</li>
</ol>
<h2 id="heading-exercises">Exercises</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1682249647286/16e38d2f-2d6d-4549-91f0-04125133ad17.webp" alt class="image--center mx-auto" /></p>
<p>If you've reached this part of the blog know that I'm extremely proud of you trying to learn this topic You've been learning the hard thing, Keep up the great work and keep learning! Below are some exercises for you to try on your own you can always reach out to me if you're stuck anywhere</p>
<ol>
<li><p>Install WSL from <a target="_blank" href="https://learn.microsoft.com/en-us/windows/wsl/">here</a>. - If you are on Linux or macOS, you don’t have to do anything special. If you are on Windows, you need to make sure you are not running cmd.exe or PowerShell; you can use WSL to run UNIX like commands.</p>
</li>
<li><p>Look up the <code>touch</code> program. The <code>man</code> program is your friend.</p>
</li>
<li><p>Use the <code>mkdir</code> command to create a new directory called <code>my_folder</code>.</p>
</li>
<li><p>Use the <code>cd</code> command to navigate to <code>my_folder</code>.</p>
</li>
<li><p>Use the <code>touch</code> command to create two new files called <code>file1.txt</code> and <code>file2.txt</code>.</p>
</li>
<li><p>Use the <code>ls</code> command to verify that the two new files were created.</p>
</li>
<li><p>Use the <code>echo</code> command to write some text to <code>file1.txt</code>.</p>
</li>
</ol>
<p>Whenever you do these tweet about them and tag me It's always better to learn in public .</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, understanding the shell and scripting is essential for any computer scientist, as it allows us to expand our skill set and accomplish more than ever before. The shell serves as a mediator between the user and the operating system, allowing us to perform tasks that may not be possible through the graphical user interface. By using the shell, we can interact with the kernel of our operating system and execute powerful commands.</p>
<h3 id="heading-connect-with-me"><strong>Connect with Me</strong></h3>
<p>If you enjoyed this post and would like to stay updated on my work, feel free to connect with me on social media</p>
<ul>
<li><p>Linkedin: Click <a target="_blank" href="https://www.linkedin.com/in/bhardwajvaibhav42/"><strong>here</strong></a></p>
</li>
<li><p>Twitter: Click <a target="_blank" href="https://twitter.com/__vxibhxv"><strong>here</strong></a></p>
</li>
</ul>
<p>I'm always open to new connections and networking opportunities, so don't hesitate to reach out and say hello. Thank you for reading!</p>
</br><p style="color:#757CF9;">This article is published w/ <a target="_blank" href="https://scattr.io?ref=dev">Scattr  ↗️</a></p>