Loop examples Flashcards
For loop example:
Using command substitution for specifying LIST items
[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
For loop example:
Using the content of a variable to specify LIST items
!/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
While loop example:
Simple example
!/bin/bash
This script opens 4 terminal windows.
i=”0”
while [$i -lt 4]
do
xterm &
i=$[$i+1]
done