0% found this document useful (0 votes)
4 views12 pages

Lab 7

The document provides an overview of Bash scripting, explaining its purpose and how to create and execute scripts. It covers variable declaration, command arguments, user input, and arithmetic evaluation in Bash. Additionally, it includes useful resources for further learning on Bash scripting and preparation for the next lab.

Uploaded by

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

Lab 7

The document provides an overview of Bash scripting, explaining its purpose and how to create and execute scripts. It covers variable declaration, command arguments, user input, and arithmetic evaluation in Bash. Additionally, it includes useful resources for further learning on Bash scripting and preparation for the next lab.

Uploaded by

yehiasaaamir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Operating systems

Lab 7
Outline 1 Bash Scripting
2 Useful Resources
1 Bash Scripting
Bash Scripting
• In the previous labs we learned how to use the shell through different
commands and techniques.
• Consider this scenario: You wrote a few commands to delete all files
in a directory that start with numbers, because those files were
automatically generated and you don’t really care about them. Now
imagine that those files are generated every few days or even hours.
It would be boring to keep writing the same commands every time.
• It would be a lot easier if we could save those few commands
somewhere so we can re-run them whenever we want in the future.
• This concept is actually the definition of a bash script.
Bash Scripting
• Just like Python programs, we can write our own shell programs
using Bash. For instance, all of the commands we used before are a
combination of C code and Bash.
• Let’s create our first bash script! We navigate to a directory then
create a new file then open it with a text editor, or just create a new
file with nano in one step “nano script.sh”:

• Write the following code then save and exit:


Bash Scripting
•This means we can’t run our new script by just typing “script.sh”. Instead,
we write the script’s full path or just type “./script.sh”. Notice how we are
denied access. Let’s check our permissions with “ls –l script.sh”:

• We notice that neither the user, group or others have execution permission.
For the sake of this example, let’s allow everything by typing “chmod 777
script.sh” and rerun the script:
Bash Scripting
• Just like any other programming language, we use variables to store data.
Let’s take a look at this code:

• We declare variables like Python. We just write the variables name fol-
lowed by a “=” then a string, or number or “$(...)” that allows us to exe-
cute a command and take its output string and put it in a variable. We can
not use spaces when declaring variables.
• We can then use the declared variables in other places by following them
with the dollar sign “$”.
Bash Scripting
• Remember command arguments and parameters that we used to pass
with previous commands. These arguments are stored in their own
special variables ranging from “$1”, “$2” up till “$9”. “$0” is not an
argument because it’s the program’s name itself.
• Another way to take input from the user during the script’s runtime is
to use the statement “read”. Both methods are represented in the next
examples:
Bash Scripting
• In order to evaluate arithmetic expressions in Bash, we put it inside
“$((...))”. Arithmetic expressions can contain both numbers and
variables. Notice that “**” is the syntax for raising to a power:
2 Useful Resources
Useful resources
• More on bash scripting:
• https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
• https://www.learnshell.org
• https://ryanstutorials.net/bash-scripting-tutorial
• https://linuxconfig.org/bash-scripting-tutorial
• Bash cheatsheet:
• https://devhints.io/bash
• How to pass more than 9 parameters to a script:
• https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_07.html
• Those who want to prepare for the next lab should read about the fol-
lowing topics:
• Bash scripting (conditionals and loops).
Thank you
Eng. Jomana Ahmed

You might also like