Read from file as script parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from file as script parameter
# 1  
Old 06-25-2005
Read from file as script parameter

I have a file with userIDs, one per line no spaces. I have a script that accepts userIDs as a parameter and outputs information about them. What I have had to do in the past was to modify each line of the file with userIDs to call the script with the userID and pipe the output to a data file.

/path/to/script aadams >> data.dat
/path/to/script bbaker >> data.dat
...
/path/to/script zzink >> data.dat

There has to be an easier more efficient way to read lines from a file and do something with it and capture the results.

I am just starting out in UNIX, Solaris, and have heard of Sed & Awk - but have never used them, I am not sure of the best way to proceed or if they are what I need to use.
# 2  
Old 06-25-2005
So you want to read a file line by line and do something with each line?

The easiest and fastest way is...
Code:
while read line; do
  # do something with $line
done < myfile.txt

So for your requirements, I assume something like
Code:
while read username; do
   /path/to/script ${username} >> data.dat
done < list_of_usernames_one_per_line.txt

is what you want.

If you're are executing lots of (different) commands and want the output all to go to the same file, something like
Code:
{  
  command1
  command2
  command3
} >> foo.txt

is a good way to group the commands, and not have trillions of >> redirects.

Cheers
ZB
# 3  
Old 06-25-2005
MySQL

Excellent - that is exactly what I was looking for Smilie . My main issue was how to identify the input file. '< infile' solved that problem.
Three lines of code - thats smooth compared to what I had to do before!

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How pass the input parameter to a file in the script ?

OS version: RHEL 6.7 myTextFile.txt file is referred within Script1.sh script, I only execute Script1.sh and I want the input variable to be passed inside myTextFile.txt . Any idea how I can do this ? $ cat script1.sh cat myTextFile.txt $ cat myTextFile.txt $1 Requirement1.... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

Read parameter file for a shell script

Hi All, I need urgent Help from all of you here. Below is my code- ================================================== #!/usr/bin/sh cd $1 cat $2 | tr -ds "$" "" > parameter_file.param export `parameter_file.param` chmod 777 parameter_file.param echo $1 echo $2 cd $prmDirInput... (5 Replies)
Discussion started by: Amit786
5 Replies

3. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

4. Shell Programming and Scripting

How to read a directory as parameter in shell script?

Hi All, I have written a shell script to compile an oracle R12 form and also a procedure is called from it. The procedure registers the form, function and creates a menu entry. I took directory as a parameter. Code is as below: #!/bin/ksh echo... (3 Replies)
Discussion started by: veena484
3 Replies

5. Shell Programming and Scripting

unable to read a parameter

All, on command prompt when i am testing I am able to get the value of 'msgtime' parameter but in the script I get a space value for it . Please help .. following is the code snippet - grep "Component Manager stopped" msgtime.txt | while read line ... (16 Replies)
Discussion started by: dhirajdsharma
16 Replies

6. Shell Programming and Scripting

Read parameter file in a shell script to unload a DB2 Table???

Hi , I Have following requirement: DB2 Sql query to pass from a parameter file for example, I would create a parameter file with (SELECT column 1, column 2 FROM Table name) then job would read it and create a file with the contents named table.txt How to write/modify below ksh script to... (10 Replies)
Discussion started by: developer.dwh9
10 Replies

7. Shell Programming and Scripting

creating a parameter file in the script

Here is my code which i am using wright now: CODE #!/bin/bash awk ' BEGIN { FS="|"; while ( getline <"file1.txt" > 0) a=$2 } { if($1 in a) printf("%s %s Specialty Mapped\n", $1, a) > "mapped.txt" else printf("%s %s Specialty Not mapped\n", $1, $2) > "notmapped.txt" ... (2 Replies)
Discussion started by: dsh007
2 Replies

8. Shell Programming and Scripting

awk/sed script to read values from parameter files

Hi, I am writing a shell program that executes a lot of Oracle SQL Files on different databases based on the enviroment setting value. I am trying to design a parameter file where i can store the environment values for all the databases in the below format Environment File File Name... (6 Replies)
Discussion started by: rajan_san
6 Replies

9. Shell Programming and Scripting

Passing parameter from one file to shell script

Hi All, I have a 2 files. File1 i am generating using an ETL tool, which is a comman seperated delimited file which contains country code & load date. everytime, this country code will be updated from a table. It might be AB or BA & ld_date will be for which date we need to load the file. ... (7 Replies)
Discussion started by: Amit.Sagpariya
7 Replies

10. Shell Programming and Scripting

parameter file for a shell script

Hi I was able to pass parameters to a shell script from the command line but now, I am trying to make the shell script to get those parameters/values from a file. Please give me ideas how to do this or if you have an example or website that shows how to do this. I tried searches but it... (2 Replies)
Discussion started by: bryan
2 Replies
Login or Register to Ask a Question