Reading a list of data into a function using a script.

Home -> UNIX -> Shells -> ksh

9357 views

From the computer of: seamonkey420 (130 recipes)
Created: Jul 24, 2005


Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

Well, i've been a ksh geek for only prob 2 years now, but my job requires the managing of HUGE amounts of files and data (usually about 6500 files/items per query,set). Managing and querying such large amounts of data by hand is impossible and just a waste of time when you can have a ksh script do this for you! This recipe is just an example of what you can do with some variables, menus, and functions!

Overview: This recipe is more of an appetizer to the meal. I will give a few example scripts and explain how each works and from there one can hopefully use the fundamental concepts in each and apply it to one's own area/field/use. However, there is more going on in each of my examples than just the script. One has to also be sure that the way they are setting up the data to be queried is going to work.. (ie lets say we have 100 customers who are identified in your system by a 5 digit customer number; be sure to keep the data structure for them consistent so when you go back to write a script to query data it makes life much easier!). Functions are not executed until called within the script, they just hang out until they are needed.. ;)


Creating a script that uses a function to call in a list of data.

script example:
how to use: copy the text in the quote below to a file named: script.ksh
then create list.txt file in the same folder with these numbers on a line:
1
2
3
then also create 3 text files with the same name as number in the the list, ie 1.txt, 2.txt, 3.txt and put some different data in each.

Quote:

# basic script that uses a function to call in a list of data to then query
# function_test.ksh


# creates function called readDATA
function readDATA
{
echo $data'.txt'
sleep 1
}

#main script that will call function
echo "Test Script: Read Data from List, test.txt"

while read -r data
do
readDATA
done < list.txt



so what does this script do?

Here's whats its doing...

the first thing we did was create the function. A function is basically just a mini-script within a script that you can call at any time by calling it, very handy! The function we setup is just going to echo the contents of the file $data.txt to the screen.

The next thing we setup was the while statement to execute the script.
This will read the list file, list.txt, and then for each line in the list.txt it wil then temp assign it to be the $data variable and send that variable through the function. (phew, that was a bit confusing eh? hehe)

So lets see it in action...

while -r read (using our list from above of 1, 2 , 3 on sep lines)
the first time through it would see the read = 1
it then passes the 1 value to the readDATA function..

it then translates the $data variable to 1 and will echo 1.txt to the screen.

The script will keep running through the file until it reaches the end of the file ( done < list.txt defines this!)

This is a super basic script and usuage of reading from a list. I have a script i use at work that allows me to define the list files and also have more than one variable per line and so on., but understanding how to read the data in from a list and how functions work was a great way for me to really start to understand the power of ksh.

peace
seamonkey420

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  Creating a kornshell script with text-based menus in VI
  Set an environment variable in Korn shell (ksh)
  Checking if a variable is a number in ksh
  Some Common AIX/Unix Commands
  appending a list of files to one file using xargs command
  Enable vi ksh command line editing

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.