LiveFire Labs - Online UNIX Training - Company Logo

Online UNIX Training with Hands-on Internet Lab

"Eliminate the expense and inconvenience of classroom training without eliminating the classroom experience."


Internet Lab
Console Access
Sample Course

Student Login

LiveFire Labs' UNIX Tip, Trick, or Shell Script of the Week - View

Questions? Call
1.888.843.1637 or send us email

August 4, 2003 - Using set to Process Unknown/Varying Numbers of Fields

It's relatively simple to write a shell script that will handle records or variables when the number of fields is known or fixed, but how would you write code to process records and variables when this number is unknown or varies from record to record?  This week's tip will provide you with one option for accomplishing this task.

Consider the following excerpt of shell script code:

<previous code>


while [ $1 ]
     <field processing code>

<subsequent code>
The first line of actual code ("set $RECORD") will assign each field of the record stored in the variable RECORD to a positional parameter (e.g. $1, $2, $3, etc.).  These fields are typically delimited with a blank space, but not always.

Next, the while statement checks the value of $1 to decide whether or not the commands within the loop should be executed.  If the value of $1 is null, processing will continue at the statement immediately following the loop's closing statement ("done").

Located within the while loop are field processing statements, and the shift command.  shift will move (or "shift") the values in the positional parameters to the left, or down, one position.  This means that the value in $1 will be discarded, the value in $2 will be assigned to $1, the value to $3 will be assigned to $2, and so on.

After shift is executed, the value in $1 is checked again for a null value.  The loop will continue until all of the record's fields have been processed.  Since the code is only concerned about the value stored in $1, the number of total fields in each record is really irrelevant.

If multiple records are being processed (e.g. read individually from an external file), it's safe to assume that this snippet of code will be enclosed within another loop that stores each record in the RECORD variable.

Learn more...

If you are new to the UNIX or Linux operating system and would like to learn more, you may want to consider registering for LiveFire Labs' UNIX and Linux Operating System Fundamentals online training course.

If you already have a solid grasp of the fundamentals but would like to learn more about the Korn shell and basic and advanced shell scripting, taking our Korn Shell Scripting course will be beneficial to you.

Our innovative hands-on training model allows you to learn UNIX by completing hands-on exercises on real servers in our Internet Lab.

More Tips...

Popular UNIX Tips from the Past

spacer Box Border

Receive the UNIX Tip, Trick, or Shell Script of the Week by Email

First Name:

Email Address:


Home - Contact us - Company info - Privacy Statement   

©2002-2003 LiveFire Labs.  All rights reserved.
Linux® is a registered trademark of Linus Torvalds, author and developer of this public domain operating system.
UNIX® is a registered trademark of The Open Group in the United States and other countries.