Linux FAQ's & Manuals
- Linux Scripts
- Debian Install
- Bash For Beginners
- Bugzilla
- Consultants Guide
- GCC Manual
- Linux Command Line Tools
- Gnu Pascal Coding Standards
- Linux Installation Disk
- Labolatorium Linux(PL)
- Budowa systemu Linux(PL)
- Linux Dictionary
- Network Administrators
- Rescue Disk for Linux
- Red Hat Installation
- Red Hat Customization
- Red Hat Getting Started
- Red Hat Security
- Secure & Optimize
- Slackware Manual
- Suse Support
- Suse FAQ
Datamation: "Here's a list of 49 open source projects that deserve a round of applause..."
Google and Sun May Butt Heads Over Android
Computerworld: "Google Inc. could be heading for a showdown with Sun Microsystems Inc. over the way Android, Google's new mobile phone software platform, handles Java..."
Shuttle Lands Linux SFF PCs in the UK
Register Hardware: "Shuttle has begun offering British buyers a range of its XPC machines with Linux pre-installed, the small form-factor PC specialist said today..."
Funambol To Sync With Google's Android
InformationWeek: "Mobile 2.0 messaging software provider Funambol this week announced plans to start an open source project that would result in a messaging and sync client for Google's Android mobile-phone software development platform..."
Where's The First Firefox 3 Beta?
Wired: "Firefox 3 was originally slated to arrive last summer. Obviously that didn't happen, but where is the first beta, which even on revised roadmaps was said to arrive in October...?"
9.1. the for loop
9.1.1. how does it work?
the for loop is the first of the three shell looping constructs. this loop allows for specification of a list of values. a list of commands is executed for each value in the list.
the syntax for this loop is:
for name [in list ]; do commands; done
if [in list] is not present, it is replaced with in $@ and for executes the commands once for each positional parameter that is set (see section 3.2.5 and section 7.2.1.2).
the return status is the exit status of the last command that executes. if no commands are executed because list does not expand to any items, the return status is zero.
name can be any variable name, although i is used very often. list can be any list of words, strings or numbers, which can be literal or generated by any command. the commands to execute can also be any operating system commands, script, program or shell statement. the first time through the loop, name is set to the first item in list. the second time, its value is set to the second item in the list, and so on. the loop terminates when name has taken on each of the values from list and no items are left in list.
9.1.2. examples
9.1.2.1. using command substitution for specifying list items
the first is a command line example, demonstrating the use of a for loop that makes a backup copy of each .xml file. after issuing the command, it is safe to start working on your sources:
[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 |
this one lists the files in /sbin that are just plain text files, and possibly scripts:
for i in `ls /sbin`; do file /sbin/$i | grep ascii; done |
9.1.2.2. using the content of a variable to specify list items
the following is a specific application script for converting html files, compliant with a certain scheme, to php files. the conversion is done by taking out the first 25 and the last 21 lines, replacing these with two php tags that provide header and footer lines:
[carol@octarine ~/html] cat html2php.sh #!/bin/bash # 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 |
since we don't do a line count here, there is no way of knowing the line number from which to start deleting lines until reaching the end. the problem is solved using tac, which reverses the lines in a file.