Solved

unix ksh: script to read input from file

Posted on 2010-11-10
7
1,302 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux : how to point the OS to use the new DNS server 11 101
Smart Camera scanning and reading information 3 100
improve Shell script 5 145
ShiftLeft challenge 21 78
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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

27 Experts available now in Live!

Get 1:1 Help Now