Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


RHCSA - Create Simple Shell Scripts: Using Looping Constructs (for, until & while) to Process File, Command Line Input

In Linux, looping constructs such as for, until, and while are used to repeatedly execute code blocks based on specific conditions. These constructs are useful for processing file contents or command line input iteratively.

For Loop

The for loop allows you to iterate over a list of items and perform actions on each item. Here's the basic syntax of a for loop:

for item in list
do
    # code to execute for each item
done

In the above syntax, list represents the items over which you want to iterate. The item variable holds the current item in each iteration, and you can perform operations on it within the loop. The code block between do and done is executed for each item in the list.

Iterating over a list of directories to find files greater than 500K in size:

for dir in /usr/share/doc /var/log /etc
do
    find $dir -type f -size +500K -exec ls -lh {} \; 2> /dev/null
done

The find command searches a different folder ($dir) with each loop the variable resolving to an item in the list (/usr/share/doc, /var/log & /etc). -type f means files, -size +500K means files greater than 500K and -exec ls -lh {} \; means to run the ls command against each file found to give a long listing format to confirm the file size.

For each service, show the status:

for service in sshd chronyd firewalld
do
    systemctl status $service 
done

The systemctl status command gets run for each service ($service) in the list, displaying the status.

Until Loop

The until loop executes a code block repeatedly until a specified condition becomes true. Here's the basic syntax of an until loop:

until condition
do
    # code to execute until the condition is true
done

In the above syntax, condition is an expression that evaluates to either true or false. The code block between do and done is executed repeatedly as long as the condition is false. Once the condition becomes true, the loop terminates.

Printing numbers from 1 to 5 using an until loop:

counter=1

until [ $counter -gt 5 ]
do
    echo $counter
    sleep 2
    (( counter++ ))
done

The counter variable is initialized to 1. The loop continues to execute as long as the value of counter is not greater than 5. The value of counter is printed in each iteration, and it is incremented using the (( counter++ )) expression.

Print a message until file is removed:

# create file for exercise
> /tmp/remove_me.txt

until [ ! -f /tmp/remove_me.txt ]
do
    echo "File /tmp/remove_me.txt needs removing $(date)"
    sleep 5
done

The ! -f /tmp/remove_me.txt code means if NOT a file called /tmp/remove_me.txt, therefore the loop will keep printing that the file exists until it is removed... So open another terminal and remove it!

While Loop

The while loop executes a code block repeatedly as long as a specified condition remains true. Here's the basic syntax of a while loop:

while condition
do
    # code to execute while the condition is true
done

In the above syntax, condition is an expression that evaluates to either true or false. The code block between do and done is executed repeatedly as long as the condition is true. Once the condition becomes false, the loop terminates.

Printing numbers from 1 to 5 using a while loop:

counter=1

while [ $counter -le 5 ]
do
    echo $counter
    sleep 2
    (( counter++ ))
done

The counter variable is initialized to 1. The loop continues to execute as long as the value of counter is less than or equal to 5. The value of counter is printed in each iteration, and it is incremented using the (( counter++ )) expression.

To process the contents of a file line by line, you can use the while loop in combination with the read command.

Search for accounts assigned the /sbin/nologin shell:

while IFS= read -r line
do
    echo $line | grep '/sbin/nologin'
done < /etc/passwd

The while loop reads each line from the /etc/passwd file and assigns it to the line variable. The code block within the loop processes each line, and in this case, searches for accounts assigned the /sbin/nologin shell.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.