KEMBAR78
Lab 7 - Bash Scripting | PDF | String (Computer Science) | Command Line Interface
0% found this document useful (0 votes)
4 views12 pages

Lab 7 - Bash Scripting

This document outlines Lab 7 for the Bash Scripting course at Zewail City of Science and Technology, focusing on writing and executing Bash scripts. It covers key concepts such as invoking scripts, using variables, handling user input, and implementing control structures like if statements and loops. Additionally, it includes a requirement for students to create a function that checks if a string is a palindrome, emphasizing string operations and user input handling.

Uploaded by

hassan63ahmed12
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)
4 views12 pages

Lab 7 - Bash Scripting

This document outlines Lab 7 for the Bash Scripting course at Zewail City of Science and Technology, focusing on writing and executing Bash scripts. It covers key concepts such as invoking scripts, using variables, handling user input, and implementing control structures like if statements and loops. Additionally, it includes a requirement for students to create a function that checks if a string is a palindrome, emphasizing string operations and user input handling.

Uploaded by

hassan63ahmed12
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
You are on page 1/ 12

Zewail City of Science and Technology

University of Science and Technology


Communication and Information Engineering Program
Operating Systems (CIE 302)

Lab 7 – Bash Scripting


Introduction
Think of a script for a play, or a movie, or a TV show. The script tells the actors what they
should say and do. A script for a computer tells the computer what it should do or say. In
the context of Bash scripts we are telling the Bash shell what it should do. The purpose of
this lab is intended to give you a solid foundation in how to write Bash scripts which is a
collection of command lines stored in a text file.

An important point to remember though is:

“Anything you can run normally on the command line can be


put into a script and it will do exactly the same thing.
Similarly, anything you can put into a script can also be run
normally on the command line and it will do exactly the same
thing.”

Spring 2020 CIE 302 - Lab #7 1/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

Invoking Bash Script


 Create a file and type bash commands inside it.
 Use chmod command to make the script executable.
 Then run the script as if it were a command.

Here are the contents of script2:

• Line 1 - The hash exclamation mark ( #! ) character sequence is referred to as the


Shebang. Following it is the path to the interpreter (or program) that should be
used to run (or interpret) the rest of the lines in the text file.
• Line 2 - This is a comment. Anything after # is not executed. It is for our reference
only.
• Line 3- Is the command echo which will print a message to the screen.

Variables
There are two actions we may perform on variables inside a bash script:

• Setting a value of a variable. Set the value to the variable name directly using =
sign.

(Note there is no space on either side of the equals ( = ) sign.)

• Reading the value of a variable.  Proceed the variable name with a $ sign.

Spring 2020 CIE 302 - Lab #7 2/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

Bash Script Arguments:

To use the past arguments inside the script text:

• Use the variables $1 to represent the first command line argument, $2 to


represent the second command line argument and so on.

To pass arguments to bash script:

• Write the arguments separated by space beside the script name on the command
line.

There are a few other variables that the system sets for you to use as well.

$0 - The name of the Bash script.


$1 - $9 - The first 9 arguments to the Bash script.
$# - How many arguments were passed to the Bash script.
$@ - All the arguments supplied to the Bash script.
$? - The exit status of the most recently runs process.
$$ - The process ID of the current script.
$USER - The username of the user running the script.
$HOSTNAME - The hostname of the machine the script is running on.

Spring 2020 CIE 302 - Lab #7 3/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

Quotes and substitutions:

When we want variables to store more complex values however, we need to make use of
quotes. This is because under normal circumstances Bash uses a space to determine
separate items.

 Remember, commands work exactly the same on the command line as they do
within a script.

When we enclose our content in quotes we are indicating to Bash that the contents
should be considered as a single item. You may use single quotes ( ' ) or double quotes
( " ).

Single quotes will treat every character literally.


Double quotes will allow you to do substitution (that is including variables within
the setting of the value).

Command substitution $(command) allows us to take the output of a command or


program (what would normally be printed to the screen) and save it as the value of a
variable. To do this we place it within brackets, preceded by a $ sign.

Spring 2020 CIE 302 - Lab #7 4/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

User Input
If we would like to ask the user for input then we use a command called read. This
command takes the input and will save it into a variable.

Arithmetic
There are several ways to go about arithmetic in Bash scripting.

let expression expr expression $(( expression ))


Make a variable equal to an Print out the result of the Return the result of the
expression. expression. expression.
let a=5+4 # There must be spaces # We may space it out nicely for
between the items and no readability without the need for
#Or spaceout the expression quotes. Otherwise the quotes.
with quotes expression will not be a=$((3+5))
let "a = 5 + 4" evaluated but printed. echo $a # 8
expr 5 + 4

Spring 2020 CIE 302 - Lab #7 5/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

If Statements
If statements allow us to make decisions in our Bash scripts.

Basic If Statement

Nested If statements

If Else

Spring 2020 CIE 302 - Lab #7 6/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

If Elif Else

Boolean Operations

Case Statements

Spring 2020 CIE 302 - Lab #7 7/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

Note: The square brackets ([ ]) in the if statement above are actually a reference to the
command test. This means that all of the operators that test allow may be used with if
condition as well.

Spring 2020 CIE 302 - Lab #7 8/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

Loops
Loops allow us to take a series of commands and keep re-running them until a particular
situation is reached.

While Loops

For Loops

Ranges

Spring 2020 CIE 302 - Lab #7 9/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

Functions
Creating a function in bash scripts is fairly easy.

A few points to note:

In other programming languages it is common to have arguments passed to the


function listed inside the brackets (). In Bash they are there only for decoration
and you never put anything inside them.
The function definition ( the actual function itself) must appear in the script before
any calls to the function.

Passing Arguments

return status

Spring 2020 CIE 302 - Lab #7 10/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

Variable Scope

Basic String Operations


The shell allows some common string operations which can be very useful for script
writing using ${} notation.

String Length

Substring Extraction
Extract substring of length 1 from $name starting after position 2. Note that first
position is 0.

Spring 2020 CIE 302 - Lab #7 11/12


Zewail City of Science and Technology
University of Science and Technology
Communication and Information Engineering Program
Operating Systems (CIE 302)

Substring Replacement
Replace first occurrence of substring with replacement

Replace all occurrences of substring

Requirement:
A string is a palindrome if you can read it from left to right as you read it from right to left

1. You’re required to write a function that prints 1 if the string is a palindrome and 0
otherwise
2. Your script should read an input from the user and pass it to the function

P.S. Your code should handle mixed lower and capital letters, you should solve this using
string operations, however there’s a faster way already know from the second lab, you
can convert them all UpperCase or All lower case.

Example for running the script:

References:
https://www.learnshell.org/en/Basic_String_Operations

https://ryanstutorials.net/bash-scripting-tutorial/

Spring 2020 CIE 302 - Lab #7 12/12

You might also like