Solved

unix ksh: script to read input from file

Posted on 2010-11-10
7
1,298 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now