Avatar of Watnog
WatnogFlag for Belgium

asked on 

unix ksh: script to read input from file

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.
System ProgrammingLinux OS Dev

Avatar of undefined
Last Comment
Watnog
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

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

 
 
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

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!
Avatar of Watnog
Watnog
Flag of Belgium image

ASKER

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


Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

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

 
 
 
 

 
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Watnog
Watnog
Flag of Belgium image

ASKER

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.
Avatar of Watnog
Watnog
Flag of Belgium image

ASKER

That does it wnp, many thanks!
System Programming
System Programming

Kernel and system programming is the process of creating the software necessary for a computer or device to function and operate other programs. Some operating systems (such as Microsoft Windows) are proprietary, but others, such as the various Linux distributions, are open source.

41K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo