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

Lab 8

The document outlines key concepts in Bash scripting, including conditionals, operators, and loops. It describes the syntax for conditionals, various operators for string and numeric comparisons, and different looping methods such as 'for', 'while', and 'until'. Additionally, it provides useful resources for further learning about Bash scripting.

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 8

The document outlines key concepts in Bash scripting, including conditionals, operators, and loops. It describes the syntax for conditionals, various operators for string and numeric comparisons, and different looping methods such as 'for', 'while', and 'until'. Additionally, it provides useful resources for further learning about Bash scripting.

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 8
Outline 1 Conditionals in Bash Scripting
2 Operators in Bash Scripting
3 Loops in Bash Scripting

4 Useful Resources
1 Conditionals in Bash Scripting
Conditionals in Bash Scripting

• Conditionals in bash follow this syntax “if [ expression ]; then ... fi”.
Syntaxes like “else” and “elif” are also supported:
2 Operators in Bash Scripting
Operators in Bash Scripting
• Like C, the logical operators “&&” and “||” are also supported in bash.

• For strings, we only used “=”, but other operators are supported too like
“!=”, “<”, “>”, “-n $var” which checks if $var is not empty, and
“-z $var” which checks if $var is empty.

• Regarding numbers, there are different operators used for comparison


inside conditionals:
• “-lt” meaning “less than”.
• “-gt” meaning “greater than”.
• “-le” meaning “less than or equal”.
• “-ge” meaning “greater than or equal”.
• “-eq” meaning “equal”.
• “-ne” meaning “not equal”.
3 Loops in Bash Scripting
Loops in Bash Scripting
• Regarding loops in our course,
we would be interested to iterate
over the output of some commands
like “ls” for example
• We can achieve this through this
code. Notice how we incremented
the variable $itemno with “let”. We
did this because it’s an arithmetic
operation, we could also replace
this line with “itemno=$
((itemno+1))”
Loops in Bash Scripting
• If we don’t want to loop over a certain command and we’d rather loop
for a fixed number of times, we can use the command “seq” that
stands for sequence:

• Two other valid looping syntaxes are “while” and “until”:


4 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
Thank you

You might also like