Question
How to use a while loop in BASH to iterate over all items in a file?
Answer
A BASH loop can greatly increase an administrator's efficiency when handling repetitive tasks. This article provides a basic guide for using a while loop to iterate over all items in a file.
Note: Please Note that cPanel support cannot assist with creating or modifying scripts or using scripting languages, such as BASH.
while loops have four main parts when iterating over the contents of a file:
- Generating input from the file.
- Reading the input (while).
- Performing an operation with the input (do).
- Closing the loop (done).
This guide will use the following command, which reads the domains from the /etc/userdomains file, then checks the HTTP status code for each domain, to explain the structure of a while loop.
# grep -v nobody /etc/userdomains | cut -f1 -d ':' | while read url ; do echo $url ; curl -IsS $url | grep HTTP ; done
From this command, we can see that the four parts are:
-
grep -v nobody /etc/userdomains | cut -f1 -d ':', which generates the input that thewhileloop will read by finding the entries for non-nobody users, then selecting only the domain names from those entries. -
while read url, which reads the domain found for each entry and stores the domain in theurlvariable. -
do echo $url ; curl -IsS $url | grep HTTP, which performs acurlon the domain in theurlvariable and outputs the domain name and the HTTP status code or error messagecurlreceived. -
done, which ends the loop.CONFIG_TEXT: [root@server ~]cPs# grep -v nobody /etc/userdomains | cut -f1 -d ':' | while read url ; do echo $url ; curl -IsS $url | grep HTTP ; done
domain.tld
HTTP/1.1 200 OK
domain2.tld
curl: (6) Could not resolve host: domain2.tld
domain3.tld
HTTP/1.1 500 Internal Server Error
sub.domain3.tld
HTTP/1.1 405 Method Not Allowed
domain4.tld
HTTP/1.1 403 Forbidden
domain5.tld
HTTP/1.1 301 Moved Permanently
[root@server ~]cPs#
Comments
0 comments
Article is closed for comments.