Scripting 101: The missed part of your Computer Science Education

Scripting 101: The missed part of your Computer Science Education

Basics and Beyond of Bash Scripting Part 1

·

8 min read

Shell We Begin?

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.

This series is an attempt to change how you work

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

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.

Shell, What is it?

If you read the first line of the blog carefully it says Shell we begin 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.

but

We already have GUI and everything programmed why bother with the command line and shell scripting?

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.

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.

Interacting with Shell

Upon launching your terminal, it is common to encounter a prompt that may resemble the following.

vaibhav:~$
  • This prompt provides information about the machine you're using (such as the name "vaibhav") and displays your current working directory (often indicated by "~" to represent the home directory).

  • The "$" 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.

vaibhav:~$ whoami

If you execute the "whoami" command in the Bash shell and you are logged in as a user named "vaibhav", the output will be

vaibhav:~$ whoami
vaibhav

Let's try some more commands

vaibhav:~$  echo this
this

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.

But how does he shell know how to find the this or echo 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 environment variable called $PATH that lists which directories the shell should search for programs when it is given a command:

vaibhav:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

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.

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.

Navigating the shell

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:).

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.

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.

vaibhav:~$ pwd
/home/vaibhav
vaibhav:~$ cd /home
vaibhav:/home$ pwd
/home
vaibhav:/home$ cd ..
vaibhav:/$ pwd
/
vaibhav:/$ cd ./home
vaibhav:/home$ pwd
/home
vaibhav:/home$ cd vaibhav
vaibhav:~$ pwd
/home/vaibhav
vaibhav:~$ ../../bin/echo hello
hello

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.

To see what lives in a given directory, we use the ls command:

vaibhav:~$ ls
vaibhav:~$ cd ..
vaibhav:/home$ ls
vaibhav
vaibhav:/home$ cd ..
vaibhav:/$ ls
bin
boot
dev
etc
home
...

Man Command the Saviour

MAN 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.

When you type man followed by the name of a command or topic, the man command searches for and displays the corresponding manual page. For example, to view the manual page for the ls command, you would enter:

man ls

This would display the manual page for the ls command, which includes information about its usage, options, and examples.

Some commands for you to explore

  1. mv - (to rename/move a file)

  2. cp - (to copy a file)

  3. mkdir - (to make a new directory)

Exercises

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

  1. Install WSL from here. - 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.

  2. Look up the touch program. The man program is your friend.

  3. Use the mkdir command to create a new directory called my_folder.

  4. Use the cd command to navigate to my_folder.

  5. Use the touch command to create two new files called file1.txt and file2.txt.

  6. Use the ls command to verify that the two new files were created.

  7. Use the echo command to write some text to file1.txt.

Whenever you do these tweet about them and tag me It's always better to learn in public .

Conclusion

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.

Connect with Me

If you enjoyed this post and would like to stay updated on my work, feel free to connect with me on social media

  • Linkedin: Click here

  • Twitter: Click here

I'm always open to new connections and networking opportunities, so don't hesitate to reach out and say hello. Thank you for reading!

Did you find this article valuable?

Support Vaibhav by becoming a sponsor. Any amount is appreciated!