Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

LPI 010-160 Exam - Topic 5 Question 71 Discussion

Which of the following examples shows the general structure of a for loop in a shell script?
C) for file in *.txt do echo $i done
A) for *.txt as file => echo $file
B) for *.txt ( echo $i )
D) for ls *.txt exec {} \;
E) foreach @{file} { echo $i }

LPI 010-160 Exam - Topic 5 Question 71 Discussion

Actual exam question for LPI's 010-160 exam
Question #: 71
Topic #: 5
[All 010-160 Questions]

Which of the following examples shows the general structure of a for loop in a shell script?

Show Suggested Answer Hide Answer
Suggested Answer: C

The general structure of a for loop in a shell script is as follows12:

for variable in list do commands done

The variable is the name of a loop counter or iterator that takes on the values of the items in the list. The list can be a sequence of words, numbers, filenames, or the output of a command. The commands are the body of the loop that are executed for each value of the variable. The do and done keywords mark the beginning and the end of the loop body.

The option C. for file in *.txt do echo $i done follows this structure, with the variable being file, the list being *.txt (which matches all the files with the .txt extension in the current directory), and the command being echo $i (which prints the value of the variable i, which is presumably set somewhere else in the script).

The other options are incorrect because:

A . for *.txt as file => echo $file uses an invalid syntax for a for loop. The as keyword is not part of the shell script syntax, and the => symbol is not a valid operator. The correct way to write this loop would be:

for file in *.txt do echo $file done

B . for *.txt ( echo $i ) uses an invalid syntax for a for loop. The parentheses are not part of the shell script syntax, and the loop body is missing the do and done keywords. The correct way to write this loop would be:

for i in *.txt do echo $i done

D . for ls *.txt exec {} ; uses an invalid syntax for a for loop. The ls command is not a valid variable name, and the exec {} ; is not a valid command. This looks like a mix of a for loop and a find command. The correct way to write this loop would be:

for file in *.txt do exec $file done

E . foreach @{file} { echo $i } uses an invalid syntax for a for loop. The foreach keyword is not part of the shell script syntax, and the @{file} and { echo $i } are not valid expressions. This looks like a mix of a for loop and a Perl syntax. The correct way to write this loop would be:

for file in * do echo $file done


Looping Statements | Shell Script - GeeksforGeeks

How do I write a 'for' loop in Bash? - Stack Overflow

Contribute your Thoughts:

0/2000 characters
Cassie
3 days ago
I’m surprised E is even an option, it looks like a different language!
upvoted 0 times
...
Rebecka
8 days ago
Wait, is D even a for loop? Seems off.
upvoted 0 times
...
Elly
14 days ago
Definitely not B, that’s just wrong.
upvoted 0 times
...
Dortha
19 days ago
I thought it was A at first, but C makes more sense.
upvoted 0 times
...
Beckie
24 days ago
Option C is the correct syntax for a for loop.
upvoted 0 times
...
Buddy
29 days ago
Option A seems off to me; I don't think you can use "as" in a for loop in shell scripting.
upvoted 0 times
...
Kaycee
1 month ago
I feel like option C looks the closest to what we practiced, but I’m not entirely sure about the variable name used.
upvoted 0 times
...
Louvenia
1 month ago
I remember that a for loop usually has a specific structure, but I can't recall if "do" and "done" are required in shell scripts.
upvoted 0 times
...
Lea
1 month ago
I think I've seen something like this in practice questions, but I'm not sure if the syntax is exactly right.
upvoted 0 times
...

Save Cancel