The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script running with "ksh" dumping core but not with "sh" simhe02 HP-UX 9 4 Weeks Ago 05:52 PM
#!/bin/sh script fails at StringA | tr "[x]" "[y]" by_tg UNIX for Dummies Questions & Answers 3 02-22-2008 09:17 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-19-2007 10:52 PM
Avoid "++ requires lvalue" Error in Loop Calculation sandeepb Shell Programming and Scripting 3 09-24-2007 04:02 AM
How to combine "find" command in for each loop (tcsh) umen Shell Programming and Scripting 3 08-22-2005 01:07 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-12-2006
Registered User
 

Join Date: Mar 2006
Posts: 8
ksh "while" loop within a csh script

I'm no unix pro for sure, but I have programmed enough other languages to usually get the job done in unix when I have to.

I'm currently trying to automate a manual diagnostic process. One of the steps in the manual process generates a file of text output. The next step is running a small script that adds the string " 0000" to the end of each line in the file, to prep it as input for a csh script.

I thought it would be easy to combine the two scripts, but the script that adds the zeros is a ksh script with a "while" loop. Dropping that code into the csh script doesn't work. I have tried making the ksh snippet as csh-compliant as I know how, but I just don't know enough...

Can you suggest how to duplicate this ksh functionality under csh? Here's the ksh code I have:


#!/bin/ksh
touch outfile

cat infile | awk '{print $0}' |
while read line
do
echo "$line 0000" >> outfile
done


Would there be any other considerations if "infile" and "outfile" were variables like $INFILE and $OUTFILE?

I appreciate the help!
Reply With Quote
Forum Sponsor
  #2  
Old 03-12-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,656
Some suggestions...
touching the file is not really needed
"cat file |" is a waste of a process. The next process can read from the file as easily as reading from a pipe.
"| awk '{print $0}' |" is another expensive no-op
echo >> outfile will open outfile for each line...just open the file once
convert the csh script into ksh...ksh is the better shell
or just invoke the csh script from inside of the ksh script

#! /usr/bin/ksh
exec < infile
while read line ; do
echo $line 0000
done > outfile
/path/to/csh/script < outfile
Reply With Quote
  #3  
Old 03-12-2006
Registered User
 

Join Date: Mar 2006
Posts: 8
Thanks for the advice! I was pretty sure that the ksh snippet was a kludge from someone who wasn't a pro either.

HOWEVER... I had a feeling that the popular answer would be to move everything into ksh. That's where I have a problem. The csh script is a bit lengthy, and it does tasks before and after the ksh piece. I think it'll be quicker for me to convert the little ksh snippet into csh instead of the other way around.

Can you help out with that? Does csh have an equivalent to "while read"? I've tried the following:


cat $INFILE | \
while (set INPUTLINE = $<)
echo "$INPUTLINE 0000" >> $OUTFILE
end


But it gets hung up, as I think the "$<" part is waiting for input from the keyboard.

What would happen if I split the csh file into two parts, and did this?

#!/bin/ksh
part1.csh
[insert ksh 0000 snippet here]
part2.csh
Reply With Quote
  #4  
Old 03-12-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,656
Sorry. I'm not a csh expert.
Reply With Quote
  #5  
Old 03-12-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
To add the string " 0000" to the end of each line in the file...
Code:
awk '{print $0 " 0000"}' $INFILE > $OUTFILE
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0