Solved

unix ksh: script to read input from file

Posted on 2010-11-10
7
1,321 Views
Last Modified: 2013-12-26
Dear Experts,

Say I have a script (process.ksh) like this:

echo $1 >> test.txt
echo $2 >> test.txt
echo $3 >> test.txt

Those varialbes are stored in another file (data.txt) like this:
1=workstation
2=stream
3=job

What command do I need for process.ksh to read data.txt and execute the cmd's
I tried
`cat data.txt ` | process.ksh
process.ksh  `cat data.txt`
but all it produces is test.txt with empty lines

Thanks in advance.
0
Comment
Question by:Watnog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34100638
Hi,
process.ksh ´cat data.txt`
should work!
The shell will expand this statement to
process.ksh 1=workstation 2=stream 3=job
so that process.ksh will recognize "1=workstation" as $1 , "2=stream" as $2 etc. (positional parameters).
Did you actually use backticks`I think you should try

process.ksh $(cat data.txt)
It's better readable and there is no risk of confusing backticks with apostrophes.
wmp

 
 
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34100651
Er,
after copying and pasting I saw that the first metacharacter is not a backtick but a sharp accent ( ´ ) which is wrong. You need two backticks!
So I'd really suggest using the $( ... ) notation!
0
 

Author Comment

by:Watnog
ID: 34100714
Same result: empty lines in test.txt....


# cat data.txt
wks=HT0016P
stream=JOBS
job=OTS-EXIT1-CMD

# cat process.ksh
echo $wks >> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt
echo $stream >> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt
echo $job >> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt

# cat test.txt



#

When -x this is returned...
# ksh -x process.ksh $(cat data.txt)
+ echo
+ 1>> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt
+ echo
+ 1>> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt
+ echo
+ 1>> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34100779
OK,
that's quite a different thing!
In your Q you wrote "echo $1" ..., now you're writing "echo $wks" ... !
What dou you want to appear in test.txt? Only the strings "HT0016P" etc. or also the "wks=" etc. stuff?
In the latter case you could actually use $1, $2, $3 ... in process.ksh.
For the second case - do you actually need the variables $wks etc for a later use?
If not, you could simply do in process.ksh
while read line ; do echo ${line#*=} >> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt ; done
and run process.ksh like this
process.ksh < data.txt
wmp

 
 
 
 

 
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 34100869
Should you need the named variables wks, stream etc. later in your scritpt, you could do this in process.ksh
eval $(cat $1)
echo $wks >> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt
echo $stream >> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt
echo $job >> /opt/maestro/OTS/SCRIPTS/TEST/AR/JOBS/test.txt
and run process.ksh like this
process.ksh data.txt
Instead of eval you could use "export" to have the variables available in scripts called by process.ksh too.
 

 
0
 

Author Comment

by:Watnog
ID: 34100872
I thought that the name of the variable didn't matter, so that a $1 would be equivalent to $wks ...
The whole idea here is that there is a process that can produce a data.txt file, and is able to start a script. That script executes cmds using the values in data.txt in order to produce yet other files to finally xmail the result..
So to re-use the script, that would have just the variables in it, but the input file (data.txt) would be different each time and hold the values.
So process.ksh would use cmds in which HT0016p and JOBS and OTS-EXIT1-CMD are used.
Hope this clarifies.
W.
0
 

Author Closing Comment

by:Watnog
ID: 34100887
That does it wnp, many thanks!
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question