Solved

unix ksh: script to read input from file

Posted on 2010-11-10
7
1,305 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 178
scoreUp challenge 14 63
"Black Box" Testing of Control System Software 2 53
awk file 6 55
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

822 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