DEV Community

Cover image for Introduction to Shell Scripting (Day 8)
Kenneth Mahon
Kenneth Mahon

Posted on

Introduction to Shell Scripting (Day 8)

Still on this journey of becoming a DevOps Engineer in 120Days, i bring to you my Day 8 learning process.

what is shell scripting?

Shell scripting refers to writing scripts or programs in a shell language, such as Bash (Bourne Again SHell) on Linux or Unix-based systems. Shell scripting allows you to automate and execute a series of commands, perform complex tasks, and create scripts to streamline repetitive tasks.

Shell scripts are plain text files containing a sequence of commands and instructions that are interpreted by the shell. They can include control structures (such as loops and conditionals), variables, functions, and more, providing powerful capabilities for scripting and automation. Shell programming can be accomplished by directly executing shell commands at the shell prompt or by storing them in the order of execution, in a text file, called a shell script, and then executing the shell script. To execute, simply write the shell script file name, once the file has execute permission (chmod +x filename).
shell and bash terms are often used interchangeable. Bash is a shell programing language in which we can write shell command.

Use cases of Shell Scripting

Some common use cases for shell scripting include:

  • Automation: Shell scripts are often used to automate repetitive tasks or complex workflows, such as system administration tasks, backups, log analysis, and software deployments.

  • System Configuration: Shell scripts can be used to configure and customize the settings of a system or application. For example, you can write a shell script to set up environment variables, install software packages, or modify system configurations.

  • Data Processing: Shell scripts can process and manipulate data, such as parsing log files, extracting information from text files, performing calculations, or generating reports.

  • Task Sequencing: Shell scripts can be used to define the order and dependencies of multiple commands or scripts, allowing you to create more sophisticated workflows.

Shell scripting provides a flexible and efficient way to automate tasks and create custom solutions in a command-line environment. It leverages the power of the shell and its built-in utilities to accomplish various tasks, making it a fundamental skill for Linux and Unix system administrators, developers, and power users.

Different shell implementation

The first line of the shell script file begins with a "sha-bang" (#!).

: in musical notation called "sharp"

!: also called "bang"

#! is not read as a comment, followed by the full path where the shell interpreter is located. This path, tells the operating system that this file is a set of commands to be fed into the interpreter indicated. The first line may look like this:

!/bin/bash

or

!/usr/bin/bash

depends on your path

Adding comments: any text following the "#" is considered a comment

To find out what is currently active shell, and what is its path, type the below command

ps | grep $$

Response

This response shows that the shell you are using is of type 'bash'.

next find out the full path of the shell interpreter

which bash

output

This response shows the full execution path of the shell interpreter. Make sure that the "sha-bang" line at the beginning of your script, matches this same execution path.

All shell files have the same .sh file extention
Take Note
file.sh ==== shell script
file.py ==== python
file.js ==== javascript
we will be using our text editor a lot here. remember the "vim editor and Echo command" we spoke about when dealing with Linus commands, we will making use of it a lot

let me share the little practices i did today

firstly, use the vim editor to create a file
"vim class.sh"
Now let print "Hello World" using the vim editor and echo command

./name of file is use to execute script

output
if you notice, i got a bash error that says "ACESS DENIED". this is because newly created files don't have the ability to be executed. so what do we do? we will give user access to be able to execute the file by using the "sudo chmod" we learnt days ago. now process to execute the script.

Output

Yes we have "Hello world" printed. simple right???

Input

output

Day 9 loading .....

Resources:
basic shell scripting
shell scripting tutorial

Top comments (0)