Working With Shells, Scripting

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Working with shells, Scripting

GUI and CLI

Graphical User Interface(GUI) - GUI shell provides


a graphical interface for users to interact with the OS,
instead of typing commands, users interact with icons,
windows, menus,and buttons using a mouse or
touch input.

Ex :Windows Explorer,macOS Finder, Linux GNOME.

Command-line Shells - text-based interface for


interacting with an operating system or a computer
system through a command-line interface(CLI).
It allows users to enter text commands, typically
in the form of text strings, and receive text-based
responses from the system
GUI and CLI flows:

GUI command flow: CLI command flow:


●User Input ●User Input
●Graphical Interface ●Shell
●GUI Libraries ●Shell Built-ins
●Application Logic ●External Programs
●OS API ●OS API
●System Calls ●System Calls
●Kernel ●Kernel
●Hardware ●Hardware
●Response ●Response
●User Feedback ●Program Output
●User Feedback
Linux terminal

Command lines - powerful tools in Linux;


basis on which many of the GUI tools are
built. They can be accessed without the help
of a GUI, they can be scripted.

terminal - text-based interface that allows you


to interact with your computer using text
commands, aka "command line interface"
(CLI) or "shell." The terminal provides a way
for users to input commands and receive
text-based responses from the operating
system.

Bash("Bourne-Again Shell") - widely used Unix shell(command-line interpreter), in the


Unix and Linux operating systems. It is the default shell for many Linux distributions and
macOS.
4
Windows Shells:

Windows Command Prompt - "cmd" or "Command


Prompt," is a command-line interpreter application
available in most versions of the Microsoft Windows
OS. It provides a text-based interface for interacting
with the OS, running commands, and performing
various tasks.

Windows PowerShell - powerful command-line shell


and scripting language developed by Microsoft.
Designed to help administrators and advanced
users automate tasks and manage Windows systems
More efficiently.

5
Bash scripting: run script

.sh file extension is commonly used to denote


shell script files in Linux and other OS. Shell
scripts are plain text files containing a series
of commands and instructions that are executed
by a shell interpreter, such as Bash or another
compatible shell.

'sleep' - command used to introduce a delay


Or pause in the execution of a shell script
or command.
template: sleep time_in_seconds

6
Bash scripting: make script executable
The "shebang" - aka "hashbang" or "pound-bang."
special line of text that appears at the very beginning
of a script or executable file. The shebang line is
used to specify the interpreter that should be used
to execute the script or program.
shebang template: #!/path/to/interpreter

Chmod - command in Linux is used to change the


permissions (file mode) of files and directories. It
allows user to specify who can read, write, and
execute a file or directory

After making it executable, you can run the


script by specifying its filename ./file_name.txt

7
Bash scripting: variables

variables - used to store data and information that


can be accessed and manipulated within a shell
session. Shells like bash, allow you to work with
variables to store and manage data, which can be
text strings, numbers, or any other type of information

To access the value stored in a variable, you can


use the $ symbol followed by the variable name

8
Bash scripting: variables

Shell arguments, or command-line arguments or


positional parameters, are values that are passed to a
shell script or a command when it is executed in a
terminal or shell. These arguments provide a way to
pass input data or options to a script or command,
allowing them to be more flexible and customizable

To capture the output of a command and store it in


A variable in a Bash script, you can use command
substitution by using $(some_linux_command).
example: user=$(whoami)
9
Bash scripting: Exporting Variables

'export' - command to make a variable available


as an environment variable. Environment variables
Are accessible to child processes, subshells, and
other commands invoked from the current shell
Session. example: export default_user='root'

exported variables are available to child processes


and scripts but do not affect the parent shell's
environment. To make an environment variable
permanent and available across sessions, add the
Export command to your shell's startup files
Like in ~/.bashrc

10
Bash scripting: redirecting outputs

>' operator - used to redirect the standard output


(stdout) of a command to a file. If the file
specified does not exist, it will be created.
If it already exists, its content will be overwritten.

'>>' operator - used to append the standard output


(stdout) of a command to a file. If the file
specified does not exist, it will be created.
If it already exists, the new output will be added
to the end of the file, preserving the existing content.

11
Pipes, grep:

grep(Global Regular Expression Print) – command-line


utility in Linux that is used for searching text or regular
expressions in files. grep allows you to search for
patterns within one or more files or to search for text
piped from another command. It is commonly used for
text processing and data extraction tasks.

In Linux, the pipe (|) is an operator used to connect the


standard output of one command to the standard input
of another command. Allows users to create a chain of
commands where the output of one command serves
as the input to the next command. This is known as
"piping" commands together.
template: command1 | command2 | command3 | ...

12
Scripting in Windows:

Powershell Scripting - PowerShell allows you


to write scripts that can automate complex tasks.
You can create .ps1 script files that contain a
sequence of PowerShell commands.

Cmdlets -PowerShell uses cmdlets (command-lets)


as its fundamental units of operation. Cmdlets are
small, task-specific commands that can be used to
perform various operations. ex: Get-ChildItem
is equivalent of Linux ls command

13

You might also like