Loop examples Flashcards

1
Q

For loop example:

Using command substitution for specifying LIST items

A

[carol@octarine ~/articles] ls *.xml

file1.xml file2.xml file3.xml

[carol@octarine ~/articles] ls *.xml > list [carol@octarine ~/articles] for i in cat list; do cp “$i” “$i”.bak ; done

[carol@octarine ~/articles] ls *.xml* file1.xml file1.xml.bak file2.xml file2.xml.bak file3.xml file3.xml.bak

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

For loop example:

Using the content of a variable to specify LIST items

A

!/bin/bash

[carol@octarine ~/html] cat html2php.sh

specific conversion script for my html files to php LIST=”$(ls *.html)”

for i in “$LIST”; do

NEWNAME=$(ls “$i” | sed -e ‘s/html/php/’)

cat beginfile > “$NEWNAME”

cat “$i” | sed -e ‘1,25d’ | tac | sed -e ‘1,21d’| tac >> “$NEWNAME”

cat endfile >> “$NEWNAME”

done

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

While loop example:

Simple example

A

!/bin/bash

This script opens 4 terminal windows.

i=”0”

while [$i -lt 4]

do

xterm &

i=$[$i+1]

done

How well did you know this?
1
Not at all
2
3
4
5
Perfectly