Operating Systems & Administration

[Day 6] 3. Shell Scripting again

D
DITISS Admin
••4 min read

Types of Loops in Shell Scripting

  1. For Loop
  2. While Loop
  3. Until Loop
  4. Select Loop

1. While Loop

A

while
loop repeats as long as the condition remains true. The loop structure allows for initialisation, execution, and modification of variables.

Syntax:

initialisation
while [ condition ]
do
body

modification
done

  • Break and Continue statements can be used to exit the loop or skip iterations, respectively.

2. Until Loop

The

until
loop is similar to the
while
loop, but the body of the loop executes when the condition is false. Once the condition becomes true, the loop terminates. This is the reverse of how the
while
loop works.

Syntax:

initialisation
until [ condition ]
do
body

modification
done

  • Break and Continue statements can also be used in
    until
    loops.
  • This type of loop is often preferred for menu-driven programs.

3. For Loop

The

for
loop allows iteration over a sequence or a range of values.

C-Style For Loop Syntax:

for (( initialisation; condition; modification ))
do
body
done

  • This syntax works similarly to
    for
    loops in languages like C.
  • You can use operators like
    <
    ,
    >
    ,
    <=
    ,
    >=
    in the condition.

Python-Style For Loop Syntax:

In shell scripting,

for
loops can also iterate over a sequence of values:

for var in sequence
do
body
done

  • Example:
    for i in {1..10}
    will loop from 1 to 10.

4. Select Loop

A

select
loop is used to create a simple menu-driven system in a shell script. It presents a numbered list of choices and prompts the user to select one.

Syntax:

select var in option1 option2 option3
do
 	
body
  
done`

  • 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

seq
command is used to generate sequences of numbers.

Examples:

  • seq 10
    : Prints numbers from 1 to 10.
  • seq 11 20
    : Prints numbers from 11 to 20.
  • seq 11 2 20
    : Prints numbers from 11 to 20 with a step of 2.
  • seq 11 -1 5
    : Prints numbers from 11 down to 5 (decrementing by 1).

File Conditionals

File conditionals are used to check properties of files, such as existence, type, and permissions.

Common File Conditionals:

  • -e $name_or_path
    : True if the file or path exists.
  • -f $name
    : True if the file is a regular file.
  • -d $name
    : True if the file is a directory.
  • -p $name
    : True if the file is a named pipe.
  • -s $name
    : True if the file is not empty.
  • -l $name
    : True if the file is a symbolic link.
  • -c $name
    : True if the file is a character special file.
  • -b $name
    : True if the file is a block special file.
  • -r $name
    : True if the file has read permission.
  • -w $name
    : True if the file has write permission.
  • -x $name
    : True if the file has execute permission.

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...