0% found this document useful (0 votes)
8 views21 pages

Shell Scripting Basics Overview

This document contains lecture slides about shell scripting basic concepts presented by Dr. Sumalatha Aradhya of the Computer Science department at SIT Tumkur. The slides cover topics such as the shell environment, echo command, variable assignment with set, accessing variable values with $, meta characters, exporting variables between parent and child shells, backslash escaping, and single/double quotes. Examples are given for integer, floating point, and string arithmetic operations in shell scripts.

Uploaded by

Varsha Gowda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

Shell Scripting Basics Overview

This document contains lecture slides about shell scripting basic concepts presented by Dr. Sumalatha Aradhya of the Computer Science department at SIT Tumkur. The slides cover topics such as the shell environment, echo command, variable assignment with set, accessing variable values with $, meta characters, exporting variables between parent and child shells, backslash escaping, and single/double quotes. Examples are given for integer, floating point, and string arithmetic operations in shell scripts.

Uploaded by

Varsha Gowda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SHELL SCRIPTING BASIC CONCEPTS - environment

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 1
SHELL SCRIPTING BASIC CONCEPTS - echo

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 2
SHELL SCRIPTING BASIC CONCEPTS - echo

(Assigning temporary default values)

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 3
SHELL SCRIPTING BASIC CONCEPTS - set

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 4
SHELL SCRIPTING BASIC CONCEPTS - $

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 5
SHELL SCRIPTING BASIC CONCEPTS – Meta Characters

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 6
SHELL SCRIPTING BASIC CONCEPTS – access values

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 7
SHELL SCRIPTING BASIC CONCEPTS – fetch value

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 8
SHELL SCRIPTING BASIC CONCEPTS – fetch value

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 9
SHELL SCRIPTING BASIC CONCEPTS – export – Parent and Child Shell environment

24-01-2023 10
Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur
SHELL SCRIPTING BASIC CONCEPTS – BackSlash \

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 11
SHELL SCRIPTING BASIC CONCEPTS – Single Quotes ‘

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 12
SHELL SCRIPTING BASIC CONCEPTS – Double Quotes “

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 13
Command Substitution

• When a command is enclosed in backquotes, it will be executed and its output returned.
• This process is called command substitution.
• It is used when assigning the output of a command to a variable or when substituting the output of a command within a
string.
• All three shells use backquotes to perform command substitution

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 14
24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 15
Aim: 1. Write a shell script to perform the following string
operations:
a) integer arithmetic operations

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 16
24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 17
Aim: Write a shell script to perform the following string
operations:
i) Floating Point arithmetic operations

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 18
Aim: Write a shell script to perform the following string operations:
Integer Operation – find power of a number

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 19
Aim: Write a shell script to perform the following string operations:
I) To extract a sub-string from a given string.
II)To find the length of a given string.

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 20
1b. Write a shell program to find out reverse string of the given string

24-01-2023 Lecture ppt by Dr. Sumalatha Aradhya, Dept of CSE, SIT, Tumkur 21

Common questions

Powered by AI

In shell scripting, the backslash '\' is used as an escape character, which allows for treating special characters as ordinary characters within strings. This is necessary when dealing with special characters like quotes or meta-characters that should not be interpreted by the shell. For example, to print a string containing a double quote, one could use echo "This is a \"quoted\" word" .

To write a shell script to find the power of a number using integer operations, one could use a loop to multiply the base by itself exp times. An example script might use a counter that starts at zero and increments until it reaches the exponent, multiplying an accumulator by the base in each iteration: ``` base=2 exp=3 result=1 while [ $exp -gt 0 ]; do result=$((result * base)) exp=$((exp - 1)) done echo $result ``` This script calculates 2^3 .

Command substitution is a feature in shell scripting that allows the output of a command to replace the command itself in the script. It is implemented using backquotes (``) or the $(...) syntax. This is useful for assigning the result of a command to a variable or for embedding command outputs within strings. For instance, `date` will output the current date when used within a command .

Single quotes ('') preserve the literal value of each character enclosed within them, preventing variable expansion and command substitution. Double quotes ("") allow for variable expansion and command substitution within the quoted string. For example, given `var=world`, `echo '$var'` will output "$var" while `echo "$var"` will output "world". Thus, single quotes are used for strings where interpretation of special characters is not desired, whereas double quotes are used when such interpretation is necessary .

Meta characters in shell scripting are special characters that have specific meanings, which allow the user to control the behavior of the shell. For instance, characters like '*', '?', and '[]' are used for pattern matching during file search operations. Meta characters are crucial because they enable users to execute complex commands and can control the input/output redirection, which is fundamental in scripting for automating tasks .

Writing shell scripts for tasks such as reversing strings or finding string lengths automates processes that may otherwise be repetitive and prone to human error. Automating these tasks enhances productivity, ensures consistency, and allows for the processing of large volumes of data efficiently. The scripting approach can be integrated into larger workflows for comprehensive data manipulation and management, leveraging the power of command-line utilities to handle complex operations with ease .

Shell scripts inherently lack robust support for floating point arithmetic as they primarily handle integer operations. This poses accuracy and functionality challenges when performing precise numerical computations. Solutions include using external tools like 'bc' or 'awk', which support floating point operations. For example, `echo "3.5 * 4.2" | bc` would use 'bc' to perform multiplication. This workaround involves careful script design to interface between the shell and these utilities to achieve accurate results .

The 'export' command in shell scripting is used to make a variable available in the environment of subprocesses spawned from that shell (i.e., the child processes). When a variable is exported, it becomes accessible not just within the current shell, but also in any child shells that might be created. This is critical for setting environment variables that need to persist across different execution contexts .

The "echo" command in shell scripting is used to display text or the value of variables in the terminal. It plays a crucial role in assigning temporary default values as it can be used to output a message to inform users of the default values being used in a script. This is particularly important in scenarios where inputs are optional, and temporary defaults need to be stated explicitly .

Extracting a sub-string in shell scripting involves using parameter expansion or external utilities like 'cut' or 'awk'. The syntax for parameter expansion is: `${string:position:length}`. This allows you to specify the starting position and length of the substring to extract. For instance, `string="Hello World"` and extracting from position 6 with length 5 would be `${string:6:5}` resulting in "World". This operation is especially useful for processing and handling strings where specific segments need to be isolated .

You might also like