passing in arguments into a file using a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing in arguments into a file using a loop
# 1  
Old 04-11-2008
passing in arguments into a file using a loop

Hi, so Im a bit new to shell scripting and want to do the following but not sure how. Basically I have a file named "output" which contains misc text but inside the file I want to set up variables like $1 or some symbol. Anyways, in another file called "list" I have a list of items that I want to substitute for those variables in "output" The "list" file will have each element on each line. Anyways I figured Id use a for loop to do this but I'm not sure how to prepare my "output" file to take in variables and the number of elements in the "list" file will vary so I can exactly hard code the number. Anyone have any ideas how to accomplish this? Thanks!
# 2  
Old 04-11-2008
IT's not clear exactly what you are trying to do, but to loop through a file, you use:

Code:
while IFS= read -r line
do
   : do something with "$line" here
done < FILENAME

Or use awk.
# 3  
Old 04-13-2008
hey, thanks for the response and sorry for the confusion. But basically I'll give an example of what I wanted to do. I have 2 files that look like...

File 1:
Item1
Item2
Item3

File 2:
<a href= ...>
$1
<more html stuff>
$2

So basically the $1 and $2 I want them to be replaced by Item1 and Item2 and so on. Not sure if this is possible or not. I'm not exactly sure how to pass in arguments like that into a file but Im starting to understand the while loop in shell now
# 4  
Old 04-13-2008
It would be a lot more efficient to run a single sed script over the whole file.

You can create a sed script from your file 1 -- curiously, also using sed. Feel your mind wrinkle a bit if you haven't done this before.

Code:
nl file1 |
sed 's%^[ 	 ]*\([1-9][0-9]*\)[ 	 ]*\([^ 	].*\)%s/\\$\1/\2/g%'

This adds line numbers (with nl) then uses sed to break it up. We want the line number in \1 and the original input line in \2, trimming the whitespace in between. So we are looking for beginning of line ^ followed by optional whitespace (character class with space and tab in it, zero or more occurrences) followed by a non-zero number and any number of trailing numbers, which we grab into \1; followed by more whitespace, and finally grab the rest starting from the first non-whitespace character until the end of line into \2.

To type [(space)(tab)] or [^(space)(tab)] use an editor, or, at the command line, you may need to prefix the tab with a ctrl-v in order to actually type it (because in many shells, the tab key is normally used for file name expansion).

The output is a sed script which you can save to a file, or pipe to another sed instance:

Code:
s/\$1/Item1/g
s/\$2/Item2/g
s/\$3/Item3/g

To pull it all together:

Code:
nl file1 |
sed 's%^[ 	 ]*\([1-9][0-9]*\)[ 	 ]*\([^ 	].*\)%s/\\$\1/\2/g%' | 
sed -f - file2

# 5  
Old 04-14-2008
Thanks for the reply but it doesn't look like sed would work. The lines won't correspond like 1 to 2 and 2 to file from file1 to file2 respectively. Basically file 2 is an html page variables located throughout and I want to just pass in values to those variables and output that final html page.
# 6  
Old 04-14-2008
Did you try? From your description, I think it still sounds like it might work, but if not, there's probably something more to learn about your specific problem. If your variable names are not $1 $2 etc then you will need to clarify what they are.

Here's a quick experiment / demonstration. file1 contains variable values, line number indicates which is which; file2 contains HTML with $1 $2 etc.

Because the stuff in file1 contains slashes, I swapped the % and / separators in the sed script.

Code:
vnix$ cat >file1
<a href="http://unix.com/">
<blink>horrible</blink>
<p>The whole paragraph is here.</p>
</a> <!-- oops, forgot the close tag for $1 (-: -->
^D
vnix$ cat >file2
<html><head><title>welcome to $2</title></head>
<body>
<p>Please visit $1the best web site evah$4
$3
</body></html>
^D
vnix$ nl file1 |
> sed 's/^[ 	 ]*\([1-9][0-9]*\)[ 	 ]*\([^ 	].*\)/s%\\$\1%\2%g/' |
> sed -f - file2
<html><head><title>welcome to <blink>horrible</blink></title></head>
<body>
<p>Please visit <a href="http://unix.com/">the best web site evah</a> <!-- oops, forgot the close tag for $1 (-: -->
<p>The whole paragraph is here.</p>
</body></html>

# 7  
Old 04-14-2008
Hi, yea I gave it a try and it gave me some error with sed. Anyways, I guess the easiest way to describe this is give an example and the output I'm trying to create. The file1 doesn't have any variables in it. This is something I'm looking to achieve:

File1:
abc
blah
test
moretest

File2:
<html><head><title>welcome to $1</title></head>
<body>
<p>Please visit $2the best web site evah$4
$3
</body></html>

Output:
<html><head><title>welcome to abc</title></head>
<body>
<p>Please visit blahthe best web site evahmoretest
test
</body></html>

So basically stuff from file1 replaces those $# in file2. When I first ran the sed command, I ran it in a script.sh which I'm not sure makes a difference or not. Let me know what you think, thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing Arguments to shell script from file is not working as expected.

Hi All, I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination. # file_copy.sh source_dir = ${source_dir} target = ${target_dir} cp source_dir target and my parameter file is like below #parameter_file.txt source_dir =... (4 Replies)
Discussion started by: Narasimhasss
4 Replies

2. UNIX for Dummies Questions & Answers

passing command arguments from a file?

Hi Im trying to run zip shell command from an Oracle job, but this has limitations. This should take a few of explanaition,.. Oracle allows me to execute a command and then I can set up a fixed number of arguments. Ex: (summarizing in something like..): JOB DEFINITION job_name: test... (4 Replies)
Discussion started by: vegatripy
4 Replies

3. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

4. Shell Programming and Scripting

Passing arguments at runtime

Hi .. Can any one please tell how to pass argument to shell script at runtime? I want to implement funcnality just like bc, where we can provide input while script is running and can be used later in the same script. Thanks in advance... (1 Reply)
Discussion started by: kunjalhg
1 Replies

5. UNIX for Dummies Questions & Answers

Passing arguments

I need to pass arguments to a shell script.My batch is calling some java program. ################# x=$1 y=$2 java -classpath program ################### if first parameter and second parameter is null then java -classpath program if first parameter is not null and second parameter is... (3 Replies)
Discussion started by: mnjx
3 Replies

6. Shell Programming and Scripting

passing arguments

Hi I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like ./abc def /file <directory location> In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Discussion started by: zmfcat1
4 Replies

7. Shell Programming and Scripting

Passing Arguments-Help

Hi, I have a script which adds the user credentials to an ldap server. Im passing the variables as below.. /path/my_script $uname $pwd $environ ${deposit} If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Discussion started by: Tuxidow
5 Replies

8. Solaris

Passing arguments to a shell script from file while scheduling in cron

Hi, I have a shell script Scp_1.sh for which I have to pass 2 arguments to run. I have another script Scp_2.sh which in turns calls script Scp_1.sh inside. How do I make Scp_1.sh script to read arguments automatically from a file, while running Scp_2.sh? -- Weblogic Support (4 Replies)
Discussion started by: weblogicsupport
4 Replies

9. UNIX for Dummies Questions & Answers

passing strings as arguments

Is it possible to pass a string as an argument from the command line? I know I can pass a word in but can I put a line of text in with spaces and fullstops or do I just put it in brackets or quotes so the compiler can differinate between the first argument and the second. (1 Reply)
Discussion started by: iago
1 Replies

10. UNIX for Dummies Questions & Answers

passing arguments

I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts: #!/bin/ksh read fname #if (( $# > 0 )); then $fname | ls -l #fi this produces a long listing of all the files in my current... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question