The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing variables to sed function in a script .... shweta_d Shell Programming and Scripting 2 06-05-2007 07:13 AM
passing two variables into a shell script? Bashar Shell Programming and Scripting 2 05-15-2007 11:00 AM
passing variables to awk from ksh script rein Shell Programming and Scripting 3 08-11-2005 12:29 PM
Passing filenames/variables into a script MrToast Shell Programming and Scripting 5 03-02-2004 10:15 AM
Passing variables from one script to another tcoventr UNIX for Dummies Questions & Answers 2 06-18-2002 11:57 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 02-14-2008
screwed718 screwed718 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 2
Passing data from file to variables for script

Hello all! After searching through numerous helpful posts on these forums I am still having an issue with a task I am trying to accomplish.

I am trying to take data from an input file, store the contents as variables, and use the variables in the script.

The input file (input.txt) is composed of 3 values: name, number, state. For example:

Quote:
John Doe, 555-123-4567, Texas
Jane Doe, 555-321-7654, Oklahoma
Jim Doe, 555-000-1111, California
Jeane Doe, 555-111-0000, New York
The main script requires the data to be in the same order but without the commas, i.e:

Quote:
name number state
After researching these forums I have come up with the following script that takes the data and puts it in an array:

Quote:
awk -F "," '{
array[$0] = array[$0] " " $1 " " $2 " " $3;
}
END {
for (i in array)
print array[i];
}' input.txt
Verifying with the print command, the array is properly formated how the main script needs it. Now the issue I am having is using the contents of the array in the variables for my main script. The script creates a single file using variables I want filled from the array.

Here is an abridged version of the main script using manually entered data in the variables:

Quote:
#!/bin/bash

cust=("John Doe");
tele=("555-123-1456");
state=("Texas");

echo "Please enter batch name: "
read batchname
echo "Creating "$batchname".txt..."

counter=${#cust[@]}
index=0

while [ "$index" -lt "$counter" ]
do

echo -e "\nCustomer Name:\t\t"${cust[$index]}"\n\tPhone:\t\t\t"${tele[$index]}"\n\tState:\t\t\t"${state[$index]}"\n" >> $batchname".cfg"

let "index = $index +1"
done

exit 0
I am trying to get the data from the array (taken from input.txt) to be used in the 3 variables in the main script (cust, tele, state), but with no luck.

The output file will be formatted as such:

Quote:
batchname.txt

Name: John Doe
Phone: 555-123-1234
State: Texas

Name: Jane Doe
Phone: 555-321-4321
State: Oklahoma

etc, etc...
Please let me know if you have any questions or need clarification on the issue I am having.

Thanks in advance!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0