0% found this document useful (0 votes)
2 views

Ch11 Shell Programming

The document provides an overview of shell programming, explaining the roles of the kernel and shell, including command-line and graphical interfaces. It discusses the purpose of shell scripts, input/output techniques, and the distinction between system-defined and user-defined variables. Additionally, it covers how to define and access variables within shell scripts.

Uploaded by

samtnkn0711
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Ch11 Shell Programming

The document provides an overview of shell programming, explaining the roles of the kernel and shell, including command-line and graphical interfaces. It discusses the purpose of shell scripts, input/output techniques, and the distinction between system-defined and user-defined variables. Additionally, it covers how to define and access variables within shell scripts.

Uploaded by

samtnkn0711
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

BASICS OF SHELL

PROGRAMMING
INTRODUCTION

• Kernel
The computer program that forms the core of your computer's
operating system.
It is responsible for managing the following resources:

1. File management
2. Process management
3. I/O management
4. Memory management
INTRODUCTION

• Shell
It plays the role of converting the human-readable commands from the
user to kernel language.
There are two classifications of shell:

1. Command-Line Shell: Users can access this shell through a


command-line interface.
2. Graphical shell: Users can manipulate programs using a graphical
user interface (GUI) through this shell.
COMMAND LINE INTERFACE
GRAPHICAL (GUI) INTERFACE
WHAT IS SHELL PROGRAMMING?

• Shells are interactive, and they execute commands after accepting


them as input from the user.
• The files which contain commands as inputs in a file are known as shell
programs or shell scripts.
• A shell program, sometimes referred to as a shell script, is simply a
program constructed of shell commands.
• The shell scripts have a similar syntax to other programming
languages.
WHY SHELL SCRIPTS?

• Writing shell scripts is much quicker


• Quick start
• To avoid repetitive work and automation
• System admins use shell scripting for routine backups
• System monitoring
INPUT/OUTPUT TECHNIQUES

• STDIN
The STDIN file descriptor is used to reference standard inputs to the
shell.
The input redirect signal (<) commands the shell to replace the file
referenced in the redirection with the standard file descriptor.
It will retrieve the file's data, just like the user would have typed on the
keyboard.
INPUT/OUTPUT TECHNIQUES

• STDOUT
The STDOUT file descriptor is used for referencing the standard output
in the shell.
The standard output for a terminal interface is the terminal monitor.
The STDOUT file descriptor receives outputs from most bash
commands by default.
 However, programmers can change redirect the output by using
output redirection.
INPUT/OUTPUT TECHNIQUES

• STDERR
STDERR file descriptor is the one that the shell uses to handle error
messages.
It is used to reference the standard error output.
STDERR will redirect all the error messages to the same place as
STDOUT.
Hence, all error messages will go to the monitor by default.
INPUT/OUTPUT TECHNIQUES
SYSTEM-DEFINED VARIABLES

• They are variables that the Linux operating system creates and
maintains.
• The command “$ set“ is used to display these variables.
System Defined Variables Meaning
BASH=/bin/bash Shell Name
BASH_VERSION=4.1.2(1) Bash Version
COLUMNS=80 No. of columns for the screen
HOME=/home/apjtech Home Directory of the User
LINES=25 No. of columns for the screen
LOGNAME=APJTECH Logging name
USER-DEFINED VARIABLES

• They are the variables that the users define.


• The shell script allows the programmers to define their unique set of
variables.
• They are used for storing data temporarily and using it throughout the
script.
• They can be a text string of maximum:
1. 20 Letters
2. 20 Digits
3. An underscore character
HOW TO DEFINE A VARIABLE?

• Users can define a variable using the ‘=’ operator, followed by


assigning a value to a variable name.
• A variable name refers to a series of alphanumeric characters that start
with '_' or a letter.
• The shell script treats all variables as text strings unless the user
specifies them to be treated as numeric.
• In case the user needs to include spaces in-between values, they can
surround it by double-quotes.
HOW TO DEFINE A VARIABLE?

$
myvar=greetings $ myvar1=”greetings all!”
$ mycount=1
HOW TO ACCESS A VARIABLE?

• The shell script de-references a variable name by adding the prefix '$.’
• To print the variable's value, use the 'echo' command.

$ echo $myvar
greetings
HOW TO ACCESS A VARIABLE?

• If there is the ‘expr’ command, the script will treat the variable as a
numeric value.
• In case the variable is not followed by a space, surround it with spaces.

$ expr $mycount + 2 $ echo ${myvar}ss


3 greetingsss
HOW TO ACCESS A VARIABLE?

• Special characters, for example, ‘$’ retain their meaning in double-


quotes.
• Single quotes can be used if you want to treat special characters in
their literal sense.
$ myvar2=”$myvar $ myvar3=’$myvar
everyone!” everyone!’
$ echo $myvar2 $ echo $myvar3
greetings $myvar everyone!
everyone!

You might also like