reading configuration files in bash. Best way?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading configuration files in bash. Best way?
# 1  
Old 05-12-2010
reading configuration files in bash. Best way?

Context:
I have a random pin number generator script that reads a tab-delimited file containing a location and a count:

eg.,
Code:
mansfield     30
tokyo          15
smithville     34

It produces random PIN# in the amount specified by the number in the second column.

Currently, I read the file and execute the generator function as follows:
Code:
while read line
do
    BRANCH=$(awk -F"\t" '{print $1}' <<< "$line")
    CNT=$(awk -F"\t" '{print $2}' <<< "$line")
    rand_pass "$BRANCH" $CNT
done < $file

I could also do something like this:
Code:
declare -a Array3
saveIFS="$IFS"
IFS=$'\n'
Array3=($(<$LIST))
IFS="$saveIFS"

Then:
Code:
for i in ${!Array3[@]}
do
    A=${Array3[i]//[[:digit:][:space:]]/}
    B=${Array3[i]//[[:alpha:][:space:]]/}
    rand_pass "$A" $B
done

Or ...I could source the file ....but ....

The script won't know the names or amount of parameters in the file.
The file will have stuff added to it over time.
I could chuck it into the script:
Code:
$ script.sh < file.txt

Then deal with the passed variables that way ...but why?
Is there a point or advantage to sourcing the file in this case?
Have I already arrived at the ideal ( or one of several ) solution?

Thanks for reading!

Bubnoff
# 2  
Old 05-12-2010
Code:
while IFS=\<tab> read -r b c; do
    rand_pass "$b" "$c"
done < "$file"

Replace <tab> with an actual tab character, if, as your awk implies, the file's records contain tab-delimited fields.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 05-12-2010
Like this?

Code:
while IFS=$'\t' read -r b c

Is there a better way to indicate the value of IFS? I had
issues simply going:

Code:
IFS=\t

Thanks!

Bub

PS: It's working ...just wondering about the quoting.

Last edited by Bubnoff; 05-12-2010 at 09:27 PM.. Reason: Clarification.
# 4  
Old 05-12-2010
$'\t' (which is equivalent to a single quoted tab) works fine, if your shell supports it. If not, a backslash followed by a literal tab character would also work. It's important that the tab be quoted (as in your dollar sign syntax, or with a backslash, or quotes), otherwise, assuming a default value of IFS (space, tab, newline), the shell would consume the tab during the field splitting and IFS would be set to a null string.

Typing a literal tab on the command line can be done by typing control-v followed by control-i, or control-v followed by the tab key. In a text editor, you may be able to simply type tab, depending on how it's configured. A simple \t would not work. To the shell, that's equivalent to a plain t.

Regards and thank you very much for the bits,
Alister
# 5  
Old 05-12-2010
Awesome, thanks!
# 6  
Old 05-13-2010
Quote:
Originally Posted by alister
Code:
while IFS=\<tab> read -r b c; do
    rand_pass "$b" "$c"
done < "$file"

(...)
Are you sure you need to change the IFS?
Doesn't this work?
Code:
while read B C
do   rand_pass "$B" "$C"
done < "$file"

# 7  
Old 05-13-2010
Using the default value of IFS (space, tab, newline) would fail if the branch name consists of more than one space-separated word. The first word would be assigned to b, and the rest of the city words plus the count would be assigned to c.

The original code uses a tab delimiter, so mine does as well.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compilation error when I run Bash configuration command

Hi, I downloaded source code file from The GNU website and changed the source code of ls.c file, added printf command to it. It worked fine. Then, I deleted the printf command, saved the file and ran the command 'make sudo && make install' closed the terminal and printf statement went away. I... (1 Reply)
Discussion started by: akanksha1509
1 Replies

2. Shell Programming and Scripting

Reading from file bash command

Hello, I have a file in the following format id sample platform R1 R2 gene1 gene2 gene3 1 abc llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp asp 2 def llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp 3 ghi llumina ... (3 Replies)
Discussion started by: nans
3 Replies

3. Shell Programming and Scripting

Reading a text file using bash

I've a file in linux with following text: ;ip address hostname put-location alt-put-location tftpserver 192.168.1.1 r01-lab1-net /mnt/nas1/fgbu/ /opt/fgbu/devicebackup 192.168.1.254Now I want to read these values and assign them to particular variables... (6 Replies)
Discussion started by: kashif.live
6 Replies

4. Programming

Reading a router configuration file

Hello C specialists, I'm trying to write a program to read out a binary configuration file produced by a router. But the output of Name and Value is cryptic. What's going wrong? The structure of the binary file is very simple: struct nvram_tuple { char *name; char *value; ... (5 Replies)
Discussion started by: digidax
5 Replies

5. UNIX for Dummies Questions & Answers

[bash] Reading two files and change a specific field

Hi all. I have 2 files like these: file1 3 -2 5 4 . . . 3 3 3 4 . . . 2 2 3 4 . . . 3 -2 8 4 . . . file2 0.4242 2 3 4 . . . 2.562 7 3 4 . . . 0.7242 5 5 4 . ... (3 Replies)
Discussion started by: t_studen_t
3 Replies

6. Shell Programming and Scripting

Help in reading a cv file in bash

Hi All, I am trying to read a .csv file which has some 6 columns. Eg: samp.csv one, two, three, four six, seven, eight, nine I used the following code, for line in `cat samp.csv` do echo "$line" done It displays every comma seperated values in each line like, one,... (1 Reply)
Discussion started by: johnwilliams.sp
1 Replies

7. Shell Programming and Scripting

Problem in reading file (bash)

i get a name from user first name : last name, in this format. Now i am saving this to a file. what i want is, I do not want to save any name if I already have one entry o that same name..what should i do for example user give robert fernandez this will save in file as robert:fernandez. if... (5 Replies)
Discussion started by: Learnerabc
5 Replies

8. Shell Programming and Scripting

bash reading and assigning

Hi, In a script i am having trouble joining a variable to a file. for, example I read input from user as a variable a or b or c or d etc and want to join those to different files... or if user press a then it will open somefile.txt if user press b then it will open otherfile.txt any idea (4 Replies)
Discussion started by: Learnerabc
4 Replies

9. Shell Programming and Scripting

Reading a value from the configuration file

Hi, I have prepared a config file in which I am declaring the value for a country such as: COUNTRY=USA Now I am trying to read the country from the config file and print a message based on the same. I have written the following code in a script and when executing the script I getting an error. ... (14 Replies)
Discussion started by: yoursdavinder
14 Replies

10. Shell Programming and Scripting

bash: reading filenames from file

Hi, I'm trying to write a script that reads filenames from a file and use these filenames in a loop. The filenames are all on one line and the problem is that these filenames have wildcards like * and braces like in them. Right now what I'm doing is something like this: echo "reading from... (0 Replies)
Discussion started by: warp17
0 Replies
Login or Register to Ask a Question