Content-Length: 526382 | pFad | https://learning.oreilly.com/library/view/learning-the-bash/1565923472/ch05s02.html

for - Learning the bash Shell, Second Edition [Book]
Skip to Main Content
Learning the bash Shell, Second Edition
book

Learning the bash Shell, Second Edition

by Cameron Newham, Bill Rosenblatt
January 1998
Beginner content levelBeginner
336 pages
10h 6m
English
O'Reilly Media, Inc.
Content preview from Learning the bash Shell, Second Edition

for

The most obvious enhancement we could make to the previous script is the ability to report on multiple files instead of just one. Tests like -e and -d take only single arguments, so we need a way of calling the code once for each file given on the command line.

The way to do this—indeed, the way to do many things with bash—is with a looping construct. The simplest and most widely applicable of the shell’s looping constructs is the for loop. We’ll use for to enhance fileinfo soon.

The for loop allows you to repeat a section of code a fixed number of times. During each time through the code (known as an iteration), a special variable called a loop variable is set to a different value; this way each iteration can do something slightly different.

The for loop is somewhat, but not entirely, similar to its counterparts in conventional languages like C and Pascal. The chief difference is that the shell’s for loop doesn’t let you specify a number of times to iterate or a range of values over which to iterate; instead, it only lets you give a fixed list of values. In other words, you can’t do anything like this Pascal-type code, which executes statements 10 times:

for x := 1 to 10 do
begin
    statements...
            end

(You need the while construct, which we’ll see soon, to construct this type of loop. You also need the ability to do integer arithmetic, which we will see in Chapter 6.)

However, the for loop is ideal for working with arguments on the command line and with sets of files (e.g., all ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning the bash Shell, 3rd Edition

Learning the bash Shell, 3rd Edition

Cameron Newham
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 1565923472Supplemental ContentCatalog PageErrata








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://learning.oreilly.com/library/view/learning-the-bash/1565923472/ch05s02.html

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy