Operating Systems & Administration
[Day 6] 3. Shell Scripting again
D
DITISS AdminTypes of Loops in Shell Scripting
- For Loop
- While Loop
- Until Loop
- Select Loop
1. While Loop
A
whileSyntax:
initialisationwhile [ condition ]dobodymodificationdone- Break and Continue statements can be used to exit the loop or skip iterations, respectively.
2. Until Loop
The
untilwhilewhileSyntax:
initialisationuntil [ condition ]dobodymodificationdone- Break and Continue statements can also be used in loops.
until - This type of loop is often preferred for menu-driven programs.
3. For Loop
The
forC-Style For Loop Syntax:
for (( initialisation; condition; modification ))dobodydone- This syntax works similarly to loops in languages like C.
for - You can use operators like ,
<,>,<=in the condition.>=
Python-Style For Loop Syntax:
In shell scripting,
forfor var in sequencedobodydone- Example: will loop from 1 to 10.
for i in {1..10}
4. Select Loop
A
selectSyntax:
select var in option1 option2 option3do - Each option in the list is displayed as a menu.
- It works well for interactive scripts and uses simple/compound commands for execution based on user input.
seq Command
The
seqExamples:
- : Prints numbers from 1 to 10.
seq 10 - : Prints numbers from 11 to 20.
seq 11 20 - : Prints numbers from 11 to 20 with a step of 2.
seq 11 2 20 - : Prints numbers from 11 down to 5 (decrementing by 1).
seq 11 -1 5
File Conditionals
File conditionals are used to check properties of files, such as existence, type, and permissions.
Common File Conditionals:
- : True if the file or path exists.
-e $name_or_path - : True if the file is a regular file.
-f $name - : True if the file is a directory.
-d $name - : True if the file is a named pipe.
-p $name - : True if the file is not empty.
-s $name - : True if the file is a symbolic link.
-l $name - : True if the file is a character special file.
-c $name - : True if the file is a block special file.
-b $name - : True if the file has read permission.
-r $name - : True if the file has write permission.
-w $name - : True if the file has execute permission.
-x $name
Read Next
8/26/2026
[Day 7] 1. Dual Mode Operation of OS
### **Differentiating OS Processes and User Processes** - **User processes** are the regular programs you run as a user, like opening a text editor...
8/26/2026
[Day 7] 2. Hard Link and Symbolic Link
### **Hard Link** A **hard link** is essentially another name for the same file. It doesn’t create a new file; instead, it creates an additional di...