ksh "while" loop within a csh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh "while" loop within a csh script
# 1  
Old 03-12-2006
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!
# 2  
Old 03-12-2006
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
# 3  
Old 03-12-2006
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
# 4  
Old 03-12-2006
Sorry. I'm not a csh expert.
# 5  
Old 03-12-2006
To add the string " 0000" to the end of each line in the file...
Code:
awk '{print $0 " 0000"}' $INFILE > $OUTFILE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

How to fix this "Input is not UTF-8" while executing ksh script?

Hi, I have an automated script which have set of sqls but when i am trying to execute the automated script by using nohup command, it is throwing an error like "Input is not UTF-8" . And when i digged in to it, i am getting some "Â" character in the shell script. How to avoid this!? Thanks. (1 Reply)
Discussion started by: Samah
1 Replies

3. Shell Programming and Scripting

Purpose of "read" and "$END$" in ksh ?

Hi, Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator? cat $AMS/version|read a b verno d DBVer=$(/usr/bin/printf "%7s" $verno) I checked that the cat $AMS/version command returns following output: ... (10 Replies)
Discussion started by: dbadmin100
10 Replies

4. Shell Programming and Scripting

ksh script that echo " please insert your name " and store the output to a login.log file.

Hello All Nice to meet you all here in this forum, it's my 1rst time here i'm asking about a little issue that i face i added a ksh script that echo " please insert your name " and store the output to a login.log file. the script is working fine with normal telnet but Xstart is not working... (8 Replies)
Discussion started by: islam.said
8 Replies

5. Shell Programming and Scripting

How to "time" a loop in CSH ?

Hi, I am trying to estimate the time that each part of my program takes to execute. With BASH, I used to use time: #!/bin/bash time while do .... .... done This work just fine. However some of my program is in CSH. I tried the same kind of command, but it does not work: #!/bin/csh... (3 Replies)
Discussion started by: jolecanard
3 Replies

6. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, I´ve already searched the forum but can´t find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

7. Shell Programming and Scripting

Ksh script function, how to "EXIT 2" without killing the current process?

Hi, Using AIX 5.3 and Ksh. />ls -al /usr/bin/ksh -r-xr-xr-x 5 bin bin 237420 Apr 10 2007 /usr/bin/ksh /> I recently started working for a new employer. I have written UNIX K-Shell scripts for many years and have never had this particular issue before. Its perplexing me. I have... (2 Replies)
Discussion started by: troym72
2 Replies

8. HP-UX

script running with "ksh" dumping core but not with "sh"

Hi, I have small script written in korn shell. When it is called from different script, its dumping core, but no core dump when we run it standalone. And its not dumping core if we run the script using "/bin/sh" instead of "ksh" Can some body please help me how to resolve this issue. ... (9 Replies)
Discussion started by: simhe02
9 Replies

9. Shell Programming and Scripting

ksh script as a login shell return "no controlling terminal"

I have created a ksh shell script and used it as a login shell for a user. </etc/passwd> lramirev:x:111:200:Luis:/export/home/menush:/usr/local/menush/menush My shell script is like this: </usr/local/menush/menush> #!/bin/ksh # if ] then . $HOME/.profile fi ... (8 Replies)
Discussion started by: lramirev
8 Replies

10. Shell Programming and Scripting

Exactly How can we define "sh ksh csh &bash"

Hi - I m prashant. I m new in UNIX&LINUX world. I want to ask that how can we define the shell in Linux like bash,ksh,csh in Linux. What is the use of these shells. I know there are mny experts on net if you can tell me then please do me this favour and tell me about this topic. ... (1 Reply)
Discussion started by: prashantsingh
1 Replies
Login or Register to Ask a Question