Bash Read Output of Command Into While Loop

Iii types of loops are used in bash programming. While loop is one of them. Similar other loops, a while loop is used to do repetitive tasks. This article shows how y'all can apply a while loop in a fustigate script by using unlike examples.

Syntax of while loop:

while [ condition ]
exercise
commands
washed

The starting and ending block of the while loop is defined by do and done keywords in the bash script. The termination condition is divers at the starting of the loop. Open a text editor to write a fustigate script and test the following while loop examples.

Example-1: Iterate the loop for a stock-still number of times

Create a bash file named while1.sh with the following content. Here, the loop will iterate 5 times and impress the counter value in each iteration.

#!/bin/fustigate

# Initialize the counter
n=1
# Iterate the loop for v times
while [ $northward -le 5 ]
do
# Print the value of due north in each iteration
echo "Running $due north time"
# Increment the value of north by one
( ( north++ ) )
done

Output:
The post-obit output will appear afterwards executing the higher up script.

Instance-2: Using intermission statement for conditional exit

the pause statement is used to get out from the loop early based on a particular condition. Create a bash file named while2.sh with the following lawmaking. Here, the loop is divers to iterate 10 times, but the iteration will be stopped when the counter value is half dozen.

#!/bin/bash

# Initialize the counter
due north=one
# Iterate the loop for 10 times
while [ $due north -le ten ]
practise
# Check the value of n
if [ $due north == half dozen ]
so
echo "terminated"
intermission
fi
# Print the current value of due north
echo "Position: $north"
# Increment the value of n by 1
( ( due north++ ) )
done

Output:

The following output will appear after executing the higher up script.

Example-3: Using continue statement to omit item pace

Create a bash file named while3.sh with the following code. In this example, the loop will iterate for 5 times, only it will not print all 5 positions. When the loop iterates for the 3rd time, the continue argument will be executed, and the loop will go for the next iteration without press the text of the tertiary position.

#!/bin/bash

# Initialize the counter
n=0
# Iterate the loop for 5 times
while [ $n -le 5 ]
do
# Increment the value of northward by 1
( ( n++ ) )

# Cheque the value of n
if [ $n == 3 ]
then
proceed
fi
# Print the current value of n
echo "Position: $n"

washed

Output:

The following output will appear later on executing the higher up script.

Example-4: Read the control-line statement with options

Create a bash file named while4.sh with the following code. Here, the loop is used to read the command-line arguments with options. The script will impress the formatted argument values after the execution if the three-argument values pass with the valid choice.

#!/bin/fustigate

# Read the command-line arguments values with pick using loop
while getopts n:a:e: OPT
practise
case "${OPT}"
in
north) name=${OPTARG} ;;
a) address=${OPTARG} ;;
east) email=${OPTARG} ;;
* ) repeat "Invalid option"
go out 1 ;;
esac
done
# Print the statement values
printf "Name:$name \due northAccost:$address \nEmail:$email \n"

Output:

The following output volition announced afterwards executing the above script.

Instance-5: Read file line by line

Create a fustigate file named while5.sh with the following code. Here, a filename will exist given in the first command-line argument at the execution fourth dimension. If the file exists, then the content of the file will be printed line by line using the loop; otherwise, an error message will exist printed.

#!/bin/fustigate

# Check the command-line argument value is given or not
if [ $# -gt 0 ]; then
# Assign the filename from comand-line argument value
filename=$1

# Read file line past line
while read line; practise
# Print each line
echo $line
done < $filename
else
# Print message if no argument is provided
echo "Argument value is missing."
fi

Output:

The following output will appear after executing the to a higher place script.

Case-half-dozen: Write content into a file

Create a bash file named while6.sh with the following lawmaking. Here, the filename will be taken from the user in which the text content will be written. The user has to type Ctrl+D subsequently typing the content of the file.

#! /bin/bash

echo -northward "Enter the filename to create: "
# Accept the filename that volition be created
read filename
# Read the content of the file from the terminal
while read line
practice
repeat $line >> $filename
done

Output:

The following output volition appear subsequently executing the to a higher place script.

Case-7: Creating an infinite loop

Sometimes, information technology is required to declare an infinite loop for various programming purposes. Create a bash file named while7.sh and test the lawmaking of the space loop. No termination condition is set for the loop in this example. This type of loop is called an infinite loop. Here, an go out statement is used to quit from the infinite loop. Then, this loop will exist iterated x times, and when the iteration value becomes equal to ten, the exit statement will execute for exiting from the space loop.

#!/bin/fustigate

# Initialize the counter
due north=1
# Declare an infinite loop
while :
practise
printf "The current value of north=$north \northward"
if [ $n == 3 ]
then
echo "good"
elif [ $n == 5 ]
then
echo "bad"
elif [ $n == 7 ]
so
echo "ugly"
elif [ $northward == 10 ]
then
leave 0
fi
# Increment the value of n by 1
( (n++) )

done
# Take the filename that volition be created
read filename
# Read the content of the file from the terminal
while read line
do
echo $line >> $filename
done

Output:

The following output volition appear afterward executing the in a higher place script.

Example-8: Using C-style while loop

Create a fustigate file named while8.sh with the post-obit code. Hither, the while loop has been declared in a c-way format that will iterate 5 times by incrementing the counter value by 10.

#!/bin/bash

# Initialize the counter
n=5
# Define the while in C-mode
while ( (due north <= l ) )
exercise
echo $due north
# Increment counter by 10
( ( north=n+10 ) )
done

Output:

The post-obit output will announced after executing the to a higher place script.

Conclusion:

Different uses of the while loop have been explained in this tutorial past using multiple examples. I hope the bash user will be able to utilise this loop properly in their script after practicing these examples.

Most the author

I am a trainer of spider web programming courses. I like to write article or tutorial on diverse Information technology topics. I have a YouTube aqueduct where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Assistance.

edelsteinthersibley.blogspot.com

Source: https://linuxhint.com/bash-while-loop-examples/

0 Response to "Bash Read Output of Command Into While Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel